Noman Ahmed
Noman Ahmed

Reputation: 21

Multiple Alarms In Android Using Android Services

This is my save button in which i put alarm. But problem is if i add 1 alarm it works great but if i add an alarm next minute before last alarm has triggered it cancels last alarm and updates to new one, i tried changing request code but it isnt working for me

        save = (Button)findViewById(R.id.save_rem);
        save.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View arg0) 
            {
                title_txt = title.getText().toString();
                description_txt = description.getText().toString();
                id++;


                addRemInDb a = new addRemInDb();
                int v = a.insert(title_txt, hour, minute, mMonth, mDay, mYear, description_txt);
                //a.deleteTable();
                a.closeDB();
                if(v == -1)
                {
                    Toast.makeText(getApplication(), "Title & Time Must Not Be Same",Toast.LENGTH_LONG).show();
                }
                else
                {                                               
                    Calendar cal = Calendar.getInstance();

                    //Setting alarm to be triggered on specified date and time
                    cal.set(mYear, mMonth, mDay, hour, minute, seconds);

                    requestcode++; // Here request code is adding every time when i click in save button

                    Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);

                    alarmIntent.putExtra("title", title_txt);           //This is title for notification
                    alarmIntent.putExtra("note", description_txt);      //This is Description for notification
                    alarmIntent.putExtra("rq", requestcode);

                    PendingIntent sender = PendingIntent.getService(getApplicationContext(), requestcode, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);



                    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);                       

                    finish();


                }       

            }
        });

This is my AlarmReciever class

public class AlarmReceiver extends Service
{
@Override
public void onCreate() 
{

}

@Override
public void onStart(Intent intent, int startId) 
{
    super.onStart(intent, startId);     


    Bundle extras = intent.getExtras();

    String title=extras.getString("title");
    String note=extras.getString("note");
    int requestcode = extras.getInt("rq");


    NotificationManager nManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.notify, "Reminder", System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getService(getApplicationContext(), requestcode, new Intent(getApplicationContext(), AlarmReceiver.class), 0);

    notification.setLatestEventInfo(getApplicationContext(), note, title, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults = Notification.DEFAULT_SOUND;

    nManger.notify(NOTIFICATION_ID, notification);

}

Upvotes: 0

Views: 970

Answers (2)

Noman Ahmed
Noman Ahmed

Reputation: 21

The Requestcode im generating was not working as i added this line and used intent_id as my request code it worked perfectly fine

final int intent_id= (int) System.currentTimeMillis();

Upvotes: 0

Jon O
Jon O

Reputation: 6591

Try getting rid of FLAG_UPDATE_CURRENT on your PendingIntent.

Upvotes: 1

Related Questions