launch another android app on a new thread










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();













share|improve this question



















  • 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 5:08











  • @kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

    – cokeby190
    Nov 15 '18 at 5:45











  • What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

    – emandt
    Nov 15 '18 at 7:30















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();













share|improve this question



















  • 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 5:08











  • @kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

    – cokeby190
    Nov 15 '18 at 5:45











  • What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

    – emandt
    Nov 15 '18 at 7:30













0












0








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();













share|improve this question
















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();










java android multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:10









shizhen

3,68741234




3,68741234










asked Nov 15 '18 at 5:06









adam_appleadam_apple

34




34







  • 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 5:08











  • @kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

    – cokeby190
    Nov 15 '18 at 5:45











  • What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

    – emandt
    Nov 15 '18 at 7:30












  • 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 5:08











  • @kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

    – cokeby190
    Nov 15 '18 at 5:45











  • What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

    – emandt
    Nov 15 '18 at 7:30







2




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 5:08





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 5:08













@kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

– cokeby190
Nov 15 '18 at 5:45





@kiranMalvi's solution would work. You can reference stackoverflow.com/a/9671602/1275764 to see how the AsyncTask is implemented. (:

– cokeby190
Nov 15 '18 at 5:45













What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

– emandt
Nov 15 '18 at 7:30





What about "startActivityForResults()"? developer.android.com/training/basics/intents/result

– emandt
Nov 15 '18 at 7:30












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312766%2flaunch-another-android-app-on-a-new-thread%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312766%2flaunch-another-android-app-on-a-new-thread%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3