Java Android-如何关闭startForeground启动的服务中的通知?
假设我在应用程序的服务中通过以下方式启动通知:Java Android-如何关闭startForeground启动的服务中的通知?,java,android,Java,Android,假设我在应用程序的服务中通过以下方式启动通知: notification = new Notification(R.drawable.icon, getText(R.string.app_name), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, mainActivity.class); PendingIntent pendingIntent = PendingInten
notification = new Notification(R.drawable.icon, getText(R.string.app_name), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, mainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
initService.notification.setLatestEventInfo(this, getText(R.string.app_name), "ibike rider", pendingIntent);
startForeground(ONGOING_NOTIFICATION, notification);
我将如何关闭/取消此服务?在另一项活动中是否有可能做到这一点
谢谢。任何“已启动”服务(无论是后台还是前台服务)都可以通过startService
呼叫启动,并通过stopService
呼叫停止
onStartCommand
实现中处理多个请求,那么您可以通过额外调用startService
来管理其生命周期:
public int onStartCommand(Intent intent, int flags, int startId) {
// ...
if (intent.getAction().equals(STOP_MY_SERVICE)) {
stopSelf();
return START_STICKY;
}
if (intent.getAction().equals(START_FOREGROUND)) {
// startForeground(...);
return START_STICKY;
}
if (intent.getAction().equals(STOP_FOREGROUND)) {
stopForeground(true);
return START_STICKY;
}
// ...
return START_STICKY;
}
有关示例,请参见