在我的代碼,我有條件地開始我的服務是這樣的:我如何才能知道我的服務已啓動
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
u能請告訴我,如果有可能,以找出是否我已經開始在同一之前的服務,以便我不開始我的服務TWICE?
謝謝。
在我的代碼,我有條件地開始我的服務是這樣的:我如何才能知道我的服務已啓動
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
u能請告訴我,如果有可能,以找出是否我已經開始在同一之前的服務,以便我不開始我的服務TWICE?
謝謝。
檢查:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;}
@bigstones:我不知道怎麼樣,但是我有一個可以創造多少次,只要你喜歡(也許是因爲我是各種可運行的對象中創建一個服務)。
當我使用上面的代碼時,它只顯示所有系統服務而不是我創建的服務 – user1810931 2013-03-13 18:40:11
系統在首次創建服務時調用onCreate()方法,以執行一次性設置過程(在調用onStartCommand()或onBind()之前)。如果該服務已在運行,則不調用此方法。這意味着我們可以通過布爾邏輯來了解服務是否已啓動 – sssvrock 2016-07-21 10:44:34