user435739
user435739

Reputation:

How to get PackageInfo from RunningAppProcessInfo

I have the following code

    RunningAppProcessInfo p = (RunningAppProcessInfo)i.next();
        PackageInfo pi;
        try {

            pi = pm.getPackageInfo(p.processName, 0);
...
        } catch (NameNotFoundException e) {

            e.printStackTrace();

        }

For the following running apps I get nameNotFoundException. Is there any alternative way to get packageInfo

06-16 17:09:56.809:  system
06-16 17:09:56.809:  android.process.media
06-16 17:09:56.809:  com.google.process.location
06-16 17:09:56.819: com.google.android.apps.maps:LocationFriendService
06-16 17:09:56.819:  android.process.acore
06-16 17:09:56.839: com.google.android.apps.maps:FriendService

Upvotes: 0

Views: 3600

Answers (2)

vikas kumar
vikas kumar

Reputation: 11018

String packageName = getApplicationContext().getPackageName();

            ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE );
            List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
            for(ActivityManager.RunningAppProcessInfo appProcess : appProcesses){
                if( appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                    Log.i("Foreground App", appProcess.processName);

                }
            }

            sendNotifications(remoteMessage.getNotification().getBody());

Upvotes: 0

Rohan
Rohan

Reputation: 428

I have always used the packagename without problems.

String[] packages = runningappprocessinfo.pkgList;
String mypackage = packages[0];

If you have only one package name associated with your app (usually the case), you get the packagename from packages[0]. Now you can use the PackageManager to get package info:

PackageManager pm = context.getPackageManager();
PackageInfo packageinfo = pm.getPackageInfo (mypackage, 0); // use appropriate flag

The flags depend on the info you want to retrieve from PackageInfo. documentation

Upvotes: 1

Related Questions