Android如何得到当前正在屏幕上运行的程序包名?
黄舟
黄舟 2017-04-17 17:23:18
0
2
653
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

这种方法在Android 5.0以上就不能用了,返回的都是桌面包。
5.0以后如何判断?还望大神赐教!

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
阿神

Are you working as a launcher? Check whether an Activity is on the top of the current Task's stack

巴扎黑

You can try the following methods:

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();
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template