重复报警以处理打盹模式

时间:2017-03-29 23:37:50

标签: android xamarin android-service alarmmanager android-doze

我有一个基于用户位置自动化的应用程序。我现在处理打盹模式,因为打瞌睡引发了我在后台根据位置变化发送请求的能力。我希望每9分钟或在维护窗口期间提供我的服务上网服务。我正在使用AlarmManager和SetExactAndAllowWhileIdle,但由于某些原因,我的警报一遍又一遍地发送垃圾邮件,尽管我给他们一个将来的AlarmType.RtcWakeup。我的位置服务在应用程序启动时启动,我的服务也会在终止应用程序和重新启动设备时启动/重新启动。

我对何时何地设置闹钟感到困惑。现在我在定位服务开始时设置我的闹钟。警报会立即设置,甚至立即触发,而不是我将它们设置为触发的时间。我正在使用AlarmType.RtcWakeup设置SetExactAndAllowWhileIdle,我的时间看起来正确。我用烤面包将它打印出来,它在未来大约9分钟就可以清楚地显示出来。

我应该何时设置闹钟?我不应该对从警报中收到的每一个OnHandleIntent采取行动吗?我是否错误地设置了闹钟?

现在,当我的位置服务开始时,我正在调用它:

StartAlarm_Now();

我应该在应用程序终止或暂停时设置此项吗?

    // I'm using AlarmType.RtcWakeup, so I need to get current time in milliseconds
    public static long GetDateTimesMillisecondsForToday()
    {
        var test = (DateTime.Now).Subtract(DateTime.Today);
        var hours = test.TotalHours;
        long milliseconds = (long)(test.TotalMilliseconds);
        return milliseconds;
    }

    // Calling this will set the alarm for now.  Should I ever need to do this??
    public static void StartAlarm_Now()
    {
        string id = "Now_" + new Random().Next(1000, 9999).ToString();
        StartAlarm(id, GetDateTimesMillisecondsForToday());
    }

    // Calling this will set the alarm for x seconds from now.  This is the right way to do it?  
    public static void StartAlarm_SecondsFromNow(long a_secondsFromNow)
    {
        string id = "SecondsFromNow_" + new Random().Next(1000, 9999).ToString();
        long millisecondsToUse = GetDateTimesMillisecondsForToday() + (a_secondsFromNow * 1000);
        StartAlarm(id, millisecondsToUse);
    }

    private static void StartAlarm(string id, long a_milliseconds)
    {
        TimeSpan t = TimeSpan.FromMilliseconds(a_milliseconds);
        string readableTime_fromMilliseconds = string.Format(
            "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
            t.Hours,
            t.Minutes,
            t.Seconds,
            t.Milliseconds
        );
        string message = "StartAlarm " + id + " readableTime_fromMilliseconds = " + readableTime_fromMilliseconds;
        SCoreAppContext.Instance.Container.Get<ISCoreAppGlobal<Activity>>().SafeInvokeOnMainThread(() =>
        {
            Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short).Show();
        });

        using (AlarmManager alarmManager = (AlarmManager)AppContext.GetSystemService(Context.AlarmService))
        {
            Intent serviceIntent = new Intent(Android.App.Application.Context, typeof(TestService));
            PendingIntent pendingIntent = PendingIntent.GetService(Android.App.Application.Context, 0, serviceIntent, PendingIntentFlags.CancelCurrent);
            alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, a_milliseconds, pendingIntent);
        }
    }


[Service(Exported = false)]
public class TestService : IntentService
{

    public override void OnCreate()
    {
        base.OnCreate();
    }

    public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
    {
        return null;
    }

    protected override async void OnHandleIntent(Intent intent)
    {
        // This part is spamming constantly while the app is running.
        // Here is where I run my code that should execute when my alarm expires, right?... That's what I'm assuming, but this is spamming a LOT.  And my alarms set for the future and this is getting called immediately
        SCoreAppContext.Instance.Container.Get<ISCoreAppGlobal<Activity>>().SafeInvokeOnMainThread(() =>
        {
            Toast.MakeText(Android.App.Application.Context, "JoeyZTestService.OnHandleIntent", ToastLength.Short).Show();
        });


        // I read that if you want the alarm to repeat, you can set another one when this one fires.  But I believe this is why mine are looping forever.  So how can i repeat if not here?  
        // The 530 is the # of seconds in the future that the alarm should fire.  
        StartAlarm_SecondsFromNow(530);
    }

}

0 个答案:

没有答案