我在晚上10:30收到通知。但是当我的设备在晚上10:30关闭然后在我下午11:00打开我的设备后,我没有得到待处理的通知。所以我不明白问题是什么。任何帮助将不胜感激。
以下是我在创建活动中的代码。
Intent alarmIntent = new Intent(CH_Dashboard.this, TimeAlarmEvening.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(CH_Dashboard.this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
firingCal.set(Calendar.AM_PM,Calendar.PM);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime)
{
manager.setRepeating(AlarmManager.RTC,
intendedTime, AlarmManager.INTERVAL_DAY,
pendingIntent);
}
else
{
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC,
intendedTime, AlarmManager.INTERVAL_DAY,
pendingIntent);
}
我的接收器代码在此处,并在设备开启时成功获取通知。
Intent notificationIntent = new Intent(context, CH_Dashboard.class);
notificationIntent.putExtra("fromNotification","notify");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(CH_Dashboard.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
Notification notification = builder.setContentTitle("Demo App Notification")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
这是我的Boot接收器代码。
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent alarmIntent = new Intent(context, TimeAlarmEvening.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
firingCal.set(Calendar.AM_PM,Calendar.PM);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime)
{
manager.setInexactRepeating(AlarmManager.RTC,
intendedTime, AlarmManager.INTERVAL_DAY,
pendingIntent);
}
else
{
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
manager.setInexactRepeating(AlarmManager.RTC,
intendedTime, AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
最后在清单中我宣布了接收器。
<receiver
android:name=".model.AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".TimeAlarmEvening">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION_EVENING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
答案 0 :(得分:4)
那天我解决了,但忘了在这里回答。我在清单文件中做了一些更改。只需删除两个意图过滤器中的行类别默认值即可。
<receiver
android:name=".model.AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".TimeAlarmEvening">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION_EVENING" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
我没有为你写下整个代码。但我可以告诉你在哪里可以应用你的逻辑。
- &GT;首先,找到一种跟踪自己,错过的方法,例如通过跟踪数据库中的事件状态。
- &GT;然后,设置BOOT_COMPLETED
BroadcastReceiver
以查找电话何时重新启动。在那里,安排任何仍在将来的警报,并决定如何处理过去发生但错过的警报(例如,提出通知指出错过的事件)。
- 执行此操作的一种方法是收听电话关闭事件:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
在设备关闭之前,保存关机时间。
然后听设备启动:
<action android:name="android.intent.action.BOOT_COMPLETED" />
并编写自己的逻辑,确定在停机期间将触发哪些警报,并使用AlarmManager立即触发警报。