Android API-26中获取运行服务与进程的替代方案及问题咨询
Hey there, let's break down your questions one by one since they're both tied to Android's background privacy and restriction changes starting with Oreo (API 26):
activityManager.getRunningServices(int maxNum) in API 26+ First, it's important to understand why this method was deprecated: Google tightened background access to protect user privacy and reduce unnecessary resource usage. Here's what you can do depending on your use case:
If you only need to track your own app's running services:
ThegetRunningServices()method still works for your own app's services in API 26+. Just call it with a sufficiently largemaxNumvalue (likeInteger.MAX_VALUE) and it will return all active services belonging to your application. For a more reliable approach, you can also maintain your own list by overriding lifecycle callbacks in yourServiceclasses (e.g., add the service to a list inonCreate()and remove it inonDestroy()).If you need to access other apps' running services:
Sorry to say, but there's no direct replacement for this use case for regular apps. Android Oreo and later restrict access to other apps' background components to protect user privacy. Even if you request theandroid.permission.PACKAGE_USAGE_STATSpermission (which requires users to manually enable it in Settings > Security > Usage Access), you can't get a full list of running services. You can only useUsageStatsManagerto get information about apps that have been used recently, which can help infer if an app might be active, but not directly its running services. System apps with special privileges might have more access, but that's not feasible for regular third-party apps.
activityManager.getRunningAppProcesses() While this method isn't marked as deprecated, its behavior changed drastically starting in API 26 due to the same background restrictions. Here's what's happening and how to handle it:
Why results are unexpected:
In API 26+,getRunningAppProcesses()no longer returns all running processes on the device. It will only return:- Processes belonging to your own application
- Critical system processes
- Processes of apps that are currently visible to the user or have an active foreground service
How to get more accurate process information (if needed):
- For your own app: The method works as expected for your own processes, so you can rely on it here.
- For other apps: Again, you'll need the
android.permission.PACKAGE_USAGE_STATSpermission. With this, you can useUsageStatsManager.queryUsageStats()to retrieve stats about apps that have been used in a specific time window. This can help you determine if an app is active or recently used. Alternatively, you can usePackageManager.getInstalledPackages()to get a list of all installed apps, but this won't tell you if they're currently running.
Keep in mind that even with the usage stats permission, you won't get the same level of detail as the old
getRunningAppProcesses()method did before API 26. Android's restrictions are designed to limit apps' access to other apps' state unless absolutely necessary.
内容的提问来源于stack exchange,提问作者Jay




