带有datepickerdialog的Android警报管理器在重启后继续工作

时间:2017-08-06 11:06:51

标签: android android-intent alarmmanager

我的警报管理员有问题, 我正在使用timepickerdialog制作一个报警管理器, 但我希望在重启后继续工作 我试着添加

<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>    

在我的清单中,但是当我重新启动设备时,它会立即报警。

这是我的活动代码:

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);
}

这是我的收件人代码:

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());
    }

这是我将时间用于闹钟功能的代码:

 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();
        }
    };

如何在重启后保留警报管理器? PLZ

1 个答案:

答案 0 :(得分:0)

请勿使用Interval.Day。而是用户:

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);

这将在每天午夜12:00 AM触发警报,并且在启动后不会立即触发。但只在午夜12点。