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!