2012-04-22 17 views
16

मैंने अपने प्रश्न के उत्तर के लिए पोस्ट खोज ली हैं लेकिन मेरी समस्या हल करने वाली कुछ भी नहीं मिली है। मैं एक अलार्मसेटिंग कक्षा का उपयोग कर 3 अलग अलार्म सेट करने की कोशिश कर रहा हूं। जब मैं दो अलार्म सेट करता हूं, तो दूसरा व्यक्ति पहले पर प्राथमिकता लेता है और पहला कभी नहीं जाता है। मुझे लगता है कि इसे मेरे लंबित इरादे से करना पड़ सकता है ... मैं एंड्रॉइड के लिए वास्तव में नया हूं और मदद की सराहना करता हूं। यहाँ अलार्म सेट करने के लिए मेरे कोड है:एंड्रॉइड अलार्ममेनगर एकाधिक अलार्म, एक दूसरे को ओवरराइट करता है?

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 विशेषता, उदाहरण के लिए आप तीन अलार्म,

दोहराने 0,1,2 के साथ ऊपर कोड है ।

इस तरह वे एक-दूसरे को ओवरराइड नहीं करेंगे।

+2

धन्यवाद, ऐसा हुआ। – user1349984

+1

आपका स्वागत है, आप इसे अपनी समस्या के साथ अन्य लोगों के लिए स्पष्ट और सहायक होने के उत्तर के रूप में चिह्नित कर सकते हैं। कोडिंग में शुभकामनाएँ! –

+1

कृपया [ऐसे सुझाए गए संपादन] को स्वीकार न करें (http://stackoverflow.com/review/suggested-edits/950604)। मेरी टिप्पणी देखें। – hims056

संबंधित मुद्दे