Rumah > Soal Jawab > teks badan
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
这种方法在Android 5.0以上就不能用了,返回的都是桌面包。
5.0以后如何判断?还望大神赐教!
阿神2017-04-17 17:25:18
Adakah anda bekerja sebagai pelancar? Semak sama ada Aktiviti berada di bahagian atas timbunan Tugas semasa
巴扎黑2017-04-17 17:25:18
Anda boleh mencuba kaedah berikut:
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();
}