2017-02-28 11 views
0

私はカスタム/マニュアル時間でAlarmManagerによってトリガーされるサービスを作成しました。私が設定した場合、AlarmManagerは02.15 PMで実行されます。時には02.10またはその時刻より前に実行されることもあります。誰でも私を助けることができます。Android:AlarmManager premature trigger(Min API 14)

、これは時間を設定するための私のコードです:

Calendar now = Calendar.getInstance(); 
    int minute = TimerFormatHelper.getMinute();  
    now.set(Calendar.HOUR, now.get(Calendar.HOUR)); 
    now.set(Calendar.MINUTE, minute); 
    now.set(Calendar.SECOND, 0); 
    return now.getTimeInMillis(); 
    //it will return timestamp for time that set 

と、このアラームを設定するコード:ドキュメントを1としてalarManager.set()

の代わりに

AlarmManager alarmManager = (AlarmManager)  getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    long startTime = HereTimestampThatGenerateBefore; 
    Intent intentApp = new Intent(getApplicationContext(), TheServiceHere.class); 
    PendingIntent pendingIntentApp = PendingIntent 
      .getService(getApplicationContext(), 0, intentApp, PendingIntent.FLAG_ONE_SHOT); 
    alarmManager.set(AlarmManager.RTC, startTime, 
      pendingIntentApp); 

答えて

0

使用alarmManager.setExact()方法:

このメソッドはset()と似ていますが、OSが の配信時間を調整することはできません。アラームは、要求されたトリガ時間をできるだけ近くで に配信します。注:API 19(KITKAT)のアラーム の配信は不正確です:OSは、 ウェイクアップとバッテリの使用を最小限に抑えるためにアラームをシフトします。厳しい配送保証が必要なアプリケーション をサポートする新しいAPIがあります。 setWindow(int、long、long、 PendingIntent)およびsetExact(int、long、PendingIntent)を参照してください。アプリケーション のtargetSdkVersionがAPI 19より前の場合は、 が要求されたときにすべてのアラームが正確に配信される以前の動作である が引き続き表示されます。

if (android.os.Build.VERSION.SDK_INT >= 19) { 
    alarmManager.setExact(...); 
} else { 
    alarmManager.set(...); 
} 
+0

SetExact()を使用すると、バッテリの使用を最小限に抑えることができます。 –

+0

min APIは19、私のアプリケーションmin APIは14 – muhwid

+0

なのでsetExactは使えません。残念ながら、setExact()はapi 19+でのみ使用できますので、14から18まではset()を使って不正確になります。私は私の答えを編集します。 – CoderP

0

わずか1または2秒でアラームをトリガーするnow.set(Calendar.SECOND, 0);を設定します。 now.set(Calendar.SECOND, 1);

アラームが2:15に設定されている場合、時間が2:15:1になるとすぐにトリガーされます。

alarmManager.set(AlarmManager.RTC, startTime, 
     pendingIntentApp); 

そして、ここでtargetCalがTimePickerDialog

private TimePickerDialog.OnTimeSetListener timePickerListener = 
     new TimePickerDialog.OnTimeSetListener() { 
      public void onTimeSet(TimePicker view, int selectedHour, 
            int selectedMinute) { 

       Calendar calNow = Calendar.getInstance(); 
       Calendar calSet = (Calendar) calNow.clone(); 

       calSet.set(Calendar.HOUR_OF_DAY, selectedHour); 
       calSet.set(Calendar.MINUTE, selectedMinute); 
       calSet.set(Calendar.SECOND, 0); 
       calSet.set(Calendar.MILLISECOND, 0); 

       if(calSet.compareTo(calNow) <= 0) { 
        //Today Set time passed, count to tomorrow 
        calSet.add(Calendar.DATE, 1); 
       } 

       setAlarm(calSet); 

      } 
     }; 

から来て、あなたはあなたが見つけることができるSetAlarm方法でアラームを設定することができます。

+0

私はすでに分を0で設定しましたが、影響を受けていませんか? – muhwid

+0

それは0に設定しないでください。 –

0

この

alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

の代わりに使用してみてくださいあなたの受信機

private void setAlarm(Calendar targetCal){ 

    mSetTime.setText(
      "\n\n***\n" 
        + "Alarm is [email protected] " + targetCal.getTime() + "\n" 
        + "***\n"); 

    Intent intent = new Intent(getBaseContext(), MyReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);