First, I found some similar questions on StackOverflow but none of the answers there solves my problem. I also googled it and read all the top 30 results.
So I am writing a Service that will check what is running in foreground every, say, 20 seconds. If the foreground application is on a blacklist, then I will popup a dialog Activity to say that "the current application needs to be stopped", then when user press OK of that dialog, that application will be stopped via the following method:
private void killApp(String packageName) {
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(ACTIVITY_SERVICE);
Log.d(TAG, "Trying to kill app " + packageName);
am.killBackgroundProcesses(packageName);
}
However, the application is not stopped at all in my test. When the dialog is closed, the foreground app is still there, exactly the same state as it was before the dialog popped up. I believe I have the right API level as well as necessary permissions:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
I am really stuck, any help will be greatly appreciated!
Removing a background process (kill command) 1 Use the ps command to determine the process ID of the process you want to remove. You might want to pipe this command... 2 In the following example, you issue the find command to run in the background. You then decide to cancel the process. More ...
We can easily kill all processes running on the browser and close it by using: 4. Bonus fg is a command used to bring a background process to the foreground. Then, we can simply stop the process by using Ctrl+C: 4.2. System Monitor Another way to end a process is by launching the System Monitor, the Linux equivalent of the Windows Task Manager.
In Task Manager window, you can tap Process tab to see all running applications and processes incl. background processes in your computer. Here, you can check all Windows background processes and select any unwanted background processes and click End task button to terminate them temporarily.
To run a background process, we append “ &” at the end of the process we want to start: 3. How to Actually Stop a Process As we mentioned in the beginning, most of the time, we need the PID in conjunction with the kill command, but that is not the only way, as we’ll see. The ps command is used to list processes in Linux.
/Android will not kill the app or process when it is displaying on screen i.e active view. so switch to home screen and then kill the app or process you want/
public void KillApplication(String KillPackage)
{
ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(startMain);
am.killBackgroundProcesses(KillPackage);
Toast.makeText(getBaseContext(),"Process Killed : " + KillPackage ,Toast.LENGTH_LONG).show();
}
You may do the following:
First, add android:sharedUserId="android.uid.system"
inside the manifest
tag in AnfroidManifest.xml
, your manifest file should look like this now:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication"
android:sharedUserId="android.uid.system">
...
Second, claim FORCE_STOP_PACKAGES
permission. This step makes your manifest file look like:
...
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" />
...
Third, for packages you wanna stop, do:
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.forceStopPackage(PACKAGE_NAME);
Just replace PACKAGE_NAME
with your package name.
If you use Eclipse, just ignore any lint error in Preferences > Android > Lint Error Checking
.
You need to put the apk in the system directory to gain appropriate permission.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With