gpt4 book ai didi

android - 如何确定在 android 中以编程方式运行应用程序的权限?

转载 作者:行者123 更新时间:2023-11-30 03:42:16 30 4
gpt4 key购买 nike

我想在我的软件中查看运行android应用程序的权限。

为此,我有以下代码:

   List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);

PackageManager packageManager = getPackageManager();

List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();

PackageManager pm = this.getPackageManager();
int row_count = 0 ;

while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(l.get(row_count).processName);

PackageInfo packageInfo = packageManager.getPackageInfo(l.get(row_count).processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);

// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
row_count++;
// app.setSize(p.s)
apps.add(app);

}

catch(Exception e){}

但是有个问题。

当我运行我的应用程序时,我发现应用程序名称和应用程序包名称不一致。为什么会引入这个问题?

主要问题描述如下:

让我们假设一个名为“EBOOK_Reader”和“Camera”的应用程序正在我的设备中运行。包名分别是“com.a.b”和“com.c.d”。这段代码的问题是适当的包名称没有适当的应用程序名称。

它将“com.a.b”的包名显示为“Camera”,将“com.c.d”的包名显示为“EBOOK_Reader”,这是不需要的。

知道如何解决这个问题吗?谢谢

最佳答案

这是正确的并且正在运行:

   PackageManager mPm = getPackageManager();   
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);

for (PackageInfo pi : appList) {
System.out.println("Process Name: "+pi);
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;

for (String permission : pi.requestedPermissions) {
//Map<String, String> curChildMap = new HashMap<String, String>();
//System.out.println("############ "+permission);

try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
System.out.println("$$$$$ "+label+"!!!!!! "+desc);

} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}

关于android - 如何确定在 android 中以编程方式运行应用程序的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15541348/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com