Repeating Alarm Manager After reboot

后端 未结 1 820
醉酒成梦
醉酒成梦 2020-12-04 02:13

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,

相关标签:
1条回答
  • 2020-12-04 02:43

    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.

    0 讨论(0)
提交回复
热议问题