0

I am writing my first android app so I will try my best to be clear and precise. I can see some similar questions to this but none that seems to answer it exactly so perhaps it can't be done but here goes:

I have got my main activity to the point where it has data stored which includes a list of apps on the device that the user has selected to launch. On clicking a button on the main activity screen I would like the device to launch each of these selected apps in turn and then (ideally) return the user to the main activity that I have written. I would also like to define some restrictions on running each app - for example, each app runs for 30 seconds or until the app stops using the internet whichever comes earliest.

I don't believe I have any issue with linking all of this to the button click, nor is there any issue cycling through all of the selected apps. What I really need is the code to launch each app and then recall from it/move to the next app (ideally after the 30 seconds or when the app stops using the internet). Hopefully the below code makes clear where I am looking for help with the TODO. Does anyone know whether this is possible and if it is how can I get it done?

public class MainActivity extends Activity {
... //some code up here

//when the Run Apps button is clicked the onClick will run all of the selected apps using this Method:
public void RunApps(View view) {

    //run through the list of Apps and run the ones that are selected
    for (App application : list) {
        if (application.isSelected()) {

               /* TODO code that is meant to run the selected app and return to the main 
                * activity after say 30 seconds or when the app is done using the internet.
                * As a starter I have the below but this is crashing and even if it did run
                * I believe that it would not return me to the original main activity:
                */
                Intent i = new Intent(Intent.ACTION_MAIN);
                PackageManager manager = getPackageManager();
                i = manager.getLaunchIntentForPackage(application.getPackageName());
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                startActivity(i); 
        }
    }
};

...//some more code here
}

just a couple of notes - the App class is defined elsewhere and includes the package name and whether the app has been selected by the user. list is a List of Apps.

I believe that the ACTION_MAIN and CATEGORY_LAUNCHER values may not be the best to use and perhaps startActivity(i) is not the right method for what I want but am not sure how that needs to be changed or whether there a more fundamental changes needed.

Many thanks for any help.

1 Answers1

0

You should run each app from your top-level MainActivity sequentially by invoking them one-at-a-time.

Here's how:

  1. Keep a counter in your MainActivity to indicate which app you are currently invoking.
  2. Use startActivityForResult() instead of startActivity() to start your applications. This will cause execution to return to MainActivity.onActivityResult() when each of the apps is finished.
  3. The requestCode of startActivityForResult() will be returned to onActivityResult(), so you will know which application completed. Therefore, MainActivity can increment the counter and start the next application in onActivityResult().

Restriction:

  • One of your requirements is to return to MainActivity after each app completes. These steps satisfy that requirement.
  • Another requirement is to return to `MainActivity after all of the apps are finished. These steps also satisfy that requirement. You will know when you have finished the final app because of the value of your counter.
  • The final requirement is to limit the duration of each app to 30 seconds. This is a more difficult problem. You will use a Timer in your MainActivity as a watchdog to monitor the spawned apps. Use methods described here to stop the spawned app when time runs out: Finish an activity from another activity.
    Warning: get everything else working first, before you try to externally stop an app.

That's all. Good luck!

Community
  • 1
  • 1
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • Thank you for this. I have tried to implement David's suggestion but it is not quite working. e.g. if the user selects some apps to launch (say Browser is one of them and is the first one in the list) then when you start the RunApps method the Browser opens and starts loading up from the web (which is what I wanted). However, it never returns to the original app. I must manually return to the original app through the UI. Then it launches another app from the list selected but again it wont return back to my app. Any ideas? Can I auto return from the Browser and other third party apps? – Soxford Apr 07 '13 at 17:51
  • I believe that the Browser will return to your activity once the user presses 'Back'. I believe that you can keep the Browser out of the back stack so that it won't remain when the user eventually exits your activity with this: `intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);`. And, finally, may developers including myself create a custom Activity with one big `WebView` and use that for navigation to websites instead of the native Android Browser app. Perhaps you should use this mechanism when your user selects navigation to a URL. – David Manpearl Apr 07 '13 at 20:59