-1

I am trying to close particular 3 application if already opened and running in the background apps. I have tried like this

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CloseApps();
}

private void CloseApps() {
    ActivityManager actvityManager = (ActivityManager)
            this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

    for (ActivityManager.RunningAppProcessInfo runningProInfo : procInfos) {
        if (runningProInfo.processName.equals("com.aaa.aaaa")) {
            actvityManager.killBackgroundProcesses(runningProInfo.processName);
        }else if (runningProInfo.processName.equals("com.ccc.ccc")) {

            actvityManager.killBackgroundProcesses(runningProInfo.processName);
            }
        else if (runningProInfo.processName.equals("com.bbb.bbb.ad")){

            actvityManager.killBackgroundProcesses(runningProInfo.processName);
        }
    }
}

Note: I'm working on android 7.1 version.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
GNK
  • 1,036
  • 2
  • 10
  • 29
  • Tell us what issues you have with your current code. – Okas Jan 16 '18 at 09:58
  • already opened applications are not closed. – GNK Jan 16 '18 at 10:01
  • Possible duplicate of [killBackgroundProcesses no working](https://stackoverflow.com/questions/19604097/killbackgroundprocesses-no-working) – Okas Jan 16 '18 at 10:03
  • problem is not resolved in that link also. Before asking question i have seen what you shared link. – GNK Jan 17 '18 at 06:46
  • Finally I got solution.May I know why people are down voting,I could not able to comment already asked questions. Thats what only i have asked new question. – GNK Jan 17 '18 at 06:53
  • Good to hear you got solution. Please add this as an answer (you can answer your own questions) and ill remove my downvote. – Okas Jan 17 '18 at 11:29
  • I have placed my answer please find.can you please give up vote for my answer. – GNK Jan 18 '18 at 05:25

1 Answers1

1

I got solution for this question

private void CloseApps() {
    Process suProcess = null;
    try {
        suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream dataOutputStream = new DataOutputStream(suProcess.getOutputStream());
        dataOutputStream.writeBytes("am force-stop com.aaaa.aaaa" + "\n");

 } catch (IOException e) {
        e.printStackTrace();
    }.
GNK
  • 1,036
  • 2
  • 10
  • 29