在旧版本中,以下代码可以正常运行。
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
String pkgname = am.getRunningTasks(1).get(0).topActivity.getPackageName();
它不再适用于Android L.它始终返回启动器的包名称。我怎样才能在Android L中获得它?
答案 0 :(得分:2)
也许可以帮到你:
ActivityManager mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{
String mpackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}