0

I'm trying to write a UI Automation test and I'm trying to speed it up. At one point of the test, I need to close all recent apps on a device. I can do this via UiAutomator (currently doing it that way) but this isnt the part of the test I'm focusing on so I was wondering if this is doable via an intent call?

Based on the file structure, I would prefer not to add new classes, change activity code (b/c only needed for testing), or change permissions too badly.

I want to doing something along the line of:

Intent closeIntent = new Intent(context, null);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(closeIntent);

Android isnt okay with the null class parameter.

I've looked at the following but none of them are what I really want:

Finish all activities at a time

Intent data not clearing when user presses back button then recent apps button

https://gist.github.com/anonymous/9884978

Thanks in advance!

Community
  • 1
  • 1
Keith
  • 637
  • 1
  • 8
  • 23

1 Answers1

1
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < tasks.size(); i++) {
        Log.d("Running task", "Running task: " + tasks.get(i).baseActivity.toShortString() + "\t\t ID: " + tasks.get(i).id);
        String packageName = processus.processName.split(":")[0];
        if (!getPackageName().equals(packageName) && !tasks.contains(packageName)) {
            am.killBackgroundProcesses(packageName);
        }
    }

Hope this will help you out.....

Amaresh Jana
  • 732
  • 11
  • 22