我想在同一時間開始服務和活動。我正在使用下面的代碼,它是正確的?Android - 如何在同一時間啓動服務和活動?
startService(new Intent(this, Service.class));
Intent intent = new Intent(this, Activity.class);
startActivity(intent);
但是,這裏服務後,complted然後只有活動開始。爲什麼? 請幫助任何人。
我想在同一時間開始服務和活動。我正在使用下面的代碼,它是正確的?Android - 如何在同一時間啓動服務和活動?
startService(new Intent(this, Service.class));
Intent intent = new Intent(this, Activity.class);
startActivity(intent);
但是,這裏服務後,complted然後只有活動開始。爲什麼? 請幫助任何人。
您可以嘗試啓動幾個線程並使用barier標誌使用信號量。
你可以把一些示例代碼。 – Baskar 2011-12-30 09:34:54
很基本的東西。閱讀有關在java中的線程 – 2011-12-30 09:40:22
我已經嘗試了下面的代碼也不工作。 new Thread(){public void run(){try {startService(new Intent(this,Service.class)); } catch(Exception e){}}} .start(); Intent intent = new Intent(this,Activity.class); startActivity(意向); – Baskar 2011-12-30 09:45:48
無法同時啓動在同一個進程(這是默認行爲)中運行的活動和服務。
服務的onCreate()方法在主(UI)線程上運行。
Activity的onCreate()方法也在主(UI)線程上運行。
讓兩者都在同一時間啓動的唯一方法是讓服務在單獨的進程中運行。
是的。因爲startActivity方法是在startService()方法之後執行的。 – 2011-12-30 09:27:04
那麼如何在同一時間調用服務和活動? – Baskar 2011-12-30 09:36:14
startService的返回值是多少?這會告訴你服務是否已經開始。 http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29 – 2011-12-30 10:29:07