获取Android堆栈中最常运行活动的标签名称

时间:2013-06-03 09:52:21

标签: android

我正在使用以下代码行来获取Android中运行量最大的应用程序的包名称

ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

我想要的是堆栈中最常运行活动的标签名称,如果facebook在顶部运行,我应该“facebook”作为应用程序的标签名称

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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 

}