如何从RunningAppProcessInfo获取PackageInfo

时间:2013-06-16 11:41:49

标签: android performance

我有以下代码

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

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

            e.printStackTrace();

        }

对于以下正在运行的应用程序,我得到nameNotFoundException。有没有其他方法来获取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

2 个答案:

答案 0 :(得分:1)

我一直使用packagename没有问题。

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

如果您只有一个与您的应用程序关联的程序包名称(通常是这种情况),则从packages [0]获取程序包名称。现在您可以使用PackageManager获取包信息:

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

标志取决于您要从PackageInfo中检索的信息。 documentation

答案 1 :(得分:0)

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());