Service 在android中,当用户点击状态栏时,如何从状态栏中删除通知?

Service 在android中,当用户点击状态栏时,如何从状态栏中删除通知?,service,notifications,Service,Notifications,在我的例子中,我使用服务实现了通知,并开始了如下通知 notificationManager.notify(MY_NOTIFICATION_ID, myNotification); startForeground(MY_NOTIFICATION_ID, myNotification); 国旗呢 myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 在我的情况下不起作用,但如果我删除 startForeground

在我的例子中,我使用服务实现了通知,并开始了如下通知

 notificationManager.notify(MY_NOTIFICATION_ID, myNotification);  
 startForeground(MY_NOTIFICATION_ID, myNotification);
国旗呢

 myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
在我的情况下不起作用,但如果我删除

       startForeground(MY_NOTIFICATION_ID, myNotification);

它的工作原理很好,所以当用户点击通知时,通知会自动取消,因为标志起作用,但在这种情况下,若用户需要,我无法停止服务

经过一些研究,我终于找到了解决办法。我做错的是,我使用了startForeground(我的通知ID,我的通知);虽然我没有更新用户界面。我从希望停止服务的活动调用Context.stopservice(stopServiceIntent)。和服务的onDestroy部分,我取消计时器,如果不为null,则使计时器为null,并通过调用stopSelf()停止服务