コード例 #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mjc5NDMzQDMxMzgyZTMxMmUzMER2bTdGc1JRSWNqTHdHRjJCcDYraEFiMSs5WnNoUStHM2I4Q1hSdlBOYTg9");

            Xamarin.Forms.DependencyService.Register <ToastMessage>();
            LoadApplication(new App());
            Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
            Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            Window.SetStatusBarColor(Android.Graphics.Color.Rgb(99, 110, 114));
            var intent = new Intent(this, typeof(BackGroundService));

            CrossCurrentActivity.Current.Init(this, savedInstanceState);
            ContextCompat.StartForegroundService(this, intent);
            LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.icon;
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessBackgroundLocation) != (int)Permission.Granted)
            {
                RequestPermissions(new string[] { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation, Manifest.Permission.LocationHardware, Manifest.Permission.AccessBackgroundLocation }, 0);
            }
        }
コード例 #2
0
        public static void StartServiceCompat <T>(this Context context, Bundle args = null) where T : Service
        {
            var intent = new Intent(context, typeof(T));

            if (args != null)
            {
                intent.PutExtras(args);
            }
            NotificationChannel serviceChannel;
            var manager = context.GetSystemService(Context.NotificationService) as NotificationManager;

            serviceChannel = manager.GetNotificationChannel(CHANNEL_ID);
            if (serviceChannel == null)
            {
                serviceChannel = new NotificationChannel(CHANNEL_ID, "Example Service Channel", NotificationImportance.None);
                serviceChannel.EnableVibration(true);
                serviceChannel.EnableLights(true);
                serviceChannel.SetShowBadge(true);
                serviceChannel.LockscreenVisibility = NotificationVisibility.Public;
                manager.CreateNotificationChannel(serviceChannel);
            }
            serviceChannel?.Dispose();

            ContextCompat.StartForegroundService(context, intent);
        }
        /// <Docs>The Context in which the receiver is running.</Docs>
        /// <summary>
        /// When we receive the action media button intent
        /// parse the key event and tell our service what to do.
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="intent">Intent.</param>
        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action != Intent.ActionMediaButton)
            {
                return;
            }

            //The event will fire twice, up and down.
            // we only want to handle the down event though.
            var key = (KeyEvent)intent.GetParcelableExtra(Intent.ExtraKeyEvent);

            if (key.Action != KeyEventActions.Down)
            {
                return;
            }

            var action = MediaServiceBase.ActionPlay;

            switch (key.KeyCode)
            {
            case Keycode.Headsethook:
            case Keycode.MediaPlayPause:
                action = MediaServiceBase.ActionTogglePlayback;
                break;

            case Keycode.MediaPlay:
                action = MediaServiceBase.ActionPlay;
                break;

            case Keycode.MediaPause:
                action = MediaServiceBase.ActionPause;
                break;

            case Keycode.MediaStop:
                action = MediaServiceBase.ActionStop;
                break;

            case Keycode.MediaNext:
                action = MediaServiceBase.ActionNext;
                break;

            case Keycode.MediaPrevious:
                action = MediaServiceBase.ActionPrevious;
                break;

            default:
                return;
            }

            var remoteIntent = new Intent(action);

            ContextCompat.StartForegroundService(context, remoteIntent);
        }
コード例 #4
0
 public override void OnReceive(Context context, Intent intent)
 {
     App.CheckInit();
     if (AppConfig.Current.Autostart)
     {
         Logging.info("Autostart...");
         Intent serviceIntent = new Intent(context, typeof(BgService));
         serviceIntent.SetAction(BgService.Actions.START);
         serviceIntent.PutExtra("isAutostart", true);
         ContextCompat.StartForegroundService(Application.Context, serviceIntent);
     }
 }
コード例 #5
0
        private void StartFloatingViewService(Context context, FloatingObject userData)
        {
            try
            {
                // *** You must follow these rules when obtain the cutout(FloatingViewManager.findCutoutSafeArea) ***
                try
                {
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
                    {
                        // 1. 'windowLayoutInDisplayCutoutMode' do not be set to 'never'
                        if (ActivityContext?.Window.Attributes.LayoutInDisplayCutoutMode == LayoutInDisplayCutoutMode.Never)
                        {
                            //Toast.MakeText(Application.Context, "windowLayoutInDisplayCutoutMode' do not be set to 'never" , ToastLength.Short).Show();
                            //throw new Exception("'windowLayoutInDisplayCutoutMode' do not be set to 'never'");
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                if (ChatHeadService.RunService)
                {
                    return;
                }

                // launch service
                Intent intent = new Intent(context, typeof(ChatHeadService));

                if (ActivityContext != null)
                {
                    intent.PutExtra(ChatHeadService.ExtraCutoutSafeArea, FloatingViewManager.FindCutoutSafeArea(ActivityContext));
                }

                intent.PutExtra("UserData", JsonConvert.SerializeObject(userData));
                ContextCompat.StartForegroundService(context, intent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
コード例 #6
0
ファイル: App.cs プロジェクト: Arushacked/keepass2
        public void UpdateOngoingNotification()
        {
            // Start or update the notification icon service to reflect the current state
            var ctx = Application.Context;

            if (DatabaseIsUnlocked || QuickLocked)
            {
                ContextCompat.StartForegroundService(ctx, new Intent(ctx, typeof(OngoingNotificationsService)));
            }
            else
            {
                //Anrdoid 8 requires that we call StartForeground() shortly after starting the service with StartForegroundService.
                //This is not possible when we're closing the service. In this case we don't use the StopSelf in the OngoingNotificationsService.OnStartCommand() anymore but directly stop the service.

                OngoingNotificationsService.CancelNotifications(ctx); //The docs are not 100% clear if OnDestroy() will be called immediately. So make sure the notifications are up to date.

                ctx.StopService(new Intent(ctx, typeof(OngoingNotificationsService)));
            }
        }