Question

I am trying to get the following code to execute:

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME);
intent.setComponent(new ComponentName(" **Home package** "," **Home class** "));
                  startActivity(intent);

Essentially I am looking for a way to specifically target and load the exact, original, home application.

Was it helpful?

Solution 2

EDIT: SOLUTION:

        PackageManager pm=getPackageManager();
        Intent main=new Intent(Intent.ACTION_MAIN, null);

        main.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

        Collections.sort(launchables,
                         new ResolveInfo.DisplayNameComparator(pm));

        int launcher_flag = findLauncherApp(launchables);

        ResolveInfo launchable = launchables.get(launcher_flag);

        ActivityInfo activity=launchable.activityInfo;
        ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                             activity.name);
        Intent i=new Intent(Intent.ACTION_MAIN);

        i.addCategory(Intent.CATEGORY_LAUNCHER);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        i.setComponent(name);

        startActivity(i);

Where findLaucherApp() turns the List into an array of strings and interrogates each one to see if it contains "com.android.launcher2" and then returns its id.

OTHER TIPS

Technically, you have no way of always knowing "the exact, original, home application".

You can use PackageManager and queryIntentActivities() to find find who all responds to MAIN/HOME Intents. If there are two answers, and yours is one (which I am guessing is your situation), then the other is "the exact, original, home application" pretty much by definition. You can further verify this by getting to the ApplicationInfo object associated with the resolved activity and checking for FLAG_SYSTEM to see if it is installed in the system image. This approach is probably not completely bulletproof, but it may be close enough for your needs.

Yet another option is for you to simply record the current default MAIN/HOME activity when you are run for the first time. Odds are decent that your application will be run before the user elects to make you the default. Again, this has holes (e.g., they make you the default before running you for the first time).

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