2014-04-01 39 views
0

我有一個服務A,需要通知另一個服務B。如this question中所述,通過調用不應啓動服務的方法,我綁定服務B服務A如何區分運行服務和綁定服務?

serviceA.bindService(new Intent(serviceA, ServiceB.class), conn, 0); 

但是,當我想檢查服務是否正在運行下面的方法返回true,即使服務剛剛綁定。

public static boolean isServiceRunning(Context context) { 
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
     if (ServiceB.class.getName().equals(service.service.getClassName())) { 
      return true; 
     } 
    } 
    return false; 
} 

爲什麼isServiceRunning迴歸真實的服務,只是約束,並沒有運行(我檢查服務沒有被尋找到的設置正在運行的應用運行)?

+0

你能證實你的'ServiceB#onCreate'被調用嗎? – kiruwka

+0

'ServiceB#onCreate'沒有被調用。 – jellyfication

+0

好,比'ServiceB'沒有創建你想象的。我猜'isServiceRunning'方法代碼檢查它是錯誤的 – kiruwka

回答

0

當你綁定一個服務時,它由系統啓動,bindservice()實際上啓動服務。

綁定服務是Service類的一個實現,它允許其他應用程序綁定到它並與之交互。要爲服務提供綁定,您必須實現onBind()回調方法。此方法返回一個IBinder對象,該對象定義了客戶端可用於與服務交互的編程接口。

查看Android document瞭解更多信息。