【问题标题】:Alarm Goes off everytime I open the App每次打开应用程序都会响起警报
【发布时间】:2017-08-13 02:19:27
【问题描述】:

我不明白为什么每当我打开我的应用程序时,我设置为通过闹钟触发的通知就会被触发。我只在日历上设置了特定的日期和时间。不仅如此,我的闹钟甚至没有在日历上指定的时间和星期几响起。我什至需要我的闹钟服务吗?我正在努力确保即使在应用程序关闭时它也会消失。

下面的代码 (MyService) 在 MainActivity onCreate() 中被调用。

[Service]
public class MyService : Service
{

    //const int NOTIFICATION_ID = 9000;

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        SetAlarm();

        return StartCommandResult.Sticky;
    }

    private void SetAlarm()
    {

        //setting Calendar
        Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
        calendar.Set(Java.Util.CalendarField.DayOfWeek, 1);
        calendar.Set(Java.Util.CalendarField.HourOfDay, 02);
        calendar.Set(Java.Util.CalendarField.Minute, 15);

        AlarmManager manager = (AlarmManager)GetSystemService(Context.AlarmService);
        Intent managerIntent;
        PendingIntent pendingIntent;

        managerIntent = new Intent(this, typeof(AlarmNotificationReceiver));
        // pendingIntent = PendingIntent.GetBroadcast(this, 0, managerIntent, 0);
        pendingIntent = PendingIntent.GetBroadcast(this, 0, managerIntent, PendingIntentFlags.UpdateCurrent);


        //manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 3000, 60 + 1000, pendingIntent);
        // manager.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + AlarmManager.IntervalHalfHour, AlarmManager.IntervalHalfHour, pendingIntent);
        // manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, AlarmManager.IntervalHalfHour, pendingIntent);
        manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 604800, pendingIntent);

    }

[BroadcastReceiver(Enabled = true)]
public class AlarmNotificationReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.SetAutoCancel(true)
            .SetDefaults((int)NotificationDefaults.All)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle("Comic Pull List")
            .SetStyle(new NotificationCompat.BigTextStyle().BigText("New Comics have came out this week! Check your list and pull comics if needed."))
            .SetContentText("New Comics have came out this week! Check your list and pull comics if needed.");

        NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
        manager.Notify(1, builder.Build());


    }
}

}

我添加的新代码如下:

        AlarmManager manager = (AlarmManager)GetSystemService(Context.AlarmService);

        //setting Calendar
        Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
        if (calendar.Get(Java.Util.CalendarField.DayOfWeek) == Java.Util.Calendar.Wednesday)
        {
            calendar.Add(Java.Util.CalendarField.Date, 4);
        }
        while (calendar.Get(Java.Util.CalendarField.DayOfWeek) != Java.Util.Calendar.Wednesday)
        {
            calendar.Add(Java.Util.CalendarField.Date, 4);
        }
        //calendar.Set(Java.Util.CalendarField.DayOfWeek, 1);
        calendar.Set(Java.Util.CalendarField.HourOfDay, 15);
        calendar.Set(Java.Util.CalendarField.Minute, 30);


        Intent managerIntent;
        PendingIntent pendingIntent;

        managerIntent = new Intent(this, typeof(AlarmNotificationReceiver));
        // pendingIntent = PendingIntent.GetBroadcast(this, 0, managerIntent, 0);
        pendingIntent = PendingIntent.GetBroadcast(this, 0, managerIntent, PendingIntentFlags.UpdateCurrent);


        //manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 3000, 60 + 1000, pendingIntent);
        // manager.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + AlarmManager.IntervalHalfHour, AlarmManager.IntervalHalfHour, pendingIntent);
        // manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, AlarmManager.IntervalHalfHour, pendingIntent);
        manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 60 * 60 * 24 * 7, pendingIntent);

【问题讨论】:

    标签: c# android xamarin service alarmmanager


    【解决方案1】:

    您正在设置过去的第一个闹钟。

    如果您指定的触发时间已经过去,则立即触发警报。

    您需要将日期设置为下一个星期日。

    示例:

    using (AlarmManager manager = (AlarmManager)GetSystemService(AlarmService))
    using (var calendar = Calendar.Instance)
    {
        if (calendar.Get(CalendarField.DayOfWeek) == Calendar.Sunday)
            calendar.Add(CalendarField.Date, 1);
        while (calendar.Get(CalendarField.DayOfWeek) != Calendar.Sunday)
            calendar.Add(CalendarField.Date, 1);
        calendar.Set(CalendarField.HourOfDay, 02);
        calendar.Set(CalendarField.Minute, 15);
        Log.Debug("SO", $"Current date is   : {Calendar.Instance.Time.ToString()}");
        Log.Debug("SO", $"Alarm will fire at {calendar.Time.ToString()}");
        var managerIntent = new Intent(this, typeof(MainActivity));
        var pendingIntent = PendingIntent.GetBroadcast(this, 0, managerIntent, PendingIntentFlags.UpdateCurrent);
        manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 1000 * 60 * 60 * 24 * 7, pendingIntent);
    }
    

    输出:

    [SO] Current date is   : Sat Aug 12 23:01:11 PDT 2017
    [SO] Alarm will fire at: Sun Aug 13 02:15:11 PDT 2017
    

    【讨论】:

    • 所以它会在下周日开始,然后继续只在周日关闭?
    • @Freiza1991 1000 * 60 * 60 * 24 * 7 = 7 天(以毫秒为单位),尝试运行一次应用,然后将设备时间更改为下周日。
    • 好的,所以我设置了周三出发的时间运行一次,然后在手机上设置我的日期/更改以匹配它,这次我没有收到任何通知。我将新代码添加到帖子底部以供参考。
    • @Freiza1991 您将两个calendar.Add 通话从1 天更改为4 天???您可能想了解Calender 的工作原理:docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
    • 我确实这样做了,但没有奏效。我已发布我的代码以供参考
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多