0

I currently have two applications.

App A has a list of existing applications that you can click on and open. Including which is App B. Now I want to only run App B for a set amount of time ~30 minutes.

Is there a way to automatically close App B and reopen App A? I want to fully close App B. Kind of like when you press the soft button on your android and close out the app by swiping left/right.

I have tried using KillBAckgroundProcess and killProcess, but it does not fully simulate the "swiping left/right" of the app. I have looked at other issues on stack overflow, but none have the answer of how to do it this specific way.

Example Code:

    //process
        for (ActivityManager.RunningAppProcessInfo process : processes) {
            if(process.processName.equals("com.example.adam2392.helloworld")) {
                processToKill = process.processName;
                processId = process.pid;
            }
        }

        //packages
        for (ApplicationInfo packageInfo : packages) {
            if (packageInfo.packageName.equals("com.example.adam2392.helloworld")) {
                packageToKill = packageInfo.packageName;
            }
        }

           am.killBackgroundProcesses(packageToKill);
            android.os.Process.killProcess(processId);
ajl123
  • 1,172
  • 5
  • 17
  • 40

1 Answers1

0

I believe what would work better for your situation is to set an alarm in App B's onStart() or onCreate() method that goes off after 30 minutes and runs whatever code you need it to run to kill processes and then terminate App B. I've never done this myself, but this link has a nice tutorial about using Alarms in Android to schedule an app to do something. For your situation, you most likely will want to use

    AlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + 60 * 1000, PendingIntent) 

as the trigger for your alarm because there's probably no reason that App B has to begin its shutdown process at EXACTLY 30 min.

As far as closing the App, I believe this should work for you:

public void killApp() {
        Intent goHomeIntent = new Intent(Intent.ACTION_MAIN);
        goHomeIntent.addCategory(Intent.CATEGORY_HOME);
        goHomeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(goHomeIntent);
        finish();
}

This may not force quite the app like you're wanting, but usually you want to just let Android terminate the app at its own discretion after it has been closed.

Chamatake-san
  • 551
  • 3
  • 10