如何 运行 后台任务每天在 6:30PM

How to run background task erveryday at 6:30PM

https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. 我正在使用这个后台工作者。我想在我的应用程序状态为 运行 或不为 运行 的每一天重复显示本地通知。它对我不起作用..请帮忙。 SendNotification() 正在从 main activity 调用。在我的应用程序状态为 运行 的情况下,重复的本地通知运行良好。但是在我的应用程序状态不是 运行.

的情况下它不起作用
public async Task SendNotification()
 {
  Intent alarmIntent = new Intent(this, typeof(AlarmReceiverNew));
  alarmIntent.PutExtra(“message”, “message”);
  alarmIntent.PutExtra(“title”, “Welcome”);

  PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
  AlarmManager alarmManager = (AlarmManager)this.GetSystemService(Context.AlarmService);
  long futureInMillis = SystemClock.ElapsedRealtime() + 12 * 3600 * 1000;
  alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, futureInMillis, pendingIntent);
 }

[BroadcastReceiver]
public class AlarmReceiverNew : BroadcastReceiver
 {
    public async override void OnReceive(Context context, Intent intent)
     {
       Intent notIntent = new Intent(context, typeof(MainActivity));
       PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
       NotificationManagerCompat manager = NotificationManagerCompat.From(context);

      var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, 1));

       NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        .SetContentIntent(contentIntent)
        .SetSmallIcon(Resource.Drawable.icon).SetContentTitle(“title”)
        .SetContentText(“message”)
        .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
        .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
        .Extend(wearableExtender);

        Notification notification = builder.Build();

        manager.Notify(0, notification);
       }
     }

我也在尝试 Android - Running a background task every 15 minutes, even when application is not running 解决方案。但输出相同。

在Android上,如果版本<=19可以使用AlarmManager,如果版本>20可以使用JobScheduler。这是 github.

上的演示

像这样:

   public void startTask()
        {
            if (Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)
            {

                AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
                Intent intent = new Intent(this, typeof(RepeatingAlarm));
                PendingIntent sender = PendingIntent.GetBroadcast(this, 0, intent, 0);
                // Schedule the alarm!
                alarmManager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime(), 5 * 1000, sender);

            }
            else
            {
                JobScheduler scheduler = (JobScheduler)GetSystemService(Context.JobSchedulerService);

                if (isJobPollServiceOn())
                {

                }
                else
                {
                    JobInfo jobInfo = new JobInfo.Builder(1,
                        new ComponentName(this, Java.Lang.Class.FromType(typeof(JobSchedulerService))))
                            .SetPeriodic(1000*5)
                            .Build();
                    scheduler.Schedule(jobInfo);
                }
            }

        }

isJobPollServiceOn方法:

   [TargetApi(Value = 20)]
    public bool isJobPollServiceOn()
    {
        JobScheduler scheduler = (JobScheduler)GetSystemService(Context.JobSchedulerService);

        bool hasBeenScheduled = false;
        var jobInfos = scheduler.AllPendingJobs;

        for (int i = 0; i < jobInfos.Count; i++)
        {
            if (jobInfos[i].Id == 1)
            {
                hasBeenScheduled = true;
                break;
            }

        }


        return hasBeenScheduled;
    }