0

I am trying to calculate the time taken by the app(Child app) to load, hit a query and populate the screen. For this purpose i have created another simple app(Parent app) with just on button. On click of this button the Child app is launched for some number of iterations. I am able to invoke the child app from parent app, but the parent app doesn't wait for child app to finish execution.

Here is the onClick code snippet in the Parent app:

public void onClick(View v) {
    Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage("com.example.android.packagename");
    //null pointer check in case package name was not found.
    if (launchIntent != null) {
        try {
            long launchStartTime = System.currentTimeMillis();
            launchIntent.putExtra("LaunchStartTime", launchStartTime);
            System.out.println("Launching child app");
            /*Call child app 10 times. */
            for (int i = 0; i <= 10; i++) {
                startActivity(launchIntent); 
                /*Parent app doesn't waits for child app to finish its execution, and below statement is executed.*/
                System.out.println("This will be printed and child app is launched only once.");
            }
        } catch (ActivityNotFoundException err) {
            Toast t = Toast.makeText(getApplicationContext(),"App not found", Toast.LENGTH_SHORT);
            t.show();
        }
    }
}

The solution could be to use multi-threading, but i have no experience in that area. Could any one provide a way to make parent app wait for child app to finish its execution and possibly iterate for desired number of times.

EDIT:

Now I am trying to invoke the app just once on child thread. I tried to spawn a child thread from main thread and invoke the child app on child thread,

but I am getting below error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

This is the new onClick code snippet:

public void onClick(View v) {
    MyRunnable runnable = new MyRunnable();
    Thread thread = new Thread(runnable);
    thread.start();
}

And here is the MyRunnable class that implements runnable interface.

public class MyRunnable extends AppCompatActivity implements Runnable  {
@Override
public void run() {
    Intent launchIntent = ApplicationContextProvider.getContext().getPackageManager().getLaunchIntentForPackage("com.example.android.packagename");
    if (launchIntent != null) {
        try {
             startActivity(launchIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 2
    you could try AsyncTask, in doInBackground() you can perform the task and in postExecute you can call another app. – Kiran Malvi Nov 15 '18 at 05:08
  • @kiranMalvi's solution would work. You can reference https://stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (: – cokeby190 Nov 15 '18 at 05:45
  • What about "startActivityForResults()"? https://developer.android.com/training/basics/intents/result – emandt Nov 15 '18 at 07:30

0 Answers0