Akshay Choulwar
Akshay Choulwar

Reputation: 34

How to set multiple alarms in android,last alarm overwrites preveious alarms in this case

I am developing a android applications where user can set reminder times but i am facing one problem in following code which last alarm overrides the all previous alarms..

MainActivity.java

public void setreminders()
{
    DatabaseHandler db=new DatabaseHandler(this);
    ArrayList<Tablet> tt=db.getAllContacts();
    int ijk=tt.size();
    Calendar[] arr=new Calendar[ijk];
    int i=0;
    //AlarmManager[] alarmManagers=new AlarmManager[ijk];
    //ArrayList pintt=new ArrayList<PendingIntent>();
    Intent alarmintent=new Intent(this,AlarmReceiver.class);
    pint=PendingIntent.getBroadcast(this,0,alarmintent,0);
    alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    for(Tablet val:tt)
    {
        Log.w("timings",val.getTime());
        String pps=val.getTime();
        int h=Integer.parseInt(pps.substring(0,2));
        int m=Integer.parseInt(pps.substring(3,5));
        Log.w("hour",String.valueOf(h));
        Log.w("minute",String.valueOf(m));
        arr[i]=Calendar.getInstance();
        arr[i].set(Calendar.HOUR_OF_DAY,h);
        arr[i].set(Calendar.MINUTE,m);
        long intend=arr[i].getTimeInMillis();
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,intend,AlarmManager.INTERVAL_DAY,pint);
        i=i+1;
    }

Upvotes: 2

Views: 930

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

You need to use unique id´s for your PendingIntent:

pint=PendingIntent.getBroadcast(this,id,alarmintent,0);

So be sure that the second paramter of PendingIntent, the requestCode, is used only once. Also, you should implement a logic, that you can cancel the alarm every time. For canceling, you have to use the same id.

Upvotes: 3

Related Questions