Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing intent extra to android broadcast receiver

I have created a AlarmReceiver class which is used as broadcast receiver for alarm. The issue is that I need to send some values from the class which is setting the alarm to the broadcast receiver class.

setAlarmManager.java

     Intent i = new Intent(mContext, AlarmReceiver.class);
     i.putExtra(KEY_ROWID, (long)taskId); 
     PendingIntent pi = PendingIntent.getBroadcast(mContext,taskId_int,i,PendingIntent.FLAG_ONE_SHOT); 
     mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);

I need to get the KEY_ROWID from intent in alarmreceiver class. How can I do that? The AlarmReceiver class is shown below.

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             //Here I need the values from intent using bundle or anything...

    }
}
like image 299
John Avatar asked Dec 08 '11 06:12

John


People also ask

What is the difference between intent and broadcast receiver?

An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.

How pass data from BroadcastReceiver to activity in Android?

Step 1. Open your project where you want to implement this. Step 2. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.


1 Answers

You can use :

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             String keyid = intent.getStringExtra("KEY_ROWID");
    }
}
like image 93
Binoy Babu Avatar answered Oct 16 '22 04:10

Binoy Babu