簡體   English   中英

每當我打開應用程序時,鬧鍾響起

[英]Alarm Goes off everytime I open the App

我不知道為什么每當我打開我的應用程序時,我的通知集就會被鬧鍾響起。 我將其設置在日歷中僅用於特定的日期和時間。 不僅我的鬧鍾在日歷的指定時間和星期幾都沒有響起。 我的鬧鍾甚至需要服務嗎? 我正在嘗試確保即使關閉應用程序也能解決此問題。

下面的代碼(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);

您正在設置過去的第一個警報。

如果您指定的觸發時間是過去的時間,則警報將立即觸發。

您需要將日期設置為下一個星期日。

例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM