0

Hi, I have a problem with my code.I typed my code to be work on a multiple alarms every alarm take 10 seconds to be triggered REPEATEDLY.My problem is that the alarm works with just one value but although I've put a different requestCode,but it doesn't work or I don't know how I can call every one separately .

I've tried to do this:

  • alarm1 with a value and a request code 0 (for example) for 10 seconds

  • alarm2 with a different value and a request code 1 (for example) for 10 seconds

The code does: Display Alarm2 that I selected last.

    static int HELLO_ID = 1;
    boolean flag = false;
    int CountMultipleAlarm = 0;
    EditText edt,edt2;
    Button btn;
    CountDownTimer timer;

    //the strings of the notifications

    String titlePills = "Time to take Panadol",DescriptionPills  = "Panadol";
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           Spinner spinner = (Spinner) findViewById(R.id.spinner1);
           ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
     R.array.planets_array, android.R.layout.simple_spinner_item);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


     btn = (Button)findViewById(R.id.button1);
     edt = (EditText)findViewById(R.id.editText1);


     //hide the button and the edit text


     btn.setVisibility(View.GONE);
     edt.setVisibility(View.GONE);
     btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final String edittext= edt.getText().toString();  
                    Pattern pat= Pattern.compile("[0-9]");
                 // Pattern pat= Pattern.compile("[a-zA-Z0-9]");
                    Matcher matcher = pat.matcher(edittext);  


                     //*************Timer Start ******************
                     //11000 = 10 seconds(11000*6*60 == 1hour)


                     int count = 11000;
                     timer = new CountDownTimer(count, 1000) 
                     {public void onTick(long millisUntilFinished) 
                          {
                            long scnds=0;
                              scnds=(millisUntilFinished/1000);
                          }
                          public void onFinish() 
                          //****** Alarm Code ********
                            Calendar cal = Calendar.getInstance();      
                         // for using this you need to import java.util.Calendar;

                             AlarmManager am =             (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);

                             ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
                                 //the title and the description of the notification


                             Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
                                  alarmintent.putExtra("title",titlePills + "value");
                                  alarmintent.putExtra("note","value");
                                  //HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;

                                  //example:protected static int HELLO_ID =1;
                                  PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
                                  alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
                                  //VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to 
                                  //AlarmReceiver Class
                                        // Get the AlarmManager service

                                  am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

                                //Alarm coooooode end **************************************
                                   intentArray.add(sender);

                                timer.start();
                          }
                   }
                     //************* Timer Ends *************
        }
    });

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {

         getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }



     }import


//Edit from Paramone:  What comes after " }import "  ???

alarm_Receiver class

    public class Alarm_Receiver extends BroadcastReceiver {


String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager mNotificationManager = (NotificationManager)     context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationManager manger = (NotificationManager)     
          context.getSystemService(Context.NOTIFICATION_SERVICE);


       Notification notification = new Notification(R.drawable.ic_launcher,   notification1,
     System.currentTimeMillis());


   PendingIntent contentIntent = PendingIntent.getActivity(context, 
    NOTIFICATION_ID, 
     new Intent(context, Alarm_Receiver.class), 0);


    Bundle extras=intent.getExtras();
    String title=extras.getString("title");


          //here we get the title and description of our Notification
    String note=extras.getString("note");
    notification.setLatestEventInfo(context, note, title, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.DEFAULT_SOUND;


          //here we set the default sound for our notification


    // The PendingIntent to launch our activity if the user selects this  notification
    manger.notify(NOTIFICATION_ID++, notification);



}

        };
Paramone
  • 2,634
  • 4
  • 31
  • 58

0 Answers0