Unable to set repeat alarm on boot time
I got notification at 10:30 PM. But when my device is switch off at 10:30 PM and then after i switch on my device at 11:00 PM i didnt get pending notification. So i dont understand whats the problem is. any help will appreciated.
Here is my code in activity on create.
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);
}
My Receiver code is here and successfully get notification when device is ON.
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);
here is my Boot receiver code.
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);
}
}
And Last in manifest i declared receiver.
<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>
I solved on that day but forgot to answered here. I made some changes in manifest file. Just remove line category default in both intent filter and works.
<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>
I ain't writing down the entire code for you. But I can show you where you can apply your logic.
-> First, Find a way of tracking yourself, what is missed, such as by tracking event status in a database.
-> Then, Set up a BOOT_COMPLETED
BroadcastReceiver
to find when the phone is rebooted. In there, schedule any alarm(s) that are still in the future, and decide what to do about alarms that occurred in the past but were missed (eg, raise a Notification pointing out the missed events).
-- One way to do this would be to listen for the phone shutdown event:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
and before the device shuts down, save the shutdown time.
Then listen for device bootup:
<action android:name="android.intent.action.BOOT_COMPLETED" />
and write your own logic that determines which alarms would have fired during that downtime, and fire those alarms immediately with AlarmManager.
链接地址: http://www.djcxy.com/p/90786.html上一篇: 在安装Firefox 42或更新版本的Firefox中安装未签名的XPI
下一篇: 无法在开机时设定重复报警