Question

I want to see the permission of running apps of android in my software.

For this reason ,I have the following code :

   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){}

But there is a problem.

When I run my apps ,I find that the app name and app's package name are not consistent . Why has this problem introduced?

The main problem is described follow:

Let us suppose an apps named "EBOOK_Reader" and "Camera" is running in my device . The package name is "com.a.b" and "com.c.d" respectively. The problem of this code is the appropriate package name is not with appropriate apps name .

It shows the package name Of "com.a.b" to "Camera " and "com.c.d" to "EBOOK_Reader" which is not desired .

Any idea of how can the problem be solved? ThankYou

Was it helpful?

Solution 2

The app name and app's package name are normally different. You better use the package name as this is unique throughout the device.

Update: Now I understand your problem. Thanks for clarifying. It is because of the variable row_count. Basically you're are using two different iterator variables. That's why your getting 2 different results. You don't need row_count because you already have interator for i. Try the updated code below:

Basically l.get(row_count).processName was replaced by info.processName.

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 ; // no need for this. feel free to delete

   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(info.processName);

       PackageInfo packageInfo =  packageManager.getPackageInfo(info.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++; // no need for this. feel free to delete
       // app.setSize(p.s)
       apps.add(app);

     }

     catch(Exception e){}

OTHER TIPS

This is correct and Running:

   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;
       }
    }
  }

Process names are not bound to the application package name. They happen to be the same by default, as a convenience. However, each app is free to change its process name in its manifest using the android:process attribute, or to spawn more processes with different names for various components.

And in even more advanced scenarios, multiple applications can share the same process.

In particular, what this means is you can't use the process name to get the application(s) that are running currently. You should instead iterate over the list of packages that are loaded in that process using the RunningAppProcessInfo.pkgList field instead. Keep in mind that it is an array, and can contain more than one application package name. (See the note about the advanced scenarios above)

On a separate note, as the documentation for the getRunningAppProcesses() states:

Note: this method is only intended for debugging or building a user-facing process management UI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top