5

I'm loading all apps from android and then I display them in my launcher (icon + name). Code looks like following:

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

Package names and activity names for all my PhoneAppItem items are retrieved like following (for android < 5 which is relevant for the only yet known device having this issue):

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

I now have a user that get's following exception:

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)
    ...

Observations

  • the exception above is thrown for all apps, even for my own app
  • until now, this behaviour is unique to one user using following hardware: Lenovo Yoga Tablet HD+ (b8080), Android 4.4.2

Question

Does anyone have an idea why this could happen?

prom85
  • 16,896
  • 17
  • 122
  • 242
  • do you have the required permissions? – Vladyslav Matviienko Jun 20 '17 at 11:39
  • Which one? For getting the list of apps I do have the permissions `android.permission.GET_TASKS` in the manifest. For the `PackageManager.getActivityInfo(...)` call which throws the exception nothing is needed as far as I know – prom85 Jun 20 '17 at 11:45
  • @prom85 you need to define the proper ComponentName: new ComponentName=("appName","appname.mainactivity") check ur code for PhoneAppItem... – PN10 Jun 27 '17 at 13:11
  • @prom85 Do you have the device ? – Krish Jun 28 '17 at 13:10
  • No. So I can't reproduce this... – prom85 Jun 28 '17 at 15:08
  • there are two possibility. 1) getActivityInfo is deprecated. OR 2) if in application code we put security parameters no one can get your package name or activity name with the help of this function – Hemant Sankhla Jun 30 '17 at 05:57
  • For Android 11 see this question: https://stackoverflow.com/q/62345805/8214617 – Noam Oct 19 '20 at 20:10

1 Answers1

2

If processInfo.processName does not work, try another method of getting package:

processInfo.pkgList[0]
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90