You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

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):

1. Replacement for 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:
    The getRunningServices() method still works for your own app's services in API 26+. Just call it with a sufficiently large maxNum value (like Integer.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 your Service classes (e.g., add the service to a list in onCreate() and remove it in onDestroy()).

  • 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 the android.permission.PACKAGE_USAGE_STATS permission (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 use UsageStatsManager to 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.

2. Fixing unexpected results from 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_STATS permission. With this, you can use UsageStatsManager.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 use PackageManager.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

火山引擎 最新活动