Click here to Skip to main content
13,800,574 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 {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        SetAlarm(10);
        SetAlarm(20);


    }


    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 {



    @Override
    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);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);




        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        stackBuilder.addParentStack(cls);

        stackBuilder.addNextIntent(notificationIntent);




        PendingIntent pendingIntent = stackBuilder.getPendingIntent(

                DAILY_REMINDER_REQUEST_CODE,PendingIntent.FLAG_UPDATE_CURRENT);




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

        Notification notification = builder.setContentTitle(title)

                .setContentText(content).setAutoCancel(true)

                .setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher_round)

                .setContentIntent(pendingIntent).build();



        NotificationManager notificationManager = (NotificationManager)

                context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(DAILY_REMINDER_REQUEST_CODE, notification);

    }
Posted 6-Mar-18 23:37pm
Updated 6-Dec-18 19:22pm
Comments
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
 
good
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!!!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

Add
,PendingIntent.FLAG_ONE_SHOT
at the instead of 0
  Permalink  

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
Web04 | 2.8.181215.1 | Last Updated 7 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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