开发者

How to get rid of ongoing notification when a service is finished?

开发者 https://www.devze.com 2023-04-01 12:49 出处:网络
I am using this to notify the user of an ongoing service in the background. nm = (NotificationManager) this

I am using this to notify the user of an ongoing service in the background.

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

Now when the service is finished, how do i get this to dissapear?


Call cancel() on 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.


When your service stops, its onDestroy() should get called. You could put the cancel() that Viren mentioned there. That should wipe your notification when the service is finished doin its thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消