Automate closing of applications in Android
I've read a lot of documentation and other posts about closing other apps. I know the documentation says Android isn't designed that way. Android wants to hibernate an app as opposed to killing the process. That makes sense but.... I wrote an app that automatically launches other apps based on system events that the user configures. Users love it and want to close/kill those apps after subsequent system events. What I am trying to say is that users are asking me to automate this for them. That is the point of my app. It automatically launches other apps and now they want to automatically close or kill them. A lot of people say that this is not possible but it is. There are several task killer apps out there and they do the very thing I would like to do. I tried the
Process.killProcess(pid);
but that doesn't seem to do much. Reading 开发者_Python百科the doco on that method it says the Linux Kernal will still enforce permissions. I even attempted to kill all processes and it didn't do anything. Perhaps I am not getting the list of processes correctly. I use the following to get a list of processes to kill
for(RunningAppProcessInfo info:activityManager.getRunningAppProcesses()){
Then I use Process.killProcess(info.pid); to kill the process. I even tried:
activityManager.killBackgroundProcesses(String.valueOf(info.pid));
activityManager.killBackgroundProcesses(String.valueOf(info.processName));
and then
Intent i = new Intent();
i.setComponent(new ComponentName(ProcessName, ProcessName+".Main"));
context.stopService(i);
Can anyone actually answer this question and not tell me it shouldn't be done or ask why I want to do it in the first place? Please provide a code sample or something meaningful.
What you said is perfectly possible. Just get the ActivityManager system service and then use .restartPackage() (deprecated) or .killBackgroundProcesses().
ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);
You will need:
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
精彩评论