7

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!

user690421
  • 422
  • 1
  • 5
  • 15
  • any luck? you might also want to check this... http://stackoverflow.com/questions/7195167/automate-closing-of-applications-in-android – Behnam Dec 01 '13 at 08:18
  • @Campiador No luck. It seems that nowadays you simply can't do things like that on android system. – user690421 Feb 26 '14 at 23:59

3 Answers3

6

/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();       
}
user3511417
  • 96
  • 1
  • 4
2

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.

Tomato
  • 438
  • 2
  • 7
  • 1
    you will have to sign your apk as system app -> http://stackoverflow.com/questions/3635101/how-to-sign-android-app-with-system-signature – Aniket Thakur Apr 10 '15 at 05:37
0

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.

Alan.cao
  • 47
  • 9