I am using PendingIntent. I need to pass a value. Intent uses putExtra. Is there an equivalent for PendingIntent? If yes, please provide sample example. Thanks in advance.
I am using:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
Just put the extras in the original intent, i.e.
Intent i = new Intent(context, MainActivity.class);
i.putExtra("key1", "the answer");
i.putExtra("key2", 42);
...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);
The "inner" intent is the one your Activity will actually receive. Check the documentation for PendingIntent.getActivity().
Then, in MainActivity.onCreate():
Intent intent = getIntent();
String strValue = intent.getStringExtra("key1");
int intValue = intent.getIntExtra("key2");
...
Related
Intent intent1 = new Intent(context, RecipeDetailActivity.class);
Intent intent2 = new Intent(context, RecipeDetailActivity.class);
Intent intent3 = new Intent(context, RecipeDetailActivity.class);
Intent intent4 = new Intent(context, RecipeDetailActivity.class);
intent1.putExtra("id", (long) 1);
intent1.putExtra("name", "Nutella Pie");
intent2.putExtra("id", (long) 2);
intent2.putExtra("name", "Brownies");
intent3.putExtra("id", (long) 3);
intent3.putExtra("name", "Yellow Cake");
intent4.putExtra("id", (long) 4);
intent4.putExtra("name", "Cheesecake");
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 1, intent3, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 1, intent4, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.tv_recipe1, pendingIntent1);
views.setOnClickPendingIntent(R.id.tv_recipe2, pendingIntent2);
views.setOnClickPendingIntent(R.id.tv_recipe3, pendingIntent3);
views.setOnClickPendingIntent(R.id.tv_recipe4, pendingIntent4);
EDIT: I put different request codes and used OnNewIntent method in the activity. Now works.
I have 4 views in a widget. If I press on the first one, I should open the activity with id = 1, but if second, then open the activity with id = 2, and so on.
views is a RemoteViews object. The RecipeDetailActivity will act on showing data depending on the intent data (id and name). That activity contains a fragment, and when created, set the data to the fragment, then the fragment will update its information async.
It's not working, as clicking on all of them will yield the same result (currently at id = 3). Shouldn't they cancel each other so that when I open a new one give a new intent (or open a completely new activity)?
I am very new to Android Programming. I would like to know if it is possible to add extra parameters using PendingIntent ?
For example I wanna do something like intent.putExtra("test", key) in PendingIntent. Is it possible ?
You can add parameters in intent.
Intent intent= new Intent(context, YourActivity.class);
String displayName = "abc"
intent.putExtra("name", displayName);
PendingIntent pendIt = PendingIntent.getActivity(context,
TxrjConstant.REQUEST_READ_NOTIFICATION,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
I am trying to get my PendingIntent working but whenever I click the notification in the notification drawer, my app crashes.
So basically I have this java class called CustomerCurrentlyServing.class and when it is loaded, it retrieves all the strings passed from the previous activity as shown below:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
queueKey = getIntent().getExtras().getString(Constants.EX_MSG_QUEUE_KEY);
shopName = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_NAME);
shopKey = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_KEY);
customerid = getIntent().getExtras().getString(Constants.EX_MSG_CUSTOMER_ID);
I implemented a method for the notification as shown below:
public void showNotification() {
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
Resources r = getResources();
Notification notification = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(r.getString(R.string.notification_text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
It is basically opening the same class when the user clicks on the notification in the notification drawer. However, it crashes whenever I click on the notification and the error is NullPointerException on the line:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
Any idea how to solve this error ? I have been trying to solve this error for a week and your help is greatly appreciated.
You should putExtra on the Intent you pass to PendingIntent
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
here you are passing new instance of Intent
you should pass already created instance i.e.
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
and write all those putExtras before creating PendingIntent object.
In this part of the code you are adding the extras to the wrong intent , you are creating an intent then creating a new one that you pass to the pending intent!
What you should be doing is this:
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
And not creating a new intent like you did in your pending intent :
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
If you do this you are putting the extras om the first intent then passing a whole new one without extras to your pending intent so when the user clicks on the notification and it opems up the activity the activity will try and get extras that dont exist thus giving a null pointer.
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.
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);
Remember to put an unique id in the PendingIntent constructor, or you could get some weird thing when you try to get the putExtra values.
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
UUID.randomUUID().hashCode(),
aint,
PendingIntent.FLAG_UPDATE_CURRENT
);
You have to use getStringExtra() and be sure the String are not null:
Intent intent = getIntent();
msg = intent.getStringExtra("msg");
phonen = intent.getStringExtra("phone");
if(msg!=null){
Toast.makeText(context,msg,
Toast.LENGTH_LONG).show();
}
and reverse Your putExtras before PendingIntent:
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
This is because you are first initialize Intent and add to PendingIntent. After that you are adding info in intent. You should add info to intent then add this intent to PendingIntent.
In my case i was missing PendingIntent.FLAG_UPDATE_CURRENT as flag.
final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);
//put in extra string into my intent
//tell the clock that the user pressed the "alarm on button"
my_intent.putExtra("extra" , 1);
int state = my_intent.getExtras().getInt("extra");
Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);
I had the same problem and I found out that you should put the putExtra before you involve the Intent
Please Use FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
I'm trying to make notification actions for Android Wear,
And I set an PendingIntent (.getService) for every action,
But only the first Action actually work.
My code (variables names are examples):
intent1 = new Intent(this, intent1.class);
Intent intent2 = new Intent(this, intent2.class);
Intent intent3 = new Intent(this, intent3.class);
pendingIntent = PendingIntent.getService(this, 0, intent1, 0);
pendingIntent2 = PendingIntent.getService(this, 0, intent2, 0);
pendingIntent3 = PendingIntent.getService(this, 0, intent3, 0);
Only clicking on the "pendingIntent" action, it workes.
When clicking on "pendingIntent2" or "pendingIntent3" action, they don't work.
Can you help? Thanks.