Returns Package name of base activity - Android android.app

Android examples for android.app:Activity Start

Description

Returns Package name of base activity

Demo Code

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import java.util.List;

public class Main {

  /**//from  w  ww  .  j  a va  2 s. com
   * Returns Package name of base activity.
   * <p>
   * Requires GET_TASK permission
   * 
   * @param context
   *          Context to get base activity information
   * @return String containing base package name
   */
  public static String getBaseActivityPackageName(Context context) {
    ComponentName activity = getBaseActivity(context);
    if (activity == null) {
      return null;
    }
    return activity.getPackageName();
  }

  private static ComponentName getBaseActivity(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (am == null) {
      return null;
    }
    List<RunningTaskInfo> info = am.getRunningTasks(1);
    if (info == null) {
      return null;
    }
    ComponentName activity = info.get(0).baseActivity;
    return activity;
  }

}

Related Tutorials