I am passing a pending intent through alarmreceiver, from a service class. But, after the pendingIntent fires, the intent.putExtra() information is not being received by the broadcastreceiver class. Here is my code for firing the pendingIntent
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT); aint.putExtra("msg", msg); aint.putExtra("phone", phone); alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
The alarm receiver class is below
public String msg, phonen; @Override public void onReceive(Context context, Intent intent){ Bundle extras = intent.getExtras(); msg = extras.getString("msg"); phonen = extras.getString("phone"); Log.d("onReceive", "About to execute MyTask"); Toast.makeText(context,msg, Toast.LENGTH_LONG).show(); }
The msg information in toast, that is being received from pending intent, is not being shown. Instead, a blank toast is shown.
Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.
Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity . putExtra() adds extended data to the intent.
putExtra allows you to add primitive (or parcelable) key-value pairs. setData is limited to passing an Uri . setData is conventionally used for the case of requesting data from another source, such as in startActivityForResult. but an uri can be sent through putextra also.
Try this
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class); aint.putExtra("msg", msg); aint.putExtra("phone", phone); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), id, aint, // as stated in the comments, this flag is important! PendingIntent.FLAG_UPDATE_CURRENT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With