【问题标题】:Start an Alarm every 10 seconds with AlarmManager使用 AlarmManager 每 10 秒启动一次警报
【发布时间】:2021-07-27 17:29:40
【问题描述】:

我正在制作一个安卓应用,我想每 10 分钟激活一次闹钟。

问题是警报只激活一次。

这是启动 BroadcastReceiver 的代码:

public void CallAlarmReceiver(bool isRepeating, bool destroy)
    {

        Intent i = new Intent(this, typeof(AlarmReceiver));
        i.SetAction("android.intent.action.NOTIFY");

        PendingIntent pi = PendingIntent.GetBroadcast(this, 0, i, PendingIntentFlags.UpdateCurrent);

        AlarmManager alarmManager = this.GetSystemService(Context.AlarmService) as AlarmManager;

        AlarmManagerCompat.SetExactAndAllowWhileIdle(alarmManager, (int)AlarmType.RtcWakeup, (long)(Calendar.GetInstance(Android.Icu.Util.TimeZone.Default).TimeInMillis + TimeSpan.FromSeconds(10).TotalMilliseconds), pi);
    }

【问题讨论】:

  • 我想你想循环它吧..?我没有看到使用的参数“isRepeating”,我想你可能忽略了
  • 不要注意参数,它们在函数后面使用,但这部分并不重要。

标签: c# android .net xamarin alarmmanager


【解决方案1】:

收到广播时。再次调用此方法。

【讨论】:

  • 这就是我所做的,但我希望这个警报在应用程序被终止时工作并且我需要活动上下文来排队一个在应用程序被终止时不可用的警报......
  • 那么这个答案会帮助你stackoverflow.com/a/43253631/9932178
  • C'est intéressant mais je dois faire cela avec une alarme...
  • 仅供参考。您可以在此服务器中添加您的警报管理器或在此服务中添加其他方法。
【解决方案2】:

一个简单的方法是使用计时器。

Device.StartTimer(TimeSpan.FromSeconds(10), () =>
{
    // Do something

    return true; // True = Repeat again, False = Stop the timer
});

【讨论】:

猜你喜欢
  • 2013-08-22
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
相关资源
最近更新 更多