用户 ActivityManager 在 Android 中排名靠前 Activity 的正确方法是什么?
Which is the Correct way to user ActivityManager to get top Activity in Android?
我的自定义启动器使用以下代码来识别顶部 activity,我与一些允许的应用程序进行比较 运行。
private void restoreApp() {
ctx = this;
am = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
activePackages = getActivePackages();
} else {
activePackages = getActivePackagesCompat();
}
Log.i(TAG, "restoreApp() -> ");
Boolean testPackage = false;
if (activePackages != null) {
for (String activePackage : activePackages) {
Log.i(TAG, "testing -> "+activePackage);
if(!activePackage.contains("com.estapar.mobile.launcher")){
if (allowedApps.testApp(activePackage)) {
restoreAppExec(activePackage);
testPackage = true;
Log.i(TAG, "restoring -> "+activePackage);
}
}
}
if(!testPackage){
restoreAppExec("launcher");
}
}
else{
Log.i(TAG, "activePackages eh nulo");
}
}
String[] getActivePackagesCompat() {
final List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
final ComponentName componentName = taskInfo.get(0).topActivity;
final String[] activePackages = new String[1];
activePackages[0] = componentName.getPackageName();
return activePackages;
}
String[] getActivePackages() {
final Set<String> activePackages = new HashSet<String>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
String mpackageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
String mPackageName2 = am.getRunningAppProcesses().get(0).processName;
Log.i("asd",mpackageName);
Log.i("asd",mPackageName2);
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
activePackages.addAll(Arrays.asList(processInfo.pkgList));
}
}
return activePackages.toArray(new String[activePackages.size()]);
}
但在 Android 版本 5.1.1 中它停止工作 =(,总是返回启动程序包。
有人知道在这个 android 版本中使用这种方法的正确方法吗?
谢谢。
根据 android 文档
,这不再可能了
This method was deprecated in API level 21. As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still retu rn a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.
我的自定义启动器使用以下代码来识别顶部 activity,我与一些允许的应用程序进行比较 运行。
private void restoreApp() {
ctx = this;
am = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
activePackages = getActivePackages();
} else {
activePackages = getActivePackagesCompat();
}
Log.i(TAG, "restoreApp() -> ");
Boolean testPackage = false;
if (activePackages != null) {
for (String activePackage : activePackages) {
Log.i(TAG, "testing -> "+activePackage);
if(!activePackage.contains("com.estapar.mobile.launcher")){
if (allowedApps.testApp(activePackage)) {
restoreAppExec(activePackage);
testPackage = true;
Log.i(TAG, "restoring -> "+activePackage);
}
}
}
if(!testPackage){
restoreAppExec("launcher");
}
}
else{
Log.i(TAG, "activePackages eh nulo");
}
}
String[] getActivePackagesCompat() {
final List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
final ComponentName componentName = taskInfo.get(0).topActivity;
final String[] activePackages = new String[1];
activePackages[0] = componentName.getPackageName();
return activePackages;
}
String[] getActivePackages() {
final Set<String> activePackages = new HashSet<String>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
String mpackageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
String mPackageName2 = am.getRunningAppProcesses().get(0).processName;
Log.i("asd",mpackageName);
Log.i("asd",mPackageName2);
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
activePackages.addAll(Arrays.asList(processInfo.pkgList));
}
}
return activePackages.toArray(new String[activePackages.size()]);
}
但在 Android 版本 5.1.1 中它停止工作 =(,总是返回启动程序包。
有人知道在这个 android 版本中使用这种方法的正确方法吗?
谢谢。
根据 android 文档
,这不再可能了This method was deprecated in API level 21. As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still retu rn a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.