2014-03-31 87 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