Android - How to start service and activity on sam

2019-08-01 03:16发布

问题:

I want to start service and activity on same time. I am using the following code, It is correct?

startService(new Intent(this, Service.class));
Intent intent = new Intent(this, Activity.class);
startActivity(intent);

But here after service is complted then only Activity starting. Why? Please help anyone.

回答1:

You can try to launch few threads and use semaphore with barier flag.



回答2:

There is no way to start an Activity and a Service that run in the same process (which is the default behaviour) at the same time.

The onCreate() method of the Service runs on the main (UI) thread.

The onCreate() method of the Activity also runs on the main (UI) thread.

The only way to have both start at more-or-less the same time is to have the Service run in a separate process.