Kill Another Application on 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.

Kill another application on Android?

You can only kill a process that has the same userID as the one that is doing the killing. If you are trying to kill your own process it should work. Otherwise you can't do it (unless you have a rooted device and your application has root priviledges).

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