【问题标题】:How to run background task erveryday at 6:30PM如何每天下午 6:30 运行后台任务
【发布时间】:2018-04-24 05:03:18
【问题描述】:

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

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 xamarin.forms notifications broadcastreceiver localnotification


【解决方案1】:

在 Android 上,如果版本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;
    }

【讨论】:

  • 它对我不起作用。我只是下载github项目和调试。你能与本地通知集成吗
  • 正在运行 Redmi Y1 Lite。安卓版本为7.1.2 N2G47H。
  • 嗨,@AswathiPS,如果你的手机是 7.1,运行演示后,请检查日志,每 5 秒你会看到“222222222”。
  • 嗨,@AswathiPS,我已经用通知更新了我的演示。请检查一下。
  • 嗨,@Joe LV - MSFT 正在使用相同的设备运行演示。并将 JobInfo 上的 SetPeriodic 替换为 1*1000 ,但它不会每 1 秒触发 JobSchedulerService 。 JobInfo 显示 IntervelMillis =900000。那么这在 15 分钟间隔内有效吗?
猜你喜欢
  • 2019-02-10
  • 2015-12-03
  • 1970-01-01
  • 2023-04-09
  • 2021-06-21
  • 2015-01-29
  • 1970-01-01
  • 2021-10-21
  • 2019-08-20
相关资源
最近更新 更多