2012-02-15 17 views
2

我有一個在啓動時創建通知的服務。刪除正在進行的服務通知

然後ondestroy()我希望它被刪除。

我只是使用.cancel(NOTIFICATION_ID);

它工作得很好,當它是一個正常的通知,但是當我使用正在進行的事件,它只是不會取消它。

我讀過一些關於該服務不停止,如果android有資源,但如何克服這一點?

我使用此代碼來啓動服務

final Intent bg_service = new Intent(BackgroundService.class.getName()); 

    btn_start = (Button)findViewById(R.id.btn_start); 
    btn_stop = (Button)findViewById(R.id.btn_stop); 

    btn_start.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startService(bg_service); 
     } 
    }); 

    btn_stop.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      stopService(bg_service); 
     } 
    }); 

我用這個在創建

  notificationManager = (NotificationManager) 
      getSystemService(NOTIFICATION_SERVICE); 

    Notification notification = new Notification(R.drawable.ic_launcher, 
      "A new notification", System.currentTimeMillis()); 

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

    Intent intent = new Intent(this, mainapp.class); 
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 
    notification.setLatestEventInfo(this, "...", 
      "...", activity); 
    notificationManager.notify(19314, notification); 

這在破壞

  noficationManager.cancel(19314); 
+0

你能告訴一些代碼,你開始你的'Service'? – 2012-02-15 22:01:09

+0

我在帖子中添加了它。 – NikolajSvendsen 2012-02-15 22:06:53

+0

你如何添加正在進行的通知? – 2012-02-15 22:10:14

回答