Search code examples
javaandroidsmsalarmmanager

How to avoid Alarm manager overriding with same intervals of time with different message using dynamically?


What happens if we select same intervals for two different message its not sending the respective manner,its taking the first message content and sending the second sms timing. I want to send different sms with same intervals using dynamically.

MyAlaramactivity.java

else if (mytime.equals("Every 5 Minutes")) {

                        editor1.putString("p1",edittextSmsNumber.getText().toString());
                        editor1.putString("m1",edittextSmsText.getText().toString());
                        editor1.commit();
                        edittextSmsNumber.setText(" ");
                        edittextSmsText.setText(" ");


                        Calendar calendar = Calendar.getInstance();

                        calendar.set(datep.getYear(), datep.getMonth(),
                                datep.getDayOfMonth(),
                                timep.getCurrentHour(),
                                timep.getCurrentMinute(), 0);
                        Intent myIntent1 = new Intent(AndroidAlarmSMS.this,
                                MyAlarmService.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("service", 1);
                        myIntent1.putExtras(bundle);
                        //myIntent1.putExtra("service", "s1");
                        myIntent1.setData(Uri.parse("timer:myIntent1"));
                        pendingIntent1 = PendingIntent.getService(
                                AndroidAlarmSMS.this, 1, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

                        AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
                        alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,
                                calendar.getTimeInMillis(), 1000 * 60 * 2,
                                pendingIntent1);




                        // Millisec * Second *
                                                // Minute
                    } else if (mytime.equals("Every hour")) {
                        editor2.putString("p2",edittextSmsNumber.getText().toString());
                        editor2.putString("m2",edittextSmsText.getText().toString());
                        editor2.commit();
                        edittextSmsNumber.setText(" ");
                        edittextSmsText.setText(" ");
                        Calendar calendar = Calendar.getInstance();

                        calendar.set(datep.getYear(), datep.getMonth(),
                                datep.getDayOfMonth(),
                                timep.getCurrentHour(),
                                timep.getCurrentMinute(), 0);
                        Intent myIntent2 = new Intent(AndroidAlarmSMS.this,
                                MyAlarmService.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("service", 2);
                        myIntent2.putExtras(bundle);
                        myIntent2.setData(Uri.parse("timer:myIntent2"));
                        pendingIntent2 = PendingIntent.getService(
                                AndroidAlarmSMS.this, 2, myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);

                        AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);

                        alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,
                                calendar.getTimeInMillis(), 1000 * 60 * 3,
                                pendingIntent2); 




This is my service class



public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  Bundle bundle = intent.getExtras();
  //String status= b.getString("service");
  status = bundle.getInt("service", 0);


  if(i==2)
   {
   i++;
   Toast.makeText(this, "Hiii", Toast.LENGTH_LONG).show();
   }
   else
   {
       String share_pref_file = "IMS";
          SharedPreferences prefs = getSharedPreferences(share_pref_file,
          Context.MODE_PRIVATE);
       if(status == 1)
       {
           String number = prefs.getString("p1", "");
           String message = prefs.getString("m1", "");
              //String message= prefs.getString("extraSmsText", "");

              Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
              Toast.makeText(this,
                     number,
                     Toast.LENGTH_LONG).show();

              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(number, null, message, null, null);  
       }
       else if (status == 2)
       {
           String share_pref_file1 = "IMS2";
              SharedPreferences prefs2 = getSharedPreferences(share_pref_file1,
              Context.MODE_PRIVATE);
           String number = prefs2.getString("p2", "");
           String message = prefs2.getString("m2", "");
              //String message= prefs.getString("extraSmsText", "");

              Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
              Toast.makeText(this,
                     number,
                     Toast.LENGTH_LONG).show();
              try{
                  SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(number, null, message, null, null);  
                      Toast.makeText(this, "Message sent successfully", Toast.LENGTH_LONG).show();
              }
              catch(Exception e)
              {
                  Toast.makeText(this,
                            "I will kill you enter correct number",
                             Toast.LENGTH_LONG).show(); 
              }


       }

Have to avoid alarammanager overriden for same intervals


Solution

  • i reviewed your code, as you want to set multiple alarams first you have to create alaram manager array and change your code to like this.. first you create one global integer variable with name fivemin

    else if (mytime.equals("Every 5 Minutes")) {
    fivemin=fivemin+1;
    
    AlarmManager[] alarmManager=new AlarmManager[24];
    
    
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    
    
    
    Intent myIntent1 = new Intent(AndroidAlarmSMS.this,
                                        MyAlarmService.class);
                                Bundle bundle = new Bundle();
                        bundle.putInt("service", 1);
                        myIntent1.putExtras(bundle);
    
    
                                myIntent1.setData(Uri.parse("timer:myIntent1"));
                                pendingIntent = PendingIntent.getService(
                                        AndroidAlarmSMS.this, fivemin, myIntent1,0);
    
                                alarmManager[fivemin] =  (AlarmManager) getSystemService(ALARM_SERVICE);        
                                alarmManager[fivemin].setRepeating(AlarmManager.RTC_WAKEUP,
                                        calendar.getTimeInMillis(), 1000 * 60 * 2,
                                        pendingIntent);
    
                                intentArray.add(pendingIntent);