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.