每当我打开应用程序时,闹钟响起 [英] Alarm Goes off everytime I open the App

查看:106
本文介绍了每当我打开应用程序时,闹钟响起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么每次打开我的应用时,设置为被闹钟关闭的通知都会被取消。我将其设置在日历中仅用于特定的日期和时间。不仅我的闹钟在日历的指定时间和星期几都没有响起。我的闹钟甚至需要服务吗?我试图确保即使关闭应用程序也不会关闭。

I can't figure out why whenever I open my app my notification set to go off by an alarm is set off. I have it set on a calendar for only a specific day and time. Not only that my alarm isn't even going off at the specified time and day of the week from the calendar. Do i even need a service with my alarm? I am trying to make sure this goes off even when the application is closed.

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

The code below (MyService) is called in 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);


推荐答案

您正在设置过去的第一个警报。 / p>

You are setting the first alarm in the past.


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

If the trigger time you specify is in the past, the alarm triggers immediately.

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

You need to set the date to the next Sunday.

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);
}



输出:



Output:

[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

这篇关于每当我打开应用程序时,闹钟响起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆