Reputation: 106
I am using following line of code to get the package name of top running application in Android
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
What I want is the label name of top running activity in the stack, like if facebook is running on the top , I should get "facebook" as label name of the application
Upvotes: 2
Views: 2546
Reputation: 106
public String getTopActivityStackName()
{
ActivityManager mActivityManager = (ActivityManager)
getSystemService(Activity.ACTIVITY_SERVICE);
PackageManager mPackageManager = getPackageManager();
String packageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
ApplicationInfo mApplicationInfo;
try
{
mApplicationInfo = mPackageManager.getApplicationInfo( packageName, 0);
} catch (NameNotFoundException e) {
mApplicationInfo = null;
}
String appName = (String) (mApplicationInfo != null ?
mPackageManager.getApplicationLabel(mApplicationInfo) : "(unknown)");
return appName;
}
Upvotes: 1
Reputation: 438
try this i dont know whether it help you or not ,
PackageManager pm = context.getPackageManager();
RunningAppProcessInfo info = am.getRunningAppProcesses().get(0);
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); System.out.println("the label of the app is " + c); Log.w("LABEL", c.toString());
}
catch(Exception e) { //Name Not FOund Exception
}
Upvotes: 0