Search code examples
androidbroadcastreceiveralarmmanagerandroid-broadcast

Create multiple AlarmManager for the broadcast receiver?


I am calling a BroadcastReceiver through AlarmManager And It works fine for me.

But

What I need this time

I want to create multiple AlarmManager which call the same BroadcastReceiver Class.

What is the way to do this process?


Solution

  • You can do that by Setting alarms with a different request code for the PendingIntent used. Make sure every alarm will have a unique request code. Then receive them with a single broadcast receiver.

    Another thing to be kept in mind, if device reboots then pending intents no longer exists. So, you have to use another receiver for determining if a device reboots. You have to save your unique request codes, then When the device reboots you have to restore your alarms with the same request codes. Hope it helps.

    Edit:

    BroadCastReceiver

    package com.your.package;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String Noti_title = intent.getExtras().getString("title");
            String Noti_message = intent.getExtras().getString("notes");
            int Noti_code = intent.getExtras().getInt("code");
            Log.d("AlarmReciever", Noti_title + " " + Noti_message);
            Intent myIntent = new Intent(context, NotificationService.class);
            myIntent.putExtra("title", Noti_title);
            myIntent.putExtra("notes", Noti_message);
            myIntent.putExtra("code", Noti_code);
            context.startService(myIntent);
        }
    }
    

    Service :

    package com.your.package;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.media.Ringtone;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.IBinder;
    
    public class NotificationService extends Service {
    
        private NotificationManager mManager;
    
        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @SuppressWarnings({ "static-access", "deprecation" })
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
    
            String Noti_title = intent.getExtras().getString("title");
            String Noti_message = intent.getExtras().getString("notes");
            int Noti_Code = intent.getExtras().getInt("code");
    
            mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    
            Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
    
            Notification notification = new Notification(R.drawable.ic_launcher , Noti_title , System.currentTimeMillis());
            intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            notification.setLatestEventInfo(this.getApplicationContext(),Noti_title , Noti_message , pendingNotificationIntent);
            notification.vibrate = new long[] { 100L, 100L, 200L, 500L };
            mManager.notify(Noti_Code , notification);
            try {
                Uri notification_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification_uri);
                r.play();
            } catch (Exception e) {}
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
    
    }
    

    Usage :

    public void SetAlarm(Calendar calendar, int reqCode) {
            String dateName = idea.getText().toString();
            String dateNote = note.getText().toString();
            Log.d("SetAlarm Texts", "Date : " + dateName + " Note: " + dateNote);
    
            Intent myIntent = new Intent(mActivity, AlarmReceiver.class);
            myIntent.putExtra("title", "Her : " + dateName);
            myIntent.putExtra("notes", dateNote);
            myIntent.putExtra("code", reqCode);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity,
                    reqCode, myIntent, 0);
            AlarmManager alarmManager = (AlarmManager) mActivity
                    .getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
                    pendingIntent);
        }