2014-03-31 134 views
0

我在製作鬧鐘應用程序。我使用intent開始一項服務。停止同一服務的多個實例的服務

public void Start(View view) { 
    hr = timePick.getCurrentHour(); 
    min = timePick.getCurrentMinute(); 
    service = new Intent(this,MyService.class); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("HOUR", hr); 
    bundle.putInt("MIN", min); 
    service.putExtras(bundle); 
    startService(service); 

} 

我通過

public void Stop(View view) { 
    hr = timePick.getCurrentHour(); 
    min = timePick.getCurrentMinute(); 
    Intent myAlarm = new Intent(this, MyAlarm.class); 
    Intent myService = new Intent(this, MyService.class); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("HOUR", hr); 
    bundle.putInt("MIN", min); 
    myService.putExtras(bundle); 
    myAlarm.putExtras(bundle); 
    stopService(myAlarm); 
    stopService(myService); 
} 

停止服務,我可以有服務運行的多個實例。但是,當我嘗試通過傳遞小時和分鐘來停止服務的單個實例時,服務的所有實例都將停止。我在服務MyService中使用了alarmmanager。 MyAlarm創建通知。

例如。我已經設定了11.30和11.40的警報。我希望刪除11.40的鬧鐘。所以我按下停止按鈕。然而無論是報警被取消

回答

0

這是因爲作爲運行同一服務的多個實例沒有這樣的事,呼籲startService(intent)不啓動它只是調用onStartCommand再次

從文檔

的服務的另一實例

Multiple requests to start the service result in multiple corresponding calls to the service's onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.

http://developer.android.com/guide/components/services.html