Android 7.1下关闭指定后台运行应用的技术问询
Hey there! I see you’ve built code to shut down three specific background apps on Android 7.1—let’s walk through what’s working here, fix potential gaps, and refine the implementation for better reliability.
First, Your Original Code
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CloseApps(); } private void CloseApps() { ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo runningProInfo : procInfos) { if (runningProInfo.processName.equals("com.aaa.aaaa")) { actvityManager.killBackgroundProcesses(runningProInfo.processName); }else if (runningProInfo.processName.equals("com.ccc.ccc")) { actvityManager.killBackgroundProcesses(runningProInfo.processName); } else if (runningProInfo.processName.equals("com.bbb.bbb.ad")){ actvityManager.killBackgroundProcesses(runningProInfo.processName); } } }
Key Issues & Fixes for Android 7.1
1. Missing Required Permission
Android 6.0+ requires explicit permission to kill background processes. Add this line to your AndroidManifest.xml first:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Note: Even with this permission, Android 7.1 blocks killing foreground apps or critical system processes—this only works on expendable background processes.
2. Limitations of getRunningAppProcesses()
On Android 7.1, this method no longer returns all running processes. It only shows processes tied to your app, core system services, and visible apps. Your code might miss target apps even if they’re running in the background.
3. Cleaned-Up, More Reliable Code
We can simplify the logic using a set of target package names, add error handling for null services/empty process lists, and add logging to track what’s happening:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); closeTargetApps(); } private void closeTargetApps() { // Store target package names in a set for quick checks Set<String> targetPackages = new HashSet<>(Arrays.asList( "com.aaa.aaaa", "com.ccc.ccc", "com.bbb.bbb.ad" )); ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); if (activityManager == null) { Log.e("AppCloser", "Failed to access ActivityManager service"); return; } List<ActivityManager.RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses(); if (runningProcesses == null || runningProcesses.isEmpty()) { Log.d("AppCloser", "No running processes detected"); return; } for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { String processName = processInfo.processName; if (targetPackages.contains(processName)) { Log.d("AppCloser", "Attempting to kill: " + processName); activityManager.killBackgroundProcesses(processName); } } }
Critical Android 7.1 Behavior Notes
killBackgroundProcesses()is a request, not a command: The system may ignore your request if it needs the process for memory management or other priorities.- Apps might restart automatically: Many apps have background services that reboot the process after it’s killed. You can’t prevent this without user consent to disable the app.
- Aggressive stopping (like
forceStopPackage()) is restricted: This requires a system-level permission only granted to pre-installed system apps—regular third-party apps can’t use it.
内容的提问来源于stack exchange,提问作者GNK

