[c#] アラーム私がアプリケーションを開くたびに消えます


0 Answers

Question

なぜ私は私のアプリを開くたびにアラームで消えるように設定されている通知がオフに設定されているかわからない。 特定の日時のカレンダーに設定しています。 カレンダーから指定された時刻と曜日にアラームが消えないだけでなく、 私はアラーム付きのサービスも必要ですか? 私は、アプリケーションが終了してもこれが消えることを確認しようとしています。

以下のコード(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);



Related