2016-03-27 67 views
0

アラームマネージャがあります。 1日に1回は動作する必要があります。タスクからアプリを削除したり、デバイスを再起動したりすると、アラームが存在する必要があります。しかし、私がデバイスを再起動すると、アラームは存在しません。タスクからアプリを削除すると、アラームも存在しません。AlarmManagerが機能しない

MainActivity:

@Override 
    protected void onResume() { 
      if (isAlarmEnableded) { 
      if (!isAlarmSheduled) { 
       setAlarm(); 
       isAlarmSheduled = true; 
       putInSharedPreferences(Boolean.toString(isAlarmSheduled)); 
      } 
     } else { 
      cancelAlarm(MainActivity.this); 
      isAlarmSheduled = false; 
      putInSharedPreferences(isAlarmSheduled); 
     } 

     super.onResume(); 
    } 

isAlarmEnableded - SwitchPreference =真。 isAlarmEnableded - 私はアラームをオフにします。 isAlarmSheduled - 私が設定で保存する値。アラームの状態を表示します

private void setAlarm() { 

     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(context, AlarmReceiver.class); 
     intent.setAction(Constants.ALARM_INTENT); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
       Constants.PENDINGINTENT_REQUEST_CODE, intent, 0); 

     if (android.os.Build.VERSION.SDK_INT >= 19) { 
      alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, setAlarmTime(), 
        AlarmManager.INTERVAL_DAY, pendingIntent); 
     } else { 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setAlarmTime(), 
        AlarmManager.INTERVAL_DAY, pendingIntent); 
     } 

private long setAlarmTime() { 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 

     calendar.set(Calendar.HOUR_OF_DAY, 20); 
     calendar.set(Calendar.MINUTE, 48); 
     calendar.set(Calendar.SECOND, 0); 

     if (System.currentTimeMillis() > calendar.getTimeInMillis()) { 
      calendar.add(Calendar.DAY_OF_YEAR, 1); 
     } 
     return calendar.getTimeInMillis(); 
    } 

現在時刻>予定時刻であれば、明日のアラームを設定します。

Constants.ALARM_INTENT - "com.blabla.AlarmReceiver"

AlarmReceiver

@Override 
    public void onReceive(Context context, Intent intent) { 
     intent.setClass(context, NotificationActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    } 

NotificationActivityは、アラーム時にダイアログを表示します。 また、setAlarm()メソッドを呼び出し、isAlarmScheduledを環境設定に入れたDeviceBootReceiverを持っています。 マニフェスト

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<receiver android:name="com.mypackage..AlarmReceiver"> 
      <intent-filter> 
       <action android:name="com.blabla.AlarmReceiver" /> 
      </intent-filter> 
     </receiver> 

     <receiver 
      android:name="com.mypackage.DeviceBootReceiver"> 

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

私はadb shell dumpsys alarm > dump.txtでアラームをチェックしています。どのような場合でも1日に1回しか動作しない安定したアラームが必要です。アプリをシャットダウンして再起動すると、アラームは同じになるはずです アラームはすべての場合に機能しますが、デバイスを再起動したり、タスクからアプリを削除したりするときには機能しません。私のコードにはいくつか間違いがあります。あなたは、デバイスを起動した後、あなたのアラームを初期化するために、ブート受信機を処理する必要がありますおかげ

答えて

0

/私が間違っているのか知っている私はいけないので、私は私の問題を解決するのに役立ち、何かのように:あなたは、ブートを追加する必要があります

public class BootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     List<Note> notes = dbHelper.getAllNotesAlarms(); 
    for (Note note : notes) { 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(note.getAlarm()); 
     Intent intent = new Intent(App.getAppContext(), AlarmService.class); 
     intent.putExtra("NOTE_ID", note.getId()); 
     PendingIntent pendingIntent = PendingIntent.getService(App.getAppContext(), 
       (int) note.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager am = 
       (AlarmManager) App.getAppContext().getSystemService(Activity.ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
       pendingIntent); 
    } 
    } 

} 
+0

私は知っている。私のレシーバーはあなたのように見えます。setAlarm()メソッドを呼び出し、isAlarmScheduledをプリファレンスに配置します。しかし、それは仕事ではありません – Jackky777

+0

com.mypackage.DeviceBootReceiverクラスについては、クラスファイルを送信することができます –

+0

protected void onResume(){setAlarm(); isAlarmSheduled = true; putInSharedPreferences(Boolean.toString(isAlarmSheduled));} – Jackky777