【发布时间】:2017-07-10 08:26:11
【问题描述】:
这是我在活动类的 oncreate 中设置每日警报服务的代码。
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
if (curHr >=9) {
// Since current hour is over 9, setting the date to the next day
calendar.add(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, aHOUR);
calendar.set(Calendar.MINUTE, aMINUTE);
calendar.set(Calendar.SECOND, aSECOND);
Intent intent1 = new Intent(getBaseContext(), MyReciever.class);
intent1.putExtra("requestcode", "0");
intent1.putExtra("status", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, pendingIntent);
这是我在接收方的代码
String req=intent.getStringExtra("requestcode");
String state=intent.getStringExtra("status");
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, FirstActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, Integer.valueOf(req),
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(Integer.valueOf(req)==1)
{
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Hi "+appref.getUserFirstName()+" "+appref.getUserLastName())
.setContentText("Having a great Day? Log it in MyApp").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
if(Integer.valueOf(state)==1)
{
notificationManager.notify(MID1, mNotifyBuilder.build());
}
else
{
notificationManager.cancel(MID1);
}
除了重复,一切正常。问题是alarm notification 在第二天无法工作。如果您对此问题有任何解决方案,请提供帮助。
INTERVAL 是 alarmmanager.intervalday_ 相同的问题,然后更改为 int interval=86000L 但问题看起来相同。
提前致谢
【问题讨论】:
-
设备是否在这段时间内启动?如果是这样,那么
AlarmManager请求将被丢弃。您可能已经这样做了,但请尝试将持续时间更改为更小(约 5 分钟),以确认没有其他干扰。 -
5 分钟。 15 分钟……甚至几个小时都在工作。手动不重新启动我的系统。即使启动发生,它会影响接收者等级吗?
-
是的...
AlarmManager请求在系统启动时终止。即使它现在没有发生,你也必须补偿它。设置一个BroadcastReceiver用于监听启动BOOT_COMPLETED事件,然后再次调用AlarmManager方法。 -
您能否调试并发现您的接收器是否在第二天被调用。如果您的接收器被调用,则问题可能会显示通知。 notificationManager.notify(MID1, mNotifyBuilder.build());在这个调用中确保 MIDI 是唯一的。如果您输入相同的 MID1 值,则通知与现有通知重叠并显示为一个。如果您的调度程序运行良好,这可能是原因。
-
Shaishav:我正在使用广播接收器,但未使用意图过滤器 BOOT_COMPLETED 并获得引导加载程序权限。它会导致问题吗?
标签: android alarmmanager repeatingalarm