Click here to Skip to main content
13,869,132 members
Rate this:
Please Sign up or sign in to vote.
See more:
I write this program for set alarms and I want that this alarms send notification for user in special time.I use AlarmManager and BroadcastReceiver. But it just work for one alarm and just SetAlarm(20) works and send notification. How can I use alarm and notification more than one and for several time?

This is my alarm method And my receiver:

What I have tried:

public class Main3Activity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {



    private void SetAlarm(int sec) {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, sec);

        long time = cal.getTimeInMillis();

        Intent i = new Intent(Main3Activity.this, MyReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(Main3Activity.this, 0, i, 0);

        am.set(AlarmManager.RTC_WAKEUP, time, pi);



public class MyReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        showNotification(context,Main3Activity.class,"Test Time","do it now");


    public static void showNotification(Context context,Class<?> cls,String title,String content)


        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent notificationIntent = new Intent(context, cls);


        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);



        PendingIntent pendingIntent = stackBuilder.getPendingIntent(


        NotificationCompat.Builder builder = new     NotificationCompat.Builder(context);

        Notification notification = builder.setContentTitle(title)




        NotificationManager notificationManager = (NotificationManager)


        notificationManager.notify(DAILY_REMINDER_REQUEST_CODE, notification);

Updated 6-Dec-18 19:22pm
David Crow 16-Mar-18 22:53pm
From the documentation:

If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.

So, your 10 second alarm will not run as expected.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

From the documentation it says
When you set a second alarm that uses the same pending intent, it replaces the original alarm.

So you shall use different BroadcastIds for the pending intent here

int ticks = (int) System.currentTimeMillis();
PendingIntent pi = PendingIntent.getBroadcast(Main3Activity.this, ticks, i, 0);

Hope this helps!!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

PendingIntent pendingIntent = PendingIntent.getBroadcast(Disease_Credentials.this,_id,intent2,PendingIntent.FLAG_ONE_SHOT);

at the instead of 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190214.1 | Last Updated 7 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100