Android获取所有App信息
Posted guangdeshishe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android获取所有App信息相关的知识,希望对你有一定的参考价值。
示例代码
/**
* 获取所有APP信息,保存到SDcard/download/AppInfo.txt文件中
*/
public static void getAppInfo(Context context)
if (context == null)
return;
ArrayList<String> launchList = getLauncherList(context);
Log.d("getAppInfo", Arrays.toString(new ArrayList[]launchList));
List<ApplicationInfo> packageInfos = context.getPackageManager().getInstalledApplications(0);
Log.d("myGetAppInfo", packageInfos.size() + "");
String fileName = "AppInfo.txt";
Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;//内部存储的Download路径
ContentResolver resolver = context.getContentResolver();
Uri insertUri;
String selection = MediaStore.Files.FileColumns.DISPLAY_NAME + "='" + fileName + "'";
Cursor cursor = resolver.query(external, null, selection, null, null);
if (cursor != null && cursor.moveToFirst())
int columnId = cursor.getColumnIndex(MediaStore.Downloads._ID);
insertUri = Uri.withAppendedPath(external, "" + cursor.getInt(columnId));
else
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DISPLAY_NAME, fileName);
values.put(MediaStore.Files.FileColumns.MIME_TYPE, "text/plain");//MediaStore对应类型名
values.put(MediaStore.Files.FileColumns.TITLE, fileName);
insertUri = resolver.insert(external, values);//使用ContentResolver创建需要操作的文件
PrintWriter out = null;
try
out = new PrintWriter(resolver.openOutputStream(insertUri));
out.println(ServerThread.getDeviceInfo());
for (ApplicationInfo info : packageInfos)
String packageName = info.packageName;
CharSequence charSequence = context.getPackageManager().getApplicationLabel(info);
String appName = charSequence == null ? "" : charSequence.toString();
String path = info.sourceDir;
String[] pathSplit = path.split("/");
String pathHead = pathSplit[0] + "/" + pathSplit[1] + "/" + pathSplit[2] + "/";
boolean canUninstall = false;
if (pathHead.startsWith("/data/app"))
canUninstall = true;
boolean isLaunchApp = false;
if (launchList.contains(packageName))
isLaunchApp = true;
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_CONFIGURATIONS);
StringBuilder packageInfoLineBuilder = new StringBuilder();
packageInfoLineBuilder.append(appName).append("@");
packageInfoLineBuilder.append(packageName).append("@");
packageInfoLineBuilder.append(packageInfo.versionName).append("@");
packageInfoLineBuilder.append(isLaunchApp).append("@");
packageInfoLineBuilder.append(canUninstall).append("@");
packageInfoLineBuilder.append(path);
if (isLaunchApp)
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String launchClass = packageName + "/" + intent.getComponent().getClassName();
//Log.d("myGetAppInfo", packageName + "/" + intent.getComponent().getClassName());
packageInfoLineBuilder.append("@");
packageInfoLineBuilder.append(launchClass);
//Log.d("myGetAppInfo", packageInfoLineBuilder.toString());
out.println(packageInfoLineBuilder.toString());
catch (Throwable e)
e.printStackTrace();
finally
if (out != null)
out.close();
android11以及以上版本需要在AndroidManifest.xml中添加以下权限,否则有很多APP获取不到:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
以上是关于Android获取所有App信息的主要内容,如果未能解决你的问题,请参考以下文章
关于获取Android系统所有已安装App信息的一些操作(详细)