-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceUtils.java
More file actions
416 lines (368 loc) · 12.4 KB
/
DeviceUtils.java
File metadata and controls
416 lines (368 loc) · 12.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class DeviceUtils {
/**
* 获取屏幕分辨率
*/
public static DisplayMetrics getDisplayMetrics(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics;
}
/**
* dp 转化为 px
*
* @param context context
* @param dpValue dpValue
* @return int
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 转化为 dp
*
* @param context context
* @param pxValue pxValue
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 获取设备宽度(px)
*
* @param context context
* @return int
*/
public static int deviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取设备高度(px)
*/
public static int deviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取控件宽高
*/
public static void widgetWidthHeight(View view){
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
view.getHeight();
view.getWidth();
System.out.println(attractionImgIV.getHeight() + "----------------" + attractionImgIV.getWidth());
}
});
}
/**
* SD卡判断
*
* @return boolean
*/
public static boolean isSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 是否有网
*
* @param context context
* @return boolean
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 返回版本名字
* 对应build.gradle中的versionName
*
* @param context context
* @return String
*/
public static String getVersionName(Context context) {
String versionName = "";
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return versionName;
}
/**
* 返回版本号
* 对应build.gradle中的versionCode
*
* @param context context
* @return String
*/
public static String getVersionCode(Context context) {
String versionCode = "";
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionCode = String.valueOf(packInfo.versionCode);
} catch (Exception e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 获取设备的唯一标识,deviceId
*
* @param context context
* @return String
*/
public static String getDeviceId(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
if (deviceId == null) {
return "-";
} else {
return deviceId;
}
}
/**
* 获取Wifi ip地址
*
* @param context
* @return
*/
public static String getWifiIpAddress(Context context) {
try {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int i = wifiInfo.getIpAddress();
return int2ip(i);
} catch (Exception ex) {
return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage();
}
}
/**
* 将ip的整数形式转换成ip形式
*
* @param ipInt
* @return
*/
private static String int2ip(int ipInt) {
StringBuilder sb = new StringBuilder();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
public String getLocalIpAddress() {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
}
return null;
}
/**
* 获取手机品牌
*
* @return String
*/
public static String getPhoneBrand() {
return android.os.Build.BRAND;
}
/**
* 获取手机型号
*
* @return String
*/
public static String getPhoneModel() {
return android.os.Build.MODEL;
}
/**
* 获取手机Android API等级(22、23 ...)
*
* @return int
*/
public static int getBuildLevel() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* 获取手机Android 版本(4.4、5.0、5.1 ...)
*
* @return String
*/
public static String getBuildVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 获取当前App进程的id
*
* @return int
*/
public static int getAppProcessId() {
return android.os.Process.myPid();
}
/**
* 获取当前App进程的Name
*
* @param context context
* @param processId processId
* @return String
*/
public static String getAppProcessName(Context context, int processId) {
String processName = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 获取所有运行App的进程集合
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info
= (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == processId) {
CharSequence c = pm.getApplicationLabel(
pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
processName = info.processName;
return processName;
}
} catch (Exception e) {
Log.e(DeviceUtils.class.getName(), e.getMessage(), e);
}
}
return processName;
}
/**
* 创建App文件夹
*
* @param appName appName
* @param application application
* @return String
*/
public static String createAPPFolder(String appName, Application application) {
return createAPPFolder(appName, application, null);
}
/**
* 创建App文件夹
*
* @param appName appName
* @param application application
* @param folderName folderName
* @return String
*/
public static String createAPPFolder(String appName, Application application, String folderName) {
File root = Environment.getExternalStorageDirectory();
File folder;
/**
* 如果存在SD卡
*/
if (DeviceUtils.isSDCardAvailable() && root != null) {
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
} else {
/**
* 不存在SD卡,就放到缓存文件夹内
*/
root = application.getCacheDir();
folder = new File(root, appName);
if (!folder.exists()) {
folder.mkdirs();
}
}
if (folderName != null) {
folder = new File(folder, folderName);
if (!folder.exists()) {
folder.mkdirs();
}
}
return folder.getAbsolutePath();
}
/**
* 通过Uri找到File
*
* @param context context
* @param uri uri
* @return File
*/
public static File uri2File(Activity context, Uri uri) {
File file;
String[] project = { MediaStore.Images.Media.DATA };
Cursor actualImageCursor = context.getContentResolver()
.query(uri, project, null, null, null);
if (actualImageCursor != null) {
int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(
MediaStore.Images.Media.DATA);
actualImageCursor.moveToFirst();
String img_path = actualImageCursor.getString(actual_image_column_index);
file = new File(img_path);
} else {
file = new File(uri.getPath());
}
if (actualImageCursor != null) actualImageCursor.close();
return file;
}
/**
* 获取AndroidManifest.xml里 <meta-data>的值
*
* @param context context
* @param name name
* @return String
*/
public static String getMetaData(Context context, String name) {
String value = null;
try {
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
value = appInfo.metaData.getString(name);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return value;
}
/**
* 复制到剪贴板
*
* @param context context
* @param content content
*/
public static void copy2Clipboard(Context context, String content) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager clipboardManager
= (android.content.ClipboardManager) context.getSystemService(
Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText(context.getString(R.string.app_name),
content);
clipboardManager.setPrimaryClip(clipData);
} else {
android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(content);
}
}
}