Exemple #1
0
        /// <summary>
        /// 启动保活
        /// </summary>
        /// <param name="application">APP</param>
        /// <param name="runMode"></param>
        /// <param name="foregroundNotification">前台服务 必须要,安卓8.0后必须有前台通知才能正常启动Service</param>
        /// <param name="keepLiveService">保活业务</param>
        /// <param name="cactusBackgroundCallback">回调</param>
        public static void StartWork(Application?application, RunMode runMode, ForegroundNotification?foregroundNotification, IKeepLiveService?keepLiveService, ICactusBackgroundCallback cactusBackgroundCallback)
        {
            //是否主进程
            if (IsMain(application))
            {
                KeepLive.foregroundNotification = foregroundNotification;
                KeepLive.keepLiveService        = keepLiveService;
                KeepLive.runMode = runMode;
                KeepLive.BACKGROUND_CALLBACKS.Add(cactusBackgroundCallback);

                //Api大于21 JobScheduler 拉活
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    //启动定时器,在定时器中启动本地服务和守护进程
                    Intent intent = new Intent(application, typeof(JobHandlerService));

                    //Api大于29
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
                    {
                        //intent = new Intent(this, typeof(KeepAppLifeService));
                        //intent.PutExtra(KeepAppLifeService.EXTRA_NOTIFICATION_CONTENT, "DCMS正在后台运行....");
                        application.StartForegroundService(intent);
                    }
                    //Api大于26
                    else if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                    {
                        //注意26以下不支持
                        application.StartForegroundService(intent);
                    }
                    else
                    {
                        application.StartService(intent);
                    }

                    //Android.Util.AndroidRuntimeException: 'Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{ba40c5d u0 com.dcms.clientv3/.JobHandlerService}'
                }
                else
                {
                    // 通过前台 Service 提升应用权限
                    // 启动普通 Service , 但是在该 Service 的 onCreate 方法中执行了 startForeground
                    // 变成了前台 Service 服务

                    //启动本地服务
                    Intent localIntent = new Intent(application, typeof(LocalService));
                    application.StartService(localIntent);

                    //启动守护进程
                    //Intent guardIntent = new Intent(application, typeof(RemoteService));
                    //application.StartService(guardIntent);
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// 前后台切换回调,用于处理app前后台切换,非必传
 /// </summary>
 /// <param name="cactusBackgroundCallback"></param>
 public void AddBackgroundCallback(ICactusBackgroundCallback cactusBackgroundCallback)
 {
     BACKGROUND_CALLBACKS.Add(cactusBackgroundCallback);
 }