复制代码
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.content.pm.Signature; import android.graphics.drawable.Drawable; import android.net.Uri; import android.text.TextUtils; import com.olemob.activity.OlemobApplication; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class AppInfoUtil { /** * 是否为系统App * * @param info * @return */ public static boolean isSystemApp(PackageInfo info) { return null != info && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); } /** * 是否为系统软件的更新软件 * * @param info * @return */ public static boolean isSystemUpdateApp(PackageInfo info) { return null != info && ((info.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); } /** * 是否为非系统应用 * * @param info * @return */ public static boolean isUserApp(PackageInfo info) { return (!isSystemApp(info) && !isSystemUpdateApp(info)); } /** * 是否为系统应用 * * @param info * @return */ public static boolean isSysApp(PackageInfo info) { return isSystemApp(info) || isSystemUpdateApp(info); } /** * 是否为非系统应用 * * @param pkgName * @return */ public static boolean isUserApp(String pkgName) { return !isSystemApp(pkgName); } /** * 判断某个应用是否是系统应用或者系统升级应用 * * @param pkgName * @return */ public static boolean isSystemApp(String pkgName) { try { PackageInfo pInfo = OlemobApplication.getInstance().getPackageManager().getPackageInfo(pkgName, 0); return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) || ((pInfo.applicationInfo .flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); } catch (NameNotFoundException e) { LogUtil.e(e); } return false; } /** * 根据包名得到App名 * * @param context * @param pkgName * @return AppName */ public static String getAppNameByPkgName(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return ""; } String name = null; PackageManager pm = context.getPackageManager(); try { ApplicationInfo info = pm.getApplicationInfo(pkgName, PackageManager.PERMISSION_GRANTED); name = (String)pm.getApplicationLabel(info); } catch (NameNotFoundException e) { LogUtil.e(e); } return name; } /** * 根据包名获得版本号 * * @param context * @param pkgName * @return * @since 2015年6月4日 */ public static int getVersionCode(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return 0; } int verName; try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(pkgName, 0); if (pi == null) { return 0; } else { verName = pi.versionCode; return verName; } } catch (Exception ignored) { } return 0; } /** * 根据包名获得版本号 * * @param context * @param pkgName * @return * @since 2015年6月4日 */ public static String getVersionName(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } String verName; try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(pkgName, 0); if (pi == null) { return null; } else { verName = pi.versionName; return verName; } } catch (Exception ignored) { } return null; } public static Drawable getAppIcon(Context context, String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } Drawable icon = null; PackageManager pm = context.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(pkgName, PackageManager.PERMISSION_GRANTED); icon = info.applicationInfo.loadIcon(pm); } catch (Exception e) { LogUtil.e(e); } return icon; } /** * 批量获取应用图标。<br> * <font color=red>注意:不要一次获取过多</font> * * @param context * @param pkgs * @return * @since 2015年6月3日 */ public static List<Drawable> getAppIcon(Context context, List<String> pkgs) { if (pkgs == null || pkgs.isEmpty()) { return null; } List<Drawable> iconList = new ArrayList<>(); PackageManager pm = context.getPackageManager(); try { for (String pkgName : pkgs) { PackageInfo info = pm.getPackageInfo(pkgName, PackageManager.PERMISSION_GRANTED); Drawable icon = info.applicationInfo.loadIcon(pm); iconList.add(icon); } } catch (Exception e) { LogUtil.e(e); } return iconList; } /** * 当前 是否可以 isDisabled * If app's state is enable, you can disable it. or you can enable it. * if return true, you can enable the app; * if return false,you can disable the app.(不知道语法对不对,先这样写吧) * * @param context * @param pkgName * @return */ public static boolean isSystemAppDisabled(Context context, String pkgName) { boolean isDisabled = false; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { @SuppressLint("PackageManagerGetSignatures") PackageInfo pkgInfo = pkgMgr .getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); @SuppressLint("PackageManagerGetSignatures") PackageInfo sysPkgInfo = pkgMgr .getPackageInfo("android", PackageManager.GET_SIGNATURES); if (pkgInfo != null && pkgInfo.signatures != null && pkgInfo.signatures.length > 0 && sysPkgInfo != null && sysPkgInfo.signatures != null && sysPkgInfo.signatures.length > 0) { isDisabled = (sysPkgInfo.signatures[0] .equals(pkgInfo.signatures[0])) || (!pkgInfo.applicationInfo.enabled);// 未开启 } } } catch (Exception e) { LogUtil.e(e); } return isDisabled; } /** * 判断一个Intent是否可用。 * * @param context 上下文 * @param intent Intent * @return * @since 2014年12月22日 */ public static boolean isIntentAvailable(final Context context, final Intent intent) { final PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS); return resolveInfo.size() > 0; } /** * 查看某一应用 Disable or enabled * * @param context * @param pkgName * @return */ public static boolean getAppleState(Context context, String pkgName) { boolean isEnable = true; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { PackageInfo pkgInfo = pkgMgr.getPackageInfo(pkgName, 0); if (pkgInfo != null) { ApplicationInfo appInfo = pkgInfo.applicationInfo; if (appInfo != null) { isEnable = appInfo.enabled; } } } } catch (Exception e) { LogUtil.e(e); } return isEnable; } /** * 获取安装的 应用总数 * * @param context * @return */ public static int getAllAppCount(Context context) { int appCount = 0; try { PackageManager pkgMgr = context.getPackageManager(); if (pkgMgr != null) { List<PackageInfo> apps = pkgMgr.getInstalledPackages(0); if (apps != null) { appCount = apps.size(); } } } catch (Exception e) { LogUtil.e(e); } return appCount; } /** * 获取手机上所有的App * * @param context * @return */ public static List<PackageInfo> getAllAppList(Context context) { PackageManager pkgMgr = context.getPackageManager(); List<PackageInfo> apps = null; if (null != pkgMgr) { try { apps = pkgMgr.getInstalledPackages(0); } catch (Exception ignored) { } } return apps; } /** * 获取手机上所有的非系统App * * @param context * @return */ public static List<PackageInfo> getUserAppList(Context context) { PackageManager pkgMgr = context.getPackageManager(); List<PackageInfo> apps = null; if (null != pkgMgr) { try { apps = pkgMgr.getInstalledPackages(0); } catch (Exception ignored) { } } List<PackageInfo> userAppList = null; if (null != apps && apps.size() > 0) { userAppList = new ArrayList<>(); for (int i = 0; i < apps.size(); i++) { if (!isSysApp(apps.get(i))) { userAppList.add(apps.get(i)); } } } return userAppList; } /** * 获取签名的MD5 * return 签名的md5 */ public static String getSignMd5Str(String pkgName) { byte[] sign = getSignInfo(pkgName); if (null == sign || sign.length == 0) { return null; } String signMd5 = null; try { signMd5 = Md5Util.getMD5String(sign); } catch (Exception e) { LogUtil.e(e); } return signMd5; } /** * 获取App签名 */ public static byte[] getSignInfo(String pkgName) { if (TextUtils.isEmpty(pkgName)) { return null; } try { @SuppressLint("PackageManagerGetSignatures") PackageInfo packageInfo = OlemobApplication.getInstance() .getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES); Signature[] signs = packageInfo.signatures; Signature sign = signs[0]; return sign.toByteArray(); } catch (Exception e) { LogUtil.e(e); } return null; } /** * 该方法耗时0.5秒,请注意在子线程下运行 * * @param pkgName */ public static String getApkMd5(String pkgName) { try { String apkPaths = getExecResult("pm path " + pkgName); if (TextUtils.isEmpty(apkPaths)) { return ""; } String path = apkPaths; String[] pathArray = apkPaths.split(":"); if (pathArray.length >= 2) { path = pathArray[1]; } String md5String = getExecResult("md5 " + path); if (!TextUtils.isEmpty(md5String) && md5String.contains("/")) { md5String = md5String.substring(0, md5String.indexOf("/")).trim(); } return md5String; } catch (Exception ignored) { } return ""; } private static String getExecResult(String argsString) { Process process = null; DataOutputStream dataOutputStream = null; try { process = Runtime.getRuntime().exec(argsString); dataOutputStream = new DataOutputStream(process.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); final StringBuilder log = new StringBuilder(); boolean firstLine = true; String line; while ((line = reader.readLine()) != null) { if (!firstLine) { log.append("*#*"); } else { firstLine = false; } log.append(line); } return log.toString(); } catch (Exception ignored) { } finally { try { if (dataOutputStream != null) dataOutputStream.close(); if (process != null) process.destroy(); } catch (Exception ignored) { } } return ""; } public static void gotoMarket(Context context, String pkgName) { if (context == null || TextUtils.isEmpty(pkgName)) { return; } try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkgName)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } catch (Exception e) { LogUtil.e(e); } } }
最后
以上就是欢喜音响最近收集整理的关于获取已安装APP的信息的全部内容,更多相关获取已安装APP内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复