Search code examples
androidbroadcastreceiveralarmmanager

2 receiver call using single alarm manager object


I have two Broadcast Receivers :

  1. Sending SMS.

  2. Manage notification.

I have to call both receivers at a specified time.

How can i do that?? is it possible?

The code for calling notification alarm is given below:

AlarmManager alarmManager=(AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        Intent intent=new Intent(context,specialNotification.class);
        Bundle bu=new Bundle();
        bu.putString("TITLE", title);
        bu.putString("BODY", body);
        bu.putInt("ICON", icon);
        bu.putLong("TIME", t);
        intent.putExtras(bu);
        //Toast.makeText(context, ""+_id, 3000).show();
        //Toast.makeText(context, "REpeat Alarm", 3000).show();
        PendingIntent pi=PendingIntent.getBroadcast(context, _id, intent, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, t, MILLISECONDS_IN_YEAR, pi);

Please help me to call SMS receiver

Thanks in advance..


Solution

  • I believe you can create a new Intent for your SMS broadcast receiver, and PendingIntent, passing it a different id value than the one you passed to the PendingIntent of your notification.

    AlarmManager alarmManager=(AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    Intent notifIntent = new Intent(context,specialNotification.class);
    Intent smsIntent = new Intent(context,Your_SMS_Receiver.class);
    
    Bundle bu=new Bundle();
    bu.putString("TITLE", title);
    bu.putString("BODY", body);
    bu.putInt("ICON", icon);
    bu.putLong("TIME", t);
    intent.putExtras(bu);
    
    PendingIntent notifPI = PendingIntent.getBroadcast(context, NOTIFICATION_ID, notifIntent, 0);
    PendingIntent smsPI = PendingIntent.getBroadcast(context, SMS_ID, smsIntent, 0);
    
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, t, MILLISECONDS_IN_YEAR, notifPI);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, t, MILLISECONDS_IN_YEAR, smsPI);