服务完成后如何摆脱持续通知?

时间:2011-08-29 04:34:55

标签: android

我正在使用此功能在后台通知用户正在进行的服务。

 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);

现在服务完成后,我该如何解决这个问题呢?

2 个答案:

答案 0 :(得分:1)

在NotificationManager上调用cancel()

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.

答案 1 :(得分:1)

当您的服务停止时,应调用其onDestroy()。你可以把Viren提到的cancel()放在那里。这应该在服务完成后擦除你的通知。