29

I need to get the name of launcher activity to launch the activity from my application. Any solution

user1767260
  • 1,543
  • 8
  • 26
  • 51
  • ComponentName name = new ComponentName(Package,launcheractivity); 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); _context.startActivity(i); Here I want name of launcheractivity for ComponentName.. – user1767260 Oct 23 '12 at 09:33
  • Launcher activity of any specific app or all installed apps? – Paresh Mayani Oct 23 '12 at 09:37
  • Launcher activity of installed apps – user1767260 Oct 23 '12 at 09:42
  • Take a look at [this answer](http://stackoverflow.com/a/5097838/318508), it lists the required information for all installed apps. – molnarm Oct 23 '12 at 09:33
  • if you hv the package name then why dont you try directly my code . – Vivek Bajpai Dec 03 '12 at 06:42

4 Answers4

63

late but the better way it will give the exact intent to launch an activity

PackageManager pm = getPackageManager();
Intent intent=pm.getLaunchIntentForPackage(pacakgeName);
startActivity(intent);
Vivek Bajpai
  • 1,617
  • 1
  • 19
  • 35
26

Use the following code to get the launcher activity of all packages:

final PackageManager pm = getPackageManager();

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));

for (ResolveInfo temp : appList) {
    Log.v("my logs", "package and activity name = "
            + temp.activityInfo.packageName + "    "
            + temp.activityInfo.name);
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
19

Even though the answers above answer directly the OP's question I would like to add my two cents:

/** Backwards compatible method that will clear all activities in the stack. */
public void startLauncherActivity(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
    ComponentName componentName = intent.getComponent();
    Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName);
    context.startActivity(mainIntent);
}

Here I do not only get the launcher activity of the application, but also am clearing all the backstack of the activities (which is what I actually needed when I triggered the launcher activity). I call this in case of expired auth token for example.

Important thing is to use IntentCompat, otherwise one has to resort to Intent flag Intent.FLAG_ACTIVITY_CLEAR_TASK, which is introduced only in API 11.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
3

This is the easiest solution you can use. And it works perfectly.`

private String getLauncherActivityName(){
        String activityName = "";
        final PackageManager pm = getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(getPackageName());
        List<ResolveInfo> activityList = pm.queryIntentActivities(intent,0);
        if(activityList != null){
            activityName = activityList.get(0).activityInfo.name;
        }
        return activityName;
    }
SHISHIR
  • 321
  • 3
  • 4