Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating Alarm Manager After reboot

i want to create broadcast AlarmManager(repeating) with Notification message.i pass my calender object from Pickers. If i don't reboot my device it works normally. However, when i reboot my device,as you know my calander object will be null. How can i manage my repeating alarm after rebooting and how can i hold my Calendar schedules? Thanks for your ideas.

public class MyReceiver extends BroadcastReceiver {
private static final int PERIOD = 10000;
final public static String ALARM_ID = "AlarmId";
final public static String NOTIFICATION_ID = "NotificationId";


@Override
public void onReceive(Context ctxt, Intent i) {

}

static void scheduleAlarms(Context ctxt,Calendar c) {
    AlarmManager alarManager = (AlarmManager) ctxt
            .getSystemService(Context.ALARM_SERVICE);
//notification servise  
    Intent i = new Intent(ctxt, ScheduledService.class);
i.putExtra(ALARM_ID, 1);
i.putExtra(NOTIFICATION_ID, 1);

PendingIntent pi = PendingIntent.getService(ctxt, 0, i,
            PendingIntent.FLAG_UPDATE_CURRENT);
alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),PERIOD, pi);
}
like image 249
Osman Yılmaz Avatar asked Jul 30 '13 07:07

Osman Yılmaz


People also ask

Does alarm Manager persist even after reboot?

The AlarmManager service is a convenient class to schedule working on your Android application; however, when the device shuts down or reboots, all your alarms will be lost since the system does not retain them between system restarts.

How do I turn off alarm manager?

You can cancel the alarm like this: Intent intent = new Intent(this, Mote. class); PendingIntent pendingIntent = PendingIntent. getBroadcast(getApplicationContext(), 1253, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.


Video Answer


1 Answers

You need to use a BroadcastReceiver and set it to respond to BOOT_COMPLETED messages. For example

Register your BroadcastReceiver in the manifest

<receiver android:name=".MyBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Handle the message within your code

MyBootReceiver.java

public class MyBootReceiver extends BroadcastReceiver 
{
    private static final String TAG = "MyBootReceiver";

    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        Calendar cal = this.getMyCalendar();
        this.scheduleAlarms(context, cal);
    }

    private Calendar getMyCalendar() {
        // get your calendar object
    }

    private void scheduleAlarms(Context ctxt, Calendar c) {
        AlarmManager alarManager = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
        //notification servise  
        Intent i = new Intent(ctxt, ScheduledService.class);
        i.putExtra(ALARM_ID, 1);
        i.putExtra(NOTIFICATION_ID, 1);

        PendingIntent pi = PendingIntent.getService(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),PERIOD, pi);
    }
}

This will reset your alarm schedule on boot.

like image 110
Kirk Avatar answered Nov 02 '22 22:11

Kirk