Thursday, February 26, 2015

How to get package name in android

  If you want to fetch the package name of your app or currently opened app in android, than use the below code. Please let me know if you face any problem here.

//this will return package name of your app



public String getPackageName(Context context) {
    return context.getPackageName();
}

//this will return currently active application name


ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
 ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());

    try {
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        info.processName, PackageManager.GET_META_DATA));
        Log.w("LABEL", c.toString());
    } catch (Exception e) {
        // Name Not FOund Exception
    }
}

//this will return package name of current open app

        ActivityManager am =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
  List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
  ActivityManager.RunningTaskInfo task = tasks.get(0); // current task
  ComponentName rootActivity = task.baseActivity;

        rootActivity.getPackageName();//*currently active applications package name*

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design