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)?
Related
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 have a notification using a remoteView. On the notification I have 3 buttons, each of them with their own pendingIntent. For some reason only the last pendingIntent that I add with setOnClickPendingIntent works.
Here is some of the
Intent intentPause = new Intent(context, BluetoothConnectionService.class);
intentPause.putExtra("pause", device.device.getAddress());
Intent intentIncrease = new Intent(context, BluetoothConnectionService.class);
intentIncrease.putExtra("inc", device.device.getAddress());
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 0, intentIncrease, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setOnClickPendingIntent(R.id.noti_pause_heat_button, pendingIntentPause);
expandedView.setOnClickPendingIntent(R.id.noti_inc_heat_button, pendingIntentIncrease);
Notification notification = new Notification.Builder(context)
.setContentTitle(this.device.getName())
.setSmallIcon(R.drawable.inu_small)
.setContentText("" + this.notificationId)
.setContent(expandedView)
.build();
use a different requestCode (second parameter of getService) for each PendingIndent. E.g.
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 1, intentIncrease, 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.
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");
...
i have set some alarm like:
public void SetAlarm(Context context, int tag, long time){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
i.putExtra("position", tag);
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute
}
now for some reason i want to update the Intent i of a triggered alarm. i have the id(tag) for identify the desire alarm. how can i do that ?
If all you want to do is change the extras in the Intent, you can do it like this:
Intent i = new Intent(context, Alarm.class);
// Set new extras here
i.putExtra("position", tag);
// Update the PendingIntent with the new extras
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Otherwise, if you want to change anything else in the Intent (like the action, or component, or data), you should cancel the current alarm and create a new one like this:
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
// Extras aren't used to find the PendingIntent
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_NO_CREATE); // find the old PendingIntent
if (pi != null) {
// Now cancel the alarm that matches the old PendingIntent
am.cancel(pi);
}
// Now create and schedule a new Alarm
i = new Intent(context, NewAlarm.class); // New component for alarm
i.putExtra("position", tag); // Whatever new extras
pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
// Reschedule the alarm
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute