7

I have been developing an app, and I need to close another app in my code. Does anyone know any api to call to close an app?

BTW: my app will be pre-installed.

thanks

Jimmy
  • 573
  • 4
  • 9
  • 14

5 Answers5

8

Since Android 2.2 (i.e. going forward), you can only close the background processes of other apps, you are no longer able to close their main activities.

If your app is targeting Android <2.2, look atandroid.permission.RESTART_PACKAGE.

If you want it to work properly on 2.2 and above (which you should :-)), look at android.permission.KILL_BACKGROUND_PROCESSES, but again, this only closes background services and such and might "mess up" the other app rather than doing any good.

With the right permissions, you can then do the following:

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Nick
  • 3,504
  • 2
  • 39
  • 78
  • am.restartPackage() has been deprecated, now they use another api "killBackgroundProcesses()". But thanks, really appreciate it. – Jimmy Sep 23 '10 at 00:47
5

Try This

    ActivityManager am = (ActivityManager) getApplicationContext().getSystemService("activity");
    Method forceStopPackage;                
    forceStopPackage =am.getClass().getDeclaredMethod("forceStopPackage",String.class);
    forceStopPackage.setAccessible(true);
    forceStopPackage.invoke(am, pkg);

In manifest file add this

<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
Ahmed
  • 814
  • 2
  • 19
  • 38
0

If both applications are yours, you can use AIDL for inter-process communication to send a message telling the other application to close. See http://developer.android.com/guide/developing/tools/aidl.html.

poe
  • 147
  • 1
  • 1
  • 8
0

I have been able to close another app on Android 12 successfully. Here is how:

Basically, I am closing another app from a service although you should be able to do it from an app too.

  1. My service is a privileged system app that gets installed in system/priv-app/ (It has LOCAL_PRIVILEGED_MODULE := true in its Android.mk)

  2. I added <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" /> in AndroidManifest.xml

  3. I added in privapp-permissions.xml

    <privapp-permissions package="<my service package name>">
        <permission name="android.permission.FORCE_STOP_PACKAGES"/>
    </privapp-permissions>
  1. I called in my service this method with the package name of the application I want to close:
    private void closePackageApp(String namePackage) {
        ActivityManager activityManager = (ActivityManager)
                context.getSystemService(Context.ACTIVITY_SERVICE);
        try {
            Method forceStopPackage = activityManager.getClass().
                    getDeclaredMethod("forceStopPackage", String.class);
            forceStopPackage.setAccessible(true);
            forceStopPackage.invoke(activityManager, namePackage);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

I tested this and in the logs, I can see the app is being closed. However the app is not removed from the recent screen (logs suggested the app was disposed without first being removed with the input manager!).

However, I am sure the app was really being closed when it was in the background by comparing its lifecycle on opening again. Normally, it is onPause->onResume but now it is onPause->onCreate.

zeitgeist
  • 852
  • 12
  • 19
-1

You don't ever really want to close another application, due to Android activity lifecycle.
There's no benefit, and always detriment to closing another app if it's not yours, and very little benefit to closing your own.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

If you know for certain that you'll never, ever need a root activity and its children (an "app"), you can stop it to free memory (it doesn't free that much), but if you do the user may restart it while it's still in cache, stopped, which can cause problems if the stopped state is restored. So this is a bad practice.

  • 1
    Also, pre-installed is the worst idea possible. Please tell your bosses that forcing software on a user is evil. Do you by chance work for Verizon? – Android Dev Dude Sep 23 '10 at 18:12
  • 2
    I certainly have a valid use case to force close a rogue Android app. Dialer app on my device is acting up due to the bugs in its implementation. Specifically, it is draining the battery and there will be no updates ever, first because its Android, and second because my phone is old. I have found that force closing it after events like calls do not negatively impact the usage of my phone but it does save the battery, therefore I would like to have a service which monitors and force closes the dialer instead of me doing it manually. – paulius_l Feb 09 '15 at 08:31
  • @AndroidDevDude Ever thought about business using Android devices for their own use and developing apps for their own devices? – ETL Mar 03 '22 at 06:05