即使对于我自己的应用程序,PackageManager也会为所有应用程序抛出NameNotFoundException

时间:2017-06-20 11:09:04

标签: android android-package-managers

我正在从Android加载所有应用程序然后我在我的启动器中显示它们(图标+名称)。代码如下所示:

public class PhoneAppItem
{
    String mPackageName = null;
    String mActivityName = null;

    String mName = null;
    ActivityInfo mActivityInfo = null;

    public PhoneAppItem(String packageName, String activityName)
    {
        mPackageName = packageName;
        mActivityName = activityName;
    }

    public void loadInfo()
    {
        PackageManager pm = MainApp.get().getPackageManager();
        try
        {
            // following line throws the exception!
            mActivityInfo = pm.getActivityInfo(new ComponentName(mPackageName, mActivityName), 0);
            // some other code...
        }
        catch (PackageManager.NameNotFoundException e)
        {
            L.e(e);
            mName = mPackageName;
        }
        catch (NullPointerException e)
        {
            L.e(e);
            mName = mPackageName;
        }
    }   
}

检索所有PhoneAppItem项目的包名称和活动名称,如下所示(对于android< 5,它与唯一尚未知道此问题的设备相关):

List<PhoneAppItem> apps = new ArrayList<>();
ActivityManager activityManager = (ActivityManager) MainApp.get().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos)
{
    try
    {
        ApplicationInfo ai = pm.getApplicationInfo(processInfo.processName, 0);
        if (!excludedPackages.contains(ai.packageName))
            apps.add(new PhoneAppItem(ai.packageName, ai.className));
    }
    catch (PackageManager.NameNotFoundException e) {}
}

我现在有一个用户得到以下异常:

android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.package.myapp/com.package.myapp.app.MainApp}
    at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:262)
    at com.package.myap.classes.PhoneAppItem.h(PhoneAppItem.java:70)
    ...

观察

  • 所有应用都会引发上述异常,即使对于我的自己的应用
  • 也是如此
  • 到目前为止,这种行为对于使用以下硬件的用户来说是独一无二的:联想瑜伽平板电脑HD +(b8080),Android 4.4.2

问题

有谁知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

如果processInfo.processName不起作用,请尝试另一种获取包的方法:

processInfo.pkgList[0]