Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate closing of applications in Android

Tags:

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 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.

like image 832
Luke Avatar asked Aug 25 '11 18:08

Luke


People also ask

Can automatically close apps on Android?

Swipe the app you want to get shut down, and the app automatically closes. Easy right? If your recently used apps appear, it means that the apps are not open, but they're in standby mode. This can help you with navigation or multitasking.

How do I set my phone to automatically close apps?

Now, go to Settings, Developer Options, Processes, or Settings, System, Developer Options, Running Services. Now, on the list of apps before you, tap the app that you want to kill and select Stop. This will stop the app from running.


1 Answers

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" />
like image 172
alderz Avatar answered Oct 18 '22 07:10

alderz