private void TryStartForeground(Intent intent = null) { try { string content = "DCMS正在后台运行...."; if (intent != null) { content = intent.GetStringExtra(EXTRA_NOTIFICATION_CONTENT); } //创建Notification notificationUtil = new NotificationUtil(MainActivity.Instance, Resource.Mipmap.ic_launcher, "DCMS", content, CHANNEL_ID, CHANNEL_NAME); //将服务放置前台 //startForeground() 中的id 和notification 不能为0 和 null StartForeground(NotificationUtil.NOTIFICATION_ID, notificationUtil.GetNotification()); } catch (Exception ex) { Crashes.TrackError(ex); } }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { if (intent == null) { return(StartCommandResult.NotSticky); } string content = "DCMS正在运行打印...."; if (intent != null) { content = intent.GetStringExtra(EXTRA_NOTIFICATION_CONTENT); } //创建Notification notificationUtil = new NotificationUtil(MainActivity.Instance, Resource.Mipmap.ic_launcher, "DCMS", content, CHANNEL_ID, CHANNEL_NAME); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { //将服务放置前台 //startForeground() 中的id 和notification 不能为0 和 null StartForeground(NotificationUtil.NOTIFICATION_ID, notificationUtil.GetNotification()); } return(base.OnStartCommand(intent, flags, startId)); }
public override void OnDestroy() { //取消通知 if (notificationUtil != null) { notificationUtil.CancelNotification(); notificationUtil = null; } base.OnDestroy(); }
/// <summary> /// 销毁时 /// </summary> public override void OnDestroy() { try { IsRun = false; //取消通知 if (notificationUtil != null) { notificationUtil.CancelNotification(); notificationUtil = null; } //退出服务 StopForeground(true); //停止播放 if (_mediaPlayer != null) { _mediaPlayer.Stop(); } //停止服务 this.StopSelf(); //释放电源锁 ReleaseWakeLock(); } catch (Exception ex) { Crashes.TrackError(ex); } finally { base.OnDestroy(); } }