2011-08-29 18 views
0

我正在使用它在後臺通知用戶正在進行的服務。如何在服務完成時擺脫正在進行的通知?

nm = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
       CharSequence from = "App"; 
       CharSequence message = "Getting Latest Items..."; 
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); 
       notif = new Notification(R.drawable.icon, 
         "Getting Latest Items...", System.currentTimeMillis()); 
         notif.setLatestEventInfo(this, from, message, contentIntent); 
         notif.flags = Notification.FLAG_ONGOING_EVENT; 
         nm.notify(1, notif); 

現在,當服務完成後,我該如何得到這個消失?

回答

1

呼叫取消()上NotificationManager

There are different cancel methods :

public void cancel (int id) 
Since: API Level 1 

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar. 
public void cancel (String tag, int id) 
Since: API Level 5 

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar. 
public void cancelAll() 
Since: API Level 1 

Cancel all previously shown notifications. See cancel(int) for the detailed behavior. 
+0

如何讓通知被點擊時消失? – yoshi24

+0

@ yoshi24:請在這裏看看這個問題,它會回答你的上述問題的評論:http://stackoverflow.com/questions/3479428/android-notification-intent-to-clear-it-self – Viren

1

當你的服務停止時,它的onDestroy()應該被調用。你可以把Viren在那裏提到的cancel()。當服務完成時,應該清除你的通知。