我正在编写一个服务,我想检测是否有任何应用程序正在运行。
是否有任何可用的 API 支持?
ActivityManager activityManager = (ActivityManager)
getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
if (services.get(0).topActivity.getPackageName().toString()
.equalsIgnoreCase(getApplicationContext().getPackageName().toString())) {
isActivityFound = true;
}
else{
isActivityFound = false;
}
您的清单需要以下权限:
uses-permission android:name="android.permission.GET_TASKS"
对于所有活动,请参阅此链接
http://qtcstation.com/2011/01/getting-info-about-your-currently-running-activities/