Android开发——DeviceUtil,获取Android设备信息的工具类

  • Android 设备工具DeviceUtil
  • Android 设备工具DeviceUtil dp转换px px转换dp 设备宽度 设备高度 SD卡判断 网络判断 VersionName VersionCode DeviceId 手机品牌 手机型号 系统Android API等级 系统Android 版本 App进程id App进程Name 创建App缓存文件夹 Uri转换File 获取AndroidManifestxml里meta data的值 DeviceUtil全部源码
  • dp转换px
  • 在自定义View中必须用到这个方法。自定义View的时候,不能像在xml那样可以设置dp(密度),只能设置px(像素)。不同分辨率的设备,设置px,显示就不会一样了。为了结合设计图设置dp,需要将dp转换为px值,然后再设置。
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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.File;
import java.util.Iterator;
import java.util.List;
public class DeviceUtil {
/**
* dp 转化为 px
* @param context
* @param pxValue
* @return
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
px转换dp
/**
* px 转化为 dp
* @param context
* @param pxValue
* @return
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
设备宽度
/**
* 获取设备宽度(px)
* @param context
* @return
*/
public static int deviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
设备高度
/**
* 获取设备高度(px)
* @param context
* @return
*/
public static int deviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
SD卡判断
/**
* SD卡判断
* @return
*/
public static boolean isSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}


/**
* SD卡判断
*
* @return
*/
public static boolean isSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

/**
* 是否有网
*
* @param context
* @return
*/
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
* @return
*/
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
* @return
*/
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
* @return
*/
public static String getDeviceId(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
@SuppressLint("MissingPermission") String deviceId = tm.getDeviceId();
if (deviceId == null) {
return "";
} else {
return deviceId;
}
}

/**
* 获取手机品牌
*
* @return
*/
public static String getPhoneBrand() {
return android.os.Build.BRAND;
}

/**
* 获取手机型号
*
* @return
*/
public static String getPhoneModel() {
return android.os.Build.MODEL;
}

/**
* 获取手机Android API等级(22、23 ...)
*
* @return
*/
public static int getBuildLevel() {
return android.os.Build.VERSION.SDK_INT;
}

/**
* 获取手机Android 版本(4.4、5.0、5.1 ...)
*
* @return
*/
public static String getBuildVersion() {
return android.os.Build.VERSION.RELEASE;
}

/**
* 获取当前App进程的id
*
* @return
*/
public static int getAppProcessId() {
return android.os.Process.myPid();
}

/**
* 获取当前App进程的Name
*
* @param context
* @param processId
* @return
*/
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(DeviceUtil.class.getName(), e.getMessage(), e);
}
}
return processName;
}

/**
* 创建App文件夹
*
* @param appName
* @param application
* @return
*/
public static String createAPPFolder(String appName, Application application) {
return createAPPFolder(appName, application, null);
}

/**
* 创建App文件夹
*
* @param appName
* @param application
* @param folderName
* @return
*/
public static String createAPPFolder(String appName, Application application, String folderName) {
File root = Environment.getExternalStorageDirectory();
File folder;
/**
* 如果存在SD卡
*/
if (DeviceUtil.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
* @param uri
* @return
*/
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里 的值
*
* @param context
* @param name
* @return
*/
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;
}


}