我做了一個遠程服務,這個服務是在我第一次啓動時通過我的活動啓動的,之後,如果服務啓動,活動總是會看起來避免再次啓動它。服務在onTaskRemoved後重新創建
該服務在onCreate函數中運行一些方法。此服務始終運行,並在啓動時啓動。
問題(不是一個大問題,但我想知道爲什麼)是,如果我停止我的活動創建服務,onTaskRemoved被調用,這是正確的,但幾秒鐘後,再次調用oncreate方法並且服務再次開始。
任何想法爲什麼?我怎樣才能控制這個?
<service
android:name=".Service"
android:icon="@drawable/ic_launcher"
android:label="@string/service_name"
android:process=":update_process" >
</service>
AndroidManifest.xml中
if (!isRunning()) {
Intent service = new Intent(this, UpdateService.class);
startService(service);
} else {
//Just to debug, comment it later
Toast.makeText(this, "Service was running", Toast.LENGTH_SHORT).show();
}
當如果它沒有運行
我不清楚你的問題。但不要擔心服務的多個實例。從第二次調用['startService()'](http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent))不會啓動服務兩次,如果它已經在運行。 – 2013-02-08 11:55:47
@LaiVung多重實例不是問題,問題在於活動停止後調用oncreate的方法,所以onCreate內部的方法再次被調用,我不想要。 – Marcel 2013-02-08 12:02:53
你是否在你的活動結束時開始了一些事情(比如'onDestroy()')? – 2013-02-08 12:20:12