How to Close Another App in Android

Close another app from your app?

It is not possible to close the another app from your app.

It is ethically not correct and there is no official Android API as well.

How to close application from another application in android

This is how i solve this issue. For sending intent:

        Intent intent = new Intent("com.hp.walk.CLOSE_APPLICATION");
getApplication().sendBroadcast(intent);

in another application this is how i register receiver to get intent:

@Override
public void onCreate(Bundle savedInstanceState) {

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.hp.walk.CLOSE_APPLICATION");
registerReceiver(receiver, intentFilter);

private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.hp.walk.CLOSE_APPLICATION".equals(intent.getAction())) {
Log.i(TAG, "received.. ");
finish();
}
}
};

Close(kill) another app in my application by package name

If you want to kill a background app , check the following code

 public void amKillProcess(string package_name)
{
ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService);
var runningProcesses = am.RunningAppProcesses;

foreach (RunningAppProcessInfo runningProcess in runningProcesses)
{
if (runningProcess.ProcessName.Contains(package_name))
{

Android.OS.Process.KillProcess(runningProcess.Uid);
}
}
}

And if you want to kill a foreground app, you could use Adb

public class SuUtil
{
private static Java.Lang.Process process;

public static void kill(string packageName)
{
initProcess();
killProcess(packageName);
close();
}

private static void initProcess()
{
if (process == null)
try
{
process = Runtime.GetRuntime().Exec("su");
}
catch (IOException e)
{

}
}

private static void killProcess(string packageName)
{
System.IO.Stream output = process.OutputStream;
Java.Lang.String cmd = new Java.Lang.String("am force-stop " + packageName + " \n");
try
{
output.Write(cmd.GetBytes());
output.Flush();
}
catch (IOException e)
{

}
}

private static void close()
{
if (process != null)
try
{
process.OutputStream.Close();
process = null;
}
catch (IOException e)
{

}
}

}


Related Topics



Leave a reply



Submit