c# - La alarma se apaga cada vez que abro la aplicación
android xamarin (1)
Está configurando la primera alarma en el pasado.
Si el tiempo de activación especificado es anterior, la alarma se dispara inmediatamente.
Debe establecer la fecha para el próximo domingo.
Ejemplo:
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);
}
Salida:
[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
No puedo entender por qué cada vez que abro mi aplicación, mi notificación configurada para sonar por una alarma se activa. Lo tengo configurado en un calendario solo para un día y hora específicos. No solo mi alarma no se apaga a la hora y el día especificados del calendario. ¿Incluso necesito un servicio con mi alarma? Estoy intentando asegurarme de que esto se active incluso cuando la aplicación esté cerrada.
El código a continuación (MyService) se llama en 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());
}
}
}
Mi nuevo código que agregué está a continuación:
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);