AppUtil.java
6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.xdy.util;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.UUID;
/**
* @author jianghongbo
* @version 1.0
* @file AppUtil.java
* @brief 获取APP信息与操作APP的工具类
* @date 2017/12/22
* Copyright (c) 2017
* All rights reserved.
*/
public class AppUtil {
static String TAG = "APP-UTIL";
private static String uniqueMarker;
private static String versionName;
private static int versionCode;
private static Object ip;
public static String getVersionName(Context context) {
try {
PackageInfo e = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionName = e.versionName;
return versionName;
} catch (PackageManager.NameNotFoundException var2) {
var2.printStackTrace();
return null;
}
}
public static int getVersionCode(Context context) {
try {
PackageInfo e = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionCode = e.versionCode;
return versionCode;
} catch (PackageManager.NameNotFoundException var2) {
var2.printStackTrace();
return -1;
}
}
public static String getPackageName(Context context) {
try {
PackageInfo e = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return e.packageName;
} catch (PackageManager.NameNotFoundException var2) {
LogUtil.e(TAG, "getPackageName %s", new Object[]{var2.toString()});
return "";
}
}
public static String getAppName(Context context) {
try {
PackageInfo e = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return e.applicationInfo.name;
} catch (PackageManager.NameNotFoundException var2) {
LogUtil.e(TAG, "getAppName %s", new Object[]{var2.toString()});
return "";
}
}
/**
* @return String 设备产商
* @brief 获得当前设备产商名
*/
public static String getMaker() {
return Build.MANUFACTURER.toLowerCase();
}
/**
* 关闭APP
*/
public static void shutDownApp() {
android.os.Process.killProcess(android.os.Process.myPid());
}
/**
* 获得元数据方法
*/
public static String getMetaDataValue(Context ctx, String name) {
Object value = null;
PackageManager packageManager = ctx.getApplicationContext()
.getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(ctx.getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null && applicationInfo.metaData != null) {
value = applicationInfo.metaData.get(name);
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Could not read the name in the manifest file.", e);
}
if (value == null) {
throw new RuntimeException("The name '" + name
+ "' is not defined in the manifest file's meta data.");
}
return value.toString();
}
/**
* @return String 当前设备ID
* @brief 获得设备ID
*/
public static String getDeviceId(Context ctx) {
if (uniqueMarker == null) {
uniqueMarker = getUniqueMarker(ctx);
}
return uniqueMarker;
}
private static String getUniqueMarker(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
String tmDevice, tmSerial, androidId;
tmDevice = tm.getDeviceId();
tmSerial = tm.getSimSerialNumber();
androidId = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
UUID deviceUuid = null;
String uniqueId = null;
if (!TextUtils.isEmpty(androidId)) {
deviceUuid = new UUID(androidId.hashCode(),
((long) (tmDevice == null ? 0 : tmDevice.hashCode() << 32) |
(tmSerial == null ? 100 : tmSerial.hashCode())));
uniqueId = deviceUuid.toString();
} else {
deviceUuid = UUID.randomUUID();
uniqueId = deviceUuid.toString();
}
// LogUtil.i("debug", "uuid=" + uniqueId);
return uniqueId;
}
/**
* @return String 系统版本号
* @brief 获得当前系统版本号
*/
public static String getOSVersion() {
return Build.VERSION.RELEASE;
}
public static String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()
&& inetAddress instanceof Inet4Address) {
// if (!inetAddress.isLoopbackAddress() && inetAddress
// instanceof Inet6Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// private String getLocalIpAddress(Context ctx) {
// WifiManager wifiManager = (WifiManager) ctx.getSystemService(WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// // 获取32位整型IP地址
// int ipAddress = wifiInfo.getIpAddress();
//
// //返回整型地址转换成“*.*.*.*”地址
// return String.format("%d.%d.%d.%d",
// (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
// (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
// }
}