0

I have a question with my alarm manager, I am making a alarm manager with timepickerdialog, but I want keep it working after reboot, I tried to add

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  <receiver android:name=".AlarmNotificationReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>    

in my manifest,but when I reboot my device,it will alarm immediately.

This is my activity code:

private void alarmManager(Calendar calendarTime,int id) {

    pendingIntent = PendingIntent.getBroadcast(this, id, intent, pendingIntent.FLAG_UPDATE_CURRENT);
    if(calendarTime.before(now)) {
        calendarTime.add(Calendar.DATE,1);
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        calendarTime.add(Calendar.DATE,-1);
    }
    else
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

And this is my receiver code:

public void onReceive(Context context, Intent intent) {
        if(intent.getStringExtra("contentTitle")!=null)
            contentTitle=intent.getStringExtra("contentTitle");

        if(intent.getStringExtra("contentText")!=null)
            contentText =intent.getStringExtra("contentText");

        Intent notifiIntent =new Intent(context,Home_Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
        builder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.tooth_icon)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND);

        NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,builder.build());
    }

This is my code where I pass my time to alarm function:

 protected TimePickerDialog.OnTimeSetListener morningTimePickerListener =new TimePickerDialog.OnTimeSetListener(){
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            m_calendar.setTimeZone(java.util.TimeZone.getTimeZone("GMT+8"));
            if(hourOfDay>12)
                hourOfDay-=12;
            m_calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            m_calendar.set(Calendar.MINUTE, minute);
            m_calendar.set(Calendar.SECOND, 0);
            m_calendar.set(Calendar.MILLISECOND, 0);
            m_time= simpleDateFormat.format(m_calendar.getTime()).trim();
            m_alarm.setText("AM "+m_time);
            view.setCurrentHour(hourOfDay);
            view.setCurrentMinute(minute);
            alarmManager(m_calendar,0);
            morningRef.setValue(m_time);
            Toast.makeText(Setting_Activity.this,m_calendar.getTime()+"",Toast.LENGTH_LONG).show();
        }
    };

How can I keep my alarm manager after reboot? plz

  • Where is your code where you pass the calendar to the alarm function ? – intellignt_idiot Aug 06 '17 at 11:11
  • I edited my post:) –  Aug 06 '17 at 14:14
  • Check existing answers: https://stackoverflow.com/questions/20065433/alarm-doesnt-trigger-after-reboot https://stackoverflow.com/questions/17941371/repeating-alarm-manager-after-reboot https://stackoverflow.com/questions/12034357/does-alarm-manager-persist-even-after-reboot https://stackoverflow.com/questions/39442607/how-to-reset-alarm-after-reboot-android-studio – dominicoder Aug 06 '17 at 14:28
  • I have seen these questions yet,but I meet a problam is that when I reboot my device,alarm manager will push notification immediately. –  Aug 06 '17 at 14:43

1 Answers1

0

Do not user Interval.Day. Instead user this :

Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(new Date().getTime());
    calendar.set(Calendar.HOUR_OF_DAY, 0); // MIDNIGHT 12 AM
    calendar.set(Calendar.MINUTE,00);
    calendar.set(Calendar.SECOND, 00);

 PendingIntent pi = PendingIntent.getBroadcast(this, alarmType, alarmIntent, 0);
    alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            1000 * 60 *60 *24,  // EVERY DAY
            pi);

This will trigger alarm every day at midnight 12:00 AM and it will not trigger immediately after boot. But only at 12 midnight.

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
  • It works for me. Perfectly. What does not work ? What happened when you used this code ? – intellignt_idiot Aug 06 '17 at 14:26
  • It still alarm me when I reboot my device immediately –  Aug 06 '17 at 14:40
  • Check my answer again. I have edited. See the change in second line. Instead of System.currentmilliseconds , now its date. – intellignt_idiot Aug 06 '17 at 14:49
  • But my alarm time is setting from my timepicker,I edited my question,please check again:) –  Aug 06 '17 at 14:59
  • My friend, but what about the time of rebooting ? Who selects the timer when the phone reboots ? After the phone reboots, you again set the alarm, at that time you are using INTERVAL DAY ? – intellignt_idiot Aug 06 '17 at 15:04
  • Your method alarmManager() it will always make the alarm go off instantly same time. – intellignt_idiot Aug 06 '17 at 15:06
  • Don't you save the last selected time from the date picker ? So that when phone reboots, you can get that time from sharedpreference, and set that time again to alarm. Are you getting my point ? – intellignt_idiot Aug 06 '17 at 15:07
  • I know that,so how can I get my time which I last selected to call alarm manager? Shoud I combine my receiver and my alarm() together? –  Aug 06 '17 at 15:13
  • Simple, whenever user selects the timePicker, just save the time in long data type in shared preferences. And if the user again changes time, just save the time again with the same key, so it will overwrite the last time and will always save the last selected time. Now next time when phone reboots, get the saved time (which will be in long data type) and set that inside calender (calendar.setTimeInMilliSec) and now you can set that time in my code's calendar and use 1000 * 60 *60 *24 instead of IntervalDay. Dont use that calender.Date - 1. It is wrong. – intellignt_idiot Aug 06 '17 at 15:21