2012-04-22 18 views
16

私は私の質問への回答のための記事を検索しましたが、私の問題を解決するものは見つかりませんでした。 1つのAlarmSettingsクラスを使用して3つの異なるアラームを設定しようとしています。 2つのアラームを設定すると、2番目のアラームが最初のアラームより優先され、最初のアラームはオフになりません。私はそれが私の未決の意図と関係しているかもしれないと思う...私は本当にアンドロイドには新しく、助けを大いに感謝するだろう。ここでは、アラームを設定するための私のコードは次のとおりです。あなたのアラームのidに0属性は、例えば次の3つのアラームを持ってandroid alarmmanager複数のアラーム、1つは他のアラームを上書きしますか?

public void setAlarm() { 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY, timepicker.getCurrentHour()); 
     calendar.set(Calendar.MINUTE, timepicker.getCurrentMinute()); 
     calendar.set(Calendar.SECOND, 0); 

     if (timepicker.getCurrentHour() < calendar.get(Calendar.HOUR_OF_DAY)) { //if the alarm hour is less than the current hour 
      calendar.add(Calendar.DATE, 1);          //then add 24 hours (1 DATE or Day)     
     } 

     //Create the text that we want to set the TextView alarmtime to in Main 
     StringBuilder sb = new StringBuilder(); 
     if (timepicker.getCurrentHour() > 12) { 
      sb.append(timepicker.getCurrentHour() - 12); 
     } else { 
      sb.append(timepicker.getCurrentHour()); 
     } 
     sb.append(":"); 
     sb.append(timepicker.getCurrentMinute()); 
     sb.append(" "); 
     if (timepicker.getCurrentHour() > 12) { 
      sb.append("pm"); 
     } else { 
      sb.append("am"); 
     } 

     if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 1) { 
      ((GlobalVariables)getApplication()).setAlarm1Cal(calendar); 
      Main.alarmTime1.setText(sb); 

      Intent intent1 = new Intent(AlarmSettings.this, AlarmReceiver.class); 
      intent1.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited()); 
      PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_ONE_SHOT); 

      alarmmanager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1); 

     } else if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 2) { 
      ((GlobalVariables)getApplication()).setAlarm2Cal(calendar); 
      Main.alarmTime2.setText(sb); 

      Intent intent2 = new Intent(AlarmSettings.this, AlarmReceiver.class); 
      intent2.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited()); 
      PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, intent2, PendingIntent.FLAG_ONE_SHOT); 

      alarmmanager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2); 

     } else if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 3) { 
      ((GlobalVariables)getApplication()).setAlarm3Cal(calendar); 
      Main.alarmTime3.setText(sb); 

      Intent intent3 = new Intent(AlarmSettings.this, AlarmReceiver.class); 
      intent3.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited()); 
      PendingIntent pendingIntent3 = PendingIntent.getActivity(getApplicationContext(), 0, intent3, PendingIntent.FLAG_ONE_SHOT); 

      alarmmanager3.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent3); 
     } 

     finish(); 

     Toast.makeText(getApplicationContext(), "system time: " + System.currentTimeMillis() + "\n" + "picked time: " + calendar.getTimeInMillis(), Toast.LENGTH_LONG).show();  
    } 

答えて

40
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_ONE_SHOT); 

変更、

は0,1,2で上記のコードを繰り返し。

このようにして、互いにオーバーライドしません。

+2

ありがとうございました。 – user1349984

+1

あなたは、同じ問題を抱えている他の人々にとって明白で役立つ答えとしてマークすることができます。コーディングで幸運! –

+1

[このような提案された編集](http://stackoverflow.com/review/suggested-edits/950604)を承認しないでください。私のコメントを参照してください。 – hims056

関連する問題