2016-07-17 3 views
0

私はとしてAlarmManagerを使用している設定します。AlarmManagerは

final Intent myIntent = new Intent(this.context, AlarmReceiver.class); 
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
final Calendar calendar = Calendar.getInstance(); 
add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
calendar.set(Calendar.HOUR_OF_DAY, tp1.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, tp1.getCurrentMinute()); 


      myIntent.putExtra("extra", "yes"); 
      myIntent.putExtra("quote id", String.valueOf(quote)); 
      pending_intent = PendingIntent.getBroadcast(Addevent.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

      alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent); 
} 
}); 

時間が数時間に近く設定されている場合はスパンが9hrs、それのように大きい場合、これが正常に動作しますonReceive()アラームが作成されると、AlarmReceiverのメソッドがすぐに呼び出されます。どこが間違っていますか?

+0

そのコードはホックアップされています。 'calendar'の定義が間違った場所にあります... –

+0

Button onClickで使用していますが、コードを更新しました。今? – Sampada

+0

こんにちは。 'calendar'の定義はコードサンプルの最後の行の終わりです。私はそれが 'myIntent'の定義の近くにあると確信しています –

答えて

0

アラームマネージャーをスケジュールして翌日イベントをトリガーするコードスニペットを見つけてください。このことができます

public void scheduleAlarm(..) { 

      // time at which alarm will be scheduled here alarm is scheduled at 1 day from current time, 
      // we fetch the current time in milliseconds and added 1 day time 
      // i.e. 24*60*60*1000= 86,400,000 milliseconds in a day  
      Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000; 

      // create an Intent and set the class which will execute when Alarm triggers, here we have 
      // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when 
      // alarm triggers. 
      Intent intentAlarm = new Intent(this, AlarmReciever.class); 

      // create the object 
      AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

      //set the alarm for particular time 
      alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 

      Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show(); 

    } 

希望...

+0

しかし、私はアラームを設定する必要がある場合、これは動作しませんね、時間後またはそれはまだですか? – Sampada

+0

それは動作します。あなたはちょうど1 * 60 * 60 * 1000を更新する必要があります – Prashant

1

はあなたがsetへの呼び出しで使用したフラグに対応する時間の種類を使用する必要があります。 ELAPSED_REALTIME_...の2つのフラグは、ブート(elapsedRealTime)以降の時刻に対応しています。 2つのRTC_...フラグは、エポック(currentTimeMillis)以降の時間に対応します。

繰り返し偶数をスケジュールする場合は、AlarmManagerは過去にタスクをスケジュールしないことに注意してください。スケジューリングは異なるプロセスとの通信を必要とするため、時にはスケジュールに1ミリ秒以上かかることがあります。将来数ミリ秒未満の最初の発生をスケジュールすると、消えてしまうことがあります。追加する

編集:

最後に、あなたのコードが動作するようになっている(tp1に何である)が、あなたはCalendar方法set、ないaddを使用しているかは明らかではありません。 setは、単に名前付きフィールドの値を設定します。それは次のフィールドに「運び込まない」。

+0

私の要件のコードスニペットを提供できますか?また、私は1つ以上のアラームを設定する必要があり、tp1はTimePickerです。 – Sampada

+0

私は220ドル/時間を請求する。 –

+0

今は終わっていません! :D – Sampada