我希望每天晚上20点钟设置闹钟, 当我在模拟器中调试应用程序时一切正常,但在我的手机中有时它工作,有时不工作。 这是我的代码
public class AlarmUtility
{
private static TimeSpan AlarmTime = new TimeSpan(20, 00, 0);
private static long CurrentAlarm;
public static void StartAlarm()
{
AlarmManager manager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(Application.Context, typeof(DailyNotificationReciever));
pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, myIntent, PendingIntentFlags.UpdateCurrent);
long date;
//check if alarm date has passed
if(DateTime.Now.TimeOfDay < AlarmTime)
{
date = getAlarmJavaTime(false);
}
else
{
date = getAlarmJavaTime(true);
}
if (CurrentAlarm != date)
{
manager.SetInexactRepeating(AlarmType.RtcWakeup, date, AlarmManager.IntervalDay, pendingIntent);
CurrentAlarm = date;
}
}
private static long getAlarmJavaTime(bool addDay)
{
Calendar cal = Calendar.Instance;
cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
cal.Set(CalendarField.HourOfDay, AlarmTime.Hours);
cal.Set(CalendarField.Minute, AlarmTime.Minutes);
cal.Set(CalendarField.Second, AlarmTime.Seconds);
if (addDay)
{
cal.Add(CalendarField.DayOfMonth, 1);
}
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
string a = dateFormat.Format(cal.Time);
return cal.TimeInMillis;
}
}
我做错了什么?
也许当应用程序处于非活动状态时,警报不会设置?
更新
我的DailyNotificationReciever代码按要求
[BroadcastReceiver(Enabled = true)]
public class DailyNotificationReciever : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "DailyNotificationReciever", ToastLength.Long).Show();
//create nofification id
Random rnd = new Random();
int notificationNumber = rnd.Next(1000);
//create the intents
//user click yes
Intent IntentYes = new Intent("UserClickedYes");
PendingIntent pendingIntentYes = PendingIntent.GetBroadcast(context, (int)DateTime.Now.Ticks, IntentYes, PendingIntentFlags.CancelCurrent);
//user click no
Intent IntentNo = new Intent("UserClickedNo");
PendingIntent pendingIntentNo = PendingIntent.GetBroadcast(context, (int)DateTime.Now.Ticks, IntentNo, PendingIntentFlags.CancelCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.SetAutoCancel(true)
.SetDefaults((int)NotificationDefaults.All)
.SetContentTitle("test")
.SetContentText("test")
.SetAutoCancel(true)
.AddAction(Resource.Drawable.Test_32px, "Yes", pendingIntentYes)
.AddAction(Resource.Drawable.Test_32px, "No", pendingIntentNo);
NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
manager.Notify(
notificationNumber, builder.Build());
}
}
谢谢
答案 0 :(得分:1)
由于广播接收器是应用程序的一个组件,如果应用程序停止,它将不会收到任何警报或事件。这可能是您的代码有时可以工作的原因,有时候不是,除了代码级别,我没有发现您的代码有任何问题。
如果要检查这种可能性,可以使用以下adb命令:
adb shell dumpsys package "com.package.name" and check "stopped=true"
检查应用程序是否处于停止状态。
如果确认,我只能说,这种行为是设计的。
答案 1 :(得分:0)
你应该知道一些事情:
1)设备重启后所有警报都清除,您必须再次安排它:does Alarm Manager persist even after reboot?
2)如果您使用相同的PendingIntent(同样的requestCode,空Intent和相同的标志)安排警报,则此警报将覆盖之前的警报。
尝试根据以上信息分析您的问题。