2

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

4

2 回答 2

1
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;
    }
于 2013-06-05T11:04:01.900 回答
0

试试这个不知道对你有没有帮助

     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 

}
于 2013-06-03T10:56:36.580 回答