2016-05-31 7 views
1

私は2つのアラームが設定されています.1つは通知用で、もう1つはいくつかのタスクを実行します。私の問題は、アラームが1つしか動作していないようだ(通知サービス1、最初のアラームセット)。他のアラームは決して消えません。ここに私のコードです:保留中のインテントが2つあるアラームマネージャーは1つだけですか?

Intent myIntent1 = new Intent(getApplicationContext(), NotificationService.class); 
     PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent1, 0); 
     AlarmManager alarmManager1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     Calendar calendar1 = Calendar.getInstance(); 
     calendar1.setTimeInMillis(System.currentTimeMillis()); 
     long frequency1 = 30 * 1000; // in ms 
     alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), frequency1, pendingIntent); 

     // Set alarm to fire go to Next day everyday at the same time 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM 
     calendar.set(Calendar.MINUTE, 57); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     Intent myintent = new Intent(getApplicationContext(), AlarmNextDayService.class); 
     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent pi = PendingIntent.getService(getApplicationContext(), 11, myintent,0); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi); 

何か提案は大歓迎です。私は他のソースを見てきましたが、今まで私にとっては何も働いていません。 私も次のようにマニフェストファイルにアラームpermisisonを追加しました:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 

はあなたがHOUR_OF_DAYとMINUTEを設定しているが、それらあなたがいることがsetTimeInMillis(システムを呼び出すことによって上書きあなた

+1

それぞれのインテントに一意の識別子を追加しようとしましたか?Intent intent = new Intent( "uniqId"、null、context、Receiver.class); –

+1

マニフェストに2番目の 'Service'がリストされていますか?また、時と分を設定した後、 'Calendar'インスタンスを現在の時刻にリセットしていることに気付いていますか?不正確なアラームは、その間隔ではかなり離れている可能性があることにも留意してください。 –

+1

が問題だったことが判明した@MikeM。あなたはそれを答えとして書くことができますか? – LoveMeow

答えて

1
Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM 
    calendar.set(Calendar.MINUTE, 57); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 

ありがとうございます。 currentTimeMillis());

その後、すでにアラームが設定されているので、アラームはキャンセルされています。これは、以前のcalendar.getTimeMillis()の値でアラームを設定したと思います。

1

Serviceは、節電のためにアラームによってトリガされたときに動作することが保証されていないため、問題が発生する可能性が最も高いです。そのアラームがオフになったときにデバイスがバッテリとアイドル状態の場合、次回のデバイスの電源投入時またはAC電源投入時までトリガされません。ウェイクロックを保持するBroadcastReceiverを使用する必要があります。ウェイクロックは、完了するとServiceによって解放されます。 WakefulBroadcastReceiverはこれを少し扱いやすくします。このarticleは詳細を提供するのに役立ちます。

関連する問題