2016-07-06 15 views
0

アラームアプリケーションを正常にセットアップしましたが、複数のアラームがある場合に問題が発生します。前に選択したアラームが上書きされます。複数のアラームを設定する

私のMainActivity.javaファイルには、保留中の意図ステートメントの近くに何を変更すればよいですか?

私はオンラインで検索しました。彼らは、forループを使用して保留中のインテントIDをインクリメントすると言っていましたが、動作していません。

助けがあれば助かります。

1を渡すの代わりに
**PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);** 

だけのためのユニークなIDを渡す - ここであなただけの次の行に少し変更を加える、すべての保留中の意図に異なるIDを定義する必要があり、私のcode.`

public void add() { 

    button_add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, timepicker.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, timepicker.getCurrentMinute()); 
      Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); 
       **PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);** 
       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
       am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
       Toast.makeText(MainActivity.this, "the alarm is set for " + timepicker.getCurrentHour() + " : " + timepicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); 
       myDb.insertRow(timepicker.getCurrentHour().toString(), timepicker.getCurrentMinute().toString()); 

     } 
    }); 
} 

答えて

4

ですあらゆる異なるアラーム。

public static int getAlarmId(Context context) { 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
     int alarmId = preferences.getInt("ALARM", 1); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putInt("ALARM", alarmId + 1).apply(); 
     return alarmId; 
} 

は、それが動作しますホープ:)

+0

私はリストビューでアラームを格納しています。私のためにコードを編集できますか?だから私は変数を初期化し、それを増分し、一意のIDとして設定しますか?私は昨日からこれに苦しんでいる。 –

+0

はいインクリメントが正常に機能するか、またはhhmmddmmyy intフォーマットを1358080714のように使用することができます。これは重大なアラームを回避しますが、高レベルの精度が必要ない場合はインクリメントのみに進みます: ) – Neo

+0

それは働いていません。私はuniqueid変数を初期化します。= 1、* PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this、uniqueid、intent、PendingIntent.FLAG_UPDATE_CURRENT); uniqueid ++ –

関連する問題