Android - How to start service and activity on same time?

给你一囗甜甜゛ 提交于 2019-12-11 10:17:28

问题


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.



来源:https://stackoverflow.com/questions/8677956/android-how-to-start-service-and-activity-on-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!