locked
How can I close another application that is running in the background? RRS feed

  • Question

  • User362434 posted

    Hi. How can I close another application that is running in the background? like for example the applications of music. Greetings.

    Monday, June 24, 2019 2:59 AM

Answers

  • User382871 posted

    Try to kill a process using ActivityManager.killBackgroundProcesses method.

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

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

    } ```

    Don't forget to add android.permission.KILL_BACKGROUND_PROCESSES permission.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 24, 2019 7:59 AM
  • User382871 posted

    Sorry for mymistake, call KillBackgroundProcesses method. The system will immediately kill all background processes associated with the given package. And change 'Equals' to 'Contains'. ``` public void amKillProcess(string package_name) { ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService); IList 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);
        }
    }
    

    } ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 25, 2019 7:25 AM

All replies

  • User382871 posted

    Try to kill a process using ActivityManager.killBackgroundProcesses method.

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

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

    } ```

    Don't forget to add android.permission.KILL_BACKGROUND_PROCESSES permission.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 24, 2019 7:59 AM
  • User362434 posted

    I'm sorry but that does not work for me. The application is still working and I do not know why. This continues to work in the background even with Kill's permits.

    Tuesday, June 25, 2019 4:44 AM
  • User382871 posted

    Sorry for mymistake, call KillBackgroundProcesses method. The system will immediately kill all background processes associated with the given package. And change 'Equals' to 'Contains'. ``` public void amKillProcess(string package_name) { ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService); IList 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);
        }
    }
    

    } ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 25, 2019 7:25 AM
  • User362434 posted

    Hi. Well, check that if I select the same application if it ends, but if I choose another one, it does not: / :( I do not know if it is with another application it can not ... You know a possible solution, really thank you very much for your help. Greetings.

    Wednesday, June 26, 2019 1:20 AM