![](/img/trans.png)
[英]How to find the currently running applications programmatically in Android?
[英]How to count the number of applications running in background in Android?
我一直在嘗試使用以下代碼來獲取當前在后台運行的應用程序的數量:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
int numberOfApps = runningAppProcessInfo.size();
但是,即使后台運行了多個應用程序,代碼仍返回1。
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages =
pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
// the getLaunchIntentForPackage returns an intent that you can use with startActivity()
可以通過這種方式使用PackageManager填充列表,並從List.getCount()中獲取計數總數。
我希望這個能幫上忙
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.