Пример #1
0
        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));
        }
Пример #2
0
        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);
            }
        }