Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intent.putExtra() in pending intent not working

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.

like image 759
Arnab Avatar asked Apr 24 '15 11:04

Arnab


People also ask

What is the putExtra () method used with intent for?

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.

What is intent putExtra?

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.

What is the importance of the putExtra () method in android how it is different from setData ()?

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.


1 Answers

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); 
like image 146
ayesh don Avatar answered Sep 20 '22 19:09

ayesh don