Search code examples
androidandroid-intentandroid-launcherandroid-homebutton

Android Launch foreign application from my own


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.


Solution

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