2016-11-03 9 views
2

5分ごとにアラームを発生させようとしています。予定アラームがレシーバクラスを呼び出すことはありません

これは、アラームを設定するためのコードです:

@Override 
public void scheduleAlarmManager() { 
    Timber.i("After SignIn sets AlarmManager"); 
    // broadcast 
    Intent intent = new Intent(this, PatientAlarmReceiver.class); 
    intent.setAction(PATIENT_START_ALARM_ACTION); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      this, REQUEST_CODE, intent, 0); 

    // and set alarmManager 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 

    Calendar currentCal = Calendar.getInstance(); 
    long currentTIme = currentCal.getTimeInMillis(); 

    // if there's not an Alarm already set then set one 
    if (!isAlarmSet(this)) { 
     Timber.i("Alarm not set - so set one"); 
     alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent); 
    } 

} 

と私は私のlogcatに私はTimberでログメッセージを参照してくださいので、私は正しくアラームを設定することを確認することができます。

public class PatientAlarmReceiver extends BroadcastReceiver { 

public static final String TAG = "PATIENT-ALARM-RECEIVER"; 
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.i(TAG, "Inside OnReceive Patient"); 
    Timber.i("Inside OnReceive Patient"); 

    if (intent == null || intent.getAction() == null) { 
     return; 
    } 

    String action = intent.getAction(); 
    if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) { 
     onStartCheckForConnectionRequest(context); 
    } 
} 

/** 
* If is connected to network starts services 
*/ 
private void onStartCheckForConnectionRequest(Context context) { 
    NetworkUtils networkUtils = new NetworkUtils(context); 
    if (networkUtils.isNetworkConnected()) { 
     Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class); 
     context.startService(checkForConnRequestIntent); 
     Timber.i("Starts Service From PatientALARMMANAGER"); 
    } 
} 

}

そして私はマニフェストで宣言されています:

マイレシーバクラスがある。また

 <!-- Receivers --> 
    <receiver 
     android:name="bp.headsup.receivers.PatientAlarmReceiver" /> 

私は実行する場合:adb shell dumpsys alarm は、私が見ることができます:

ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock} 
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}} 

上記のレスポンスのモックは私が使っているsourceSetです。これと何か関係があるかどうかは分かりません。

問題は、私がReceiverクラスのonReceiveにあるメッセージをlogcatで読み取ることがなく、明らかにサービスが開始されないことです。誰でもそれを助けることができますか?私はkitKat 4.4(api 19)で動作するデバイスを使用していますが、エミュレータでも試してみましたが、結果は同じでした。

+0

私はあなたがmenifestで受信者の行動(BOOT_COMPLETED)を設定していないと思います。 –

+1

あなたは経過時間アラームを設定していますが、「壁時計」の開始時間を与えています。アラームを 'RTC'型に変更するか、' SystemClock.elapsedRealtime() 'から開始時刻を取得してください。 –

+1

@MikeM。正しい!出来た!ありがとう – Mes

答えて

1

ELAPSED_REALTIMEアラームを設定しています。これは、前回の起動以降の時間に基づいています。しかし、あなたは "壁時計"に基づいて開始時間を過ぎているので、アラームは実際にはかなり遠い将来に設定されます。

アラームをRTCタイプに変更するか、開始時刻をSystemClock.elapsedRealtime()から取得することができます。記述された振る舞いを考えると、経過時間のタイプを維持し、開始時間を修正することが適切と思われます。

関連する問題