我有一个在后台运行的应用程序,它知道任何内置应用程序(消息,联系人等)何时运行。这个应用程序在android 6下面工作正常。
try {
ActivityManager am = (ActivityManager) aContext.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager packageManager = aContext.getPackageManager();
if (Build.VERSION.SDK_INT > 20) {
Log.e("VERSION", "+++++++++++++++" + Build.VERSION.SDK_INT);
String currentProcName = "";
for (ActivityManager.RunningAppProcessInfo processInfo : am.getRunningAppProcesses()) {
currentProcName = processInfo.processName;
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(processInfo.processName, 0);
String title = (String) ((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");
Log.e("show app", "+++++++++++++++" + currentProcName + "---- applicationInfo " + applicationInfo.uid + "app name " + title);
break;
}
} else {
Log.e("VERSION", "===============================" + Build.VERSION.SDK_INT);
List<ActivityManager.RunningTaskInfo> alltasks = am
.getRunningTasks(1);
for (ActivityManager.RunningTaskInfo aTask : alltasks) {
String CurrentString = aTask.topActivity.flattenToShortString();
String[] separated = CurrentString.split("/");
Log.e("show app", "+++++++++++++++" + "packageName " + separated[0]);
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(separated[0], 0);
final String title = (String) ((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");
Log.e("show app", "+++++++++++++++" + "---- applicationInfo " + applicationInfo.uid + "app name " + title);
}
}
} catch (Throwable t) {
Log.e("show", "Throwable caught: " + t.getMessage(), t);
}
如何在android 6中获取前台应用程序的信息?
许可:
<uses-permission android:name="android.permission.GET_TASKS" />