Q:

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

I would like to close(kill) another app in my application by package name. I am trying something like this, but this code don't close any app. what am i doing wrong?
   public void amKillProcess(string package_name)
{
    ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService);
    IList<RunningAppProcessInfo> runningProcesses = am.RunningAppProcesses;

    foreach (RunningAppProcessInfo runningProcess in runningProcesses)
    {
        if (runningProcess.ProcessName.Contains(package_name))
        {
            Android.OS.Process.SendSignal(runningProcess.Pid, Signal.Kill);
            am.KillBackgroundProcesses(runningProcess.ProcessName);
        }
    }
}

P.S I added android.permission.KILL_BACKGROUND_PROCESSES and by this code i can  close only my own app

A:

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)
            {

            }
    }

}

