Esempio n. 1
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            if (isStarted)
            {
                foreach (var data in SQLLiteDB.ReadUserScheduleData())
                {
                    if (data.IsNotify == false && data.Time < DateTime.Now.AddMinutes(15))
                    {
                        //   CrossLocalNotifications.Current.Show(data.Message, DateTime.Now.ToString(), data.Id, DateTime.Now);

                        var manager = (NotificationManager)GetSystemService(NotificationService);

                        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                        {
                            var notification = new Notification.Builder(this, "DE")
                                               .SetContentTitle(DateTime.Now.ToString() + "알림!")
                                               .SetContentText(data.Message)
                                               .SetSmallIcon(Resource.Drawable.xamagonBlue)
                                               .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.xamagonBlue))
                                               .SetSmallIcon(Resource.Drawable.xamagonBlue)
                                               .Build();

                            manager.Notify(data.Id, notification);
                        }
                        else
                        {
                            var notification = new Notification.Builder(this)
                                               .SetContentTitle(DateTime.Now.ToString() + "알림!")
                                               .SetContentText(data.Message)
                                               .SetSmallIcon(Resource.Drawable.xamagonBlue)
                                               .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.xamagonBlue))
                                               .SetSmallIcon(Resource.Drawable.xamagonBlue)
                                               .Build();

                            manager.Notify(data.Id, notification);
                        }

                        //Notify update
                        data.IsNotify = true;
                        SQLLiteDB.Upsert(data, false);
                    }
                }
            }
            else
            {
                RegisterForegroundService();
                isStarted = true;
            }

            SQLLiteDB.InsertScheduleLog(DateTime.Now);

            AlarmReceiver.AddAlarmEvent(10);

            return(StartCommandResult.Sticky);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建服务
        /// </summary>
        public override void OnCreate()
        {
            base.OnCreate();
            //
            isRun   = true;
            _nm     = (NotificationManager)GetSystemService(NotificationService);
            _intent = new Intent(Application.Context, typeof(MainActivity));
            //通知
            ServerNotification("Alarm Running...");
            //注册广播
            _alarmReceiver = new AlarmReceiver();
            IntentFilter intentFilter = new IntentFilter();

            intentFilter.AddAction("Xamarin.Broadcast.AlarmTests");
            RegisterReceiver(_alarmReceiver, intentFilter);
        }
Esempio n. 3
0
        public override void OnCreate()
        {
            base.OnCreate();

            try
            {
                context  = Android.App.Application.Context;
                partLoad = new PartLoad();
                settings = Settings.Instance;

                #region Layout notification
                LayoutNotificationResourceInit();
                #endregion

                #region Notify Receivers
                NotifyReceiver notifyReceiver = new NotifyReceiver();

                IntentFilter notifyIntentFilter = new IntentFilter("com.arbuz.widram.notifyreceiver");
                notifyIntentFilter.AddAction(Info.IntentActionStopService);
                notifyIntentFilter.AddAction(Info.IntentActionRestartServiceRam);
                notifyIntentFilter.AddAction(Info.IntentActionUpdateSettings);

                context.RegisterReceiver(notifyReceiver, notifyIntentFilter);
                #endregion

                #region  Loading Receivers
                LoadingReceiver loadingReceiver     = new LoadingReceiver();
                IntentFilter    loadingIntentFilter = new IntentFilter(Android.Content.Intent.ActionBootCompleted);
                context.RegisterReceiver(loadingReceiver, loadingIntentFilter);
                #endregion

                #region  Alarm Receivers
                AlarmReceiver alarmReceiver = new AlarmReceiver();

                IntentFilter alarmIntentFilter = new IntentFilter("com.arbuz.widram.alarmreceiver");
                alarmIntentFilter.AddAction(Info.IntentActionAlarm);

                context.RegisterReceiver(alarmReceiver, alarmIntentFilter);

                Intent intentAlarm = new Intent(context, typeof(AlarmReceiver));
                intentAlarm.SetAction(Info.IntentActionAlarm);

                pendingIntentAlarm = PendingIntent.GetBroadcast(context, 0, intentAlarm, PendingIntentFlags.UpdateCurrent);

                alarmManager = (AlarmManager)context.GetSystemService(AlarmService);
                alarmManager.SetRepeating(AlarmType.ElapsedRealtime, 1000, 1000, pendingIntentAlarm); //API 19 Timer not working for setup number
                #endregion

                #region Main
                handler              = new Handler();
                action               = new Action(async() => await OnWork());
                partLoad.RamUpdated += Updated;
                #endregion
            }
            catch (System.Exception ex)
            {
                #region Logging
                LogRuntimeAttribute.InLogFiles(typeof(NotifyService), ex);
                #endregion
            }

            finally { }
        }
        protected override void OnHandleIntent(Intent intent)
        {
            ShowLocalNotification(ApplicationContext, intent);

            AlarmReceiver.CompleteWakefulIntent(intent);
        }
Esempio n. 5
0
            public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
            {
                AddPreferencesFromResource(Resource.Xml.settings);
                mPreferences = PreferenceManager.GetDefaultSharedPreferences(Activity);
                SettingsDir  = new File(Activity.DataDir, "shared_prefs");
                var exportPreference = FindPreference("export_settings");

                exportPreference.PreferenceClick += (sender, args) =>
                {
                    var paths = new List <File> {
                        new File(SettingsDir, SettingsFileName)
                    };
                    foreach (var file in Activity.FilesDir.ListFiles())
                    {
                        if (file.IsFile)
                        {
                            paths.Add(file);
                        }
                    }

                    if (paths.Count > 0)
                    {
                        if (OtherUtils.CreateZipArchive(paths, exportFile.AbsolutePath))
                        {
                            Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_export_successful),
                                           ToastLength.Short).Show();
                        }
                        else
                        {
                            Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_export_failed),
                                           ToastLength.Short).Show();
                        }
                    }
                    else
                    {
                        Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_export_no_files),
                                       ToastLength.Short).Show();
                    }
                };

                var importPreference = FindPreference("import_settings");

                importPreference.PreferenceClick += (sender, args) =>
                {
                    if (!exportFile.Exists())
                    {
                        Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_import_no_file),
                                       ToastLength.Short).Show();
                        return;
                    }

                    if (exportFile.Length() > 1024 * 1024)
                    {
                        Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_import_big_file),
                                       ToastLength.Short).Show();
                        return;
                    }

                    if (OtherUtils.UnpackZipArchive(exportFile.AbsolutePath, Activity.CacheDir))
                    {
                        Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_import_successful),
                                       ToastLength.Short).Show();
                    }
                    else
                    {
                        Toast.MakeText(Activity, Activity.GetString(Resource.String.toast_import_failed),
                                       ToastLength.Short).Show();
                        return;
                    }

                    foreach (var file in Activity.CacheDir.ListFiles())
                    {
                        Stream outputStream;

                        if (file.Name == SettingsFileName)
                        {
                            var path = new File(SettingsDir, SettingsFileName).AbsolutePath;
                            Log.Debug(nameof(OtherUtils.UnpackZipArchive), path);
                            outputStream = new FileStream(path, FileMode.Create);
                        }
                        else
                        {
                            outputStream = Activity.OpenFileOutput(file.Name, FileCreationMode.Private);
                        }

                        string text;
                        if (!file.Exists())
                        {
                            continue;
                        }
                        using (var reader = new StreamReader(new FileStream(file.AbsolutePath, FileMode.Open)))
                        {
                            text = reader.ReadToEnd();
                        }

                        using (var writer = new StreamWriter(outputStream))
                        {
                            writer.Write(text);
                        }

                        file.Delete();
                    }
                };

                var categoriesPreference =
                    (MultiSelectListPreference)FindPreference("selected_categories");

                categoriesPreference.PreferenceChange += (sender, args) =>
                {
                    var set = args.NewValue.JavaCast <HashSet>();
                    SystemModListenerService.SetSystemMonitoringStatus(Activity,
                                                                       set.Contains(DataHolder.SystemCategory));
                    LocationUtils.SetLocationTrackingEnabled(set.Contains(DataHolder.LocationCategory));
                    ScreenUnlockReceiver.SetUnlockReceiverStatus(Activity, set.Contains(DataHolder.ScreenCategory));
                    PhoneCallReceiver.SetCallReceiverStatus(Activity, set.Contains(DataHolder.PhoneCategory));
                    SmsReceiver.SetSmsReceiverStatus(Activity, set.Contains(DataHolder.SmsCategory));

                    var categories = new List <string>();
                    foreach (var val in set.ToArray())
                    {
                        categories.Add(val.ToString());
                    }

                    mPreferences.Edit().PutStringSet(categoriesPreference.Key, categories).Apply();
                };

                var about = FindPreference("app_info");

                about.Summary =
                    Activity.ApplicationContext.PackageManager.GetPackageInfo(Activity.PackageName, 0).VersionName;
                about.PreferenceClick += (sender, args) =>
                {
                    if ((mDevClickedTimes = (mDevClickedTimes + 1) % 7) == 0)
                    {
                        if (Activity is SettingsActivity parent)
                        {
                            parent.LoadScreen(SettingsCategory.Developer);
                        }
                    }
                };

                try
                {
                    var summaryNotifyPreference = FindPreference(MonitoringSummaryType);
                    summaryNotifyPreference.PreferenceChange += (sender, args) =>
                    {
                        AlarmReceiver.RegisterReceiver(Activity, (bool)args.NewValue ? 1 : 0);
                    };
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Log.Error(nameof(MonitoringSummaryType), e.Message);
                }
            }