Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close another app in Android?

Tags:

java

android

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

like image 706
Jimmy Avatar asked Sep 22 '10 20:09

Jimmy


2 Answers

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");
like image 50
Nick Avatar answered Sep 18 '22 00:09

Nick


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>
like image 27
Ahmed Avatar answered Sep 18 '22 00:09

Ahmed