Reputation: 422
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!
Upvotes: 7
Views: 16021
Reputation: 11060
Starting in Android 14, when your app calls killBackgroundProcesses()
, the API can kill only the background processes of your own app.
If you pass in the package name of another app, this method has no effect on that app's background processes, and the following message appears in Logcat:
Invalid packageName: com.example.anotherapp
Upvotes: 1
Reputation: 47
You must make your app have the system privilege,that is system app,or you can root your phone so that your app can kill other app as root user.
Upvotes: 0
Reputation: 438
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.
Upvotes: 2
Reputation: 96
/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();
}
Upvotes: 6