2017-08-06 77 views
0

工作後,我有一個問題我的告警管理, 我正在與timepickerdialog, 一個報警經理,但我想保持它重新啓動後的工作, 我試圖添加的Android報警經理不斷重啓

<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

+0

你的代碼在哪裏傳遞日曆到鬧鐘功能? –

+0

我編輯我的職務:) – ChienFeng

+0

檢查現有的答案: https://stackoverflow.com/questions/20065433/alarm-doesnt-trigger-after-reboot https://stackoverflow.com/questions/17941371/repeating-alarm -manager-後重啓 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

回答

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時觸發報警,並且啓動後不會立即觸發報警。但僅在午夜12點。

+0

它不爲我工作:( – ChienFeng

+0

這對我的作品。完美。 什麼不起作用? 當你使用這個代碼發生了什麼? –

+0

你能送我的代碼,我可以修復它 –