Android获取所有App信息

这段代码展示了如何在Android应用中获取所有已安装应用的信息,并将这些信息保存到SDcard的指定文件中。包括应用名称、包名、版本号、是否可卸载等,并针对Android 11及以上版本需要的权限进行了说明。
摘要由CSDN通过智能技术生成

示例代码

/**
* 获取所有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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值