Android: why did the Alarm notification stop after system reboot
Asked Answered
T

1

5

I am developing an android application that should fire an alarm five times a day:
- the times in each day is not constant
- after the alarm is fired I will schedule the next alarm.

My problem is : the alarm notification works for 1 day then it stops and also when the device is rebooted twice the notification doesn't work , I don't now if there is another way to do that , any help will be very appreciated!

Code: I have this function to call the broadcast reciever

public static void Start_Notifying(Context context){             
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.add(Calendar.HOUR_OF_DAY,hour);
    cal.add(Calendar.MINUTE, min);      
    Intent intent = new Intent(context,  OnetimeAlarmReceiver.class);
    intent.setAction("START_NOTIFYING");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notify.REQUEST_CODE, intent,0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() , pendingIntent); 
    SaveAlarmManager(alarmManager, pendingIntent);

}

the code of oneTimeAlarmReciever

public void onReceive(Context context, Intent intent) {     
    main_menu.con = context;                
    Notification notifyDetails;
    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifyDetails = new Notification(R.drawable.icon,s1,System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, new Intent(context, prayertimes.class), 0);
    notifyDetails.setLatestEventInfo(context,s2+ notify.prayName , s3, pendingIntent);
    notifyDetails.sound=Uri.parse(PlaySound());
    nm.notify(NOTIFICATION_ID, notifyDetails);                          
    main_menu.notify_me();
}

The code of notify_me()

static void notify_me() {               
    hour =pTime.num1;
    min =pTime.num2;                
    Start_Notifying(con);
}

In the manifest file

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

<receiver class =".OnetimeAlarmReceiver" android:name="OnetimeAlarmReceiver">
    <intent-filter>
        <action android:name="START_NOTIFYING" />
        <action android:name="android.intent.action.BOOT_COMPLETED"> </action>
        <data android:mimeType="audio/ogg" />
    </intent-filter>
</receiver>
Travancore answered 14/8, 2011 at 11:5 Comment(0)
J
25

why did the Alarm notification stop after system reboot

Because alarm schedules are cleared on a reboot. If you want to have your alarms pick up again after a reboot, you will need to implement a BroadcastReceiver that responds to the RECEIVE_BOOT_COMPLETED broadcast. The demo/ project in my WakefulIntentService repo demonstrates this.

Jubbah answered 14/8, 2011 at 19:12 Comment(2)
Thank u very much ,is there anyway to update the broadcast receiver like every 2 hours to ensure that the alarms will always work even the system didn't reboot; in some device the alarms stop working after 2 days (and the device never rebooted in this time) don't know why this is happening!Travancore
I suggest registring a second scheduled task to reregister the other ones. This second scheduled task would run repeatedly, every 40 hours.Rhizotomy

© 2022 - 2024 — McMap. All rights reserved.