I have an App Widget.
In the App Widget I try to set 2 Pendingintests on the same Viev:
//FIRST PENDINGINTENT
Intent i1 = new Intent(getApplicationContext(), AppWidget.class);
i1.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pi = PendingIntent.getBroadcast(
getApplicationContext(), 0, i1,
PendingIntent.FLAG_UPDATE_CURRENT);
//SECONDPENDINGINTENT
Intent i11 = new Intent(getApplicationContext(), WakeUp.class);
PendingIntent pi1 = PendingIntent.getActivity(
getApplicationContext(), 0, i11,0);
//I SET THE PENDINGINTENT ON THE VIEW
updateViews.setOnClickPendingIntent(R.id.background, pi1);
updateViews.setOnClickPendingIntent(R.id.background, pi);
As you can see I set 2 Pendingintents (pi and pi1) on the SAME view R.id.background.
The Pendingintent pi works as it shuould.
The Pendingintent pi1 has NO EFFECT.
Please any help very much appreciated
This is not possible. Any View in the RemoteViews can have only one PendingIntent for setOnClickPendingIntent(). If you call setOnClickPendingIntent() twice, the last one in wins.
Hence, please call it just once, and have WakeUp call sendBroadcast() to accomplish your second operation.
Also, please replace getApplicationContext() with this, as you do not need the application context in any of this code.
Related
I have below code creating from service for notification.
I am trying to send different values in extra still getting the last set value every time in receiver.
Intent closeButton = new Intent(this,DownloadCancelReceiver.class);
closeButton.putExtra("id",id);
closeButton.setAction("Action"+Long.toString(id));
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, id, closeButton, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
notificationView.setProgressBar(R.id.pb_progress, 100, 5, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);
The only things that you are changing in Pending Intent are Extra and Action, and then for optimization purposes Android is reusing the old intent. Try to change PendingIntent.FLAG_UPDATE_CURRENT with PendingIntent.FLAG_CANCEL_CURRENT
To create the PendingIntent try:
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, id, closeButton, PendingIntent.FLAG_CANCEL_CURRENT);
I have a PendingIntent which is used in an AlarmManager The PendingIntents have unique ids which I keep track of. Because of this, I should be able to cancel the PendingIntent by passing the unique id when I try to cancel it. I am trying to cancel it in a different Fragment from the one I created it in, if that makes any difference.
But I have not been able to cancel it even after looking at many past questions.
I have looked at eg.: https://stackoverflow.com/a/4350149/2442638 , https://stackoverflow.com/a/12257277/2442638 , https://stackoverflow.com/a/11682008/2442638
This is how I create the PendingIntent and set it with the AlarmManager:
// remCounter is unique for each PendingIntent
Intent remIntent = new Intent();
remIntent.setAction("com.example.test.remBroadcast");
Bundle extras = new Bundle();
extras.putString("content_title", (String) s_content_title);
extras.putString("content_text", (String) s_content_text);
extras.putBoolean("bOngoing", boolean_ongoing);
extras.putInt("remCounter", remCounter); // This is how I keep track of the unique id. I have proved this to work.
remIntent.putExtras(extras);
PendingIntent remPendingIntent = PendingIntent.getBroadcast(
getActivity(), remCounter, remIntent,
PendingIntent.FLAG_ONE_SHOT);
getActivity(); // (Do I need this?)
AlarmManager remAlarmManager = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
remAlarmManager.set(AlarmManager.RTC_WAKEUP,
setForCal,
remPendingIntent);
This is how I'm trying to cancel a PendingIntent:
// cancelCounter is the value of the unique id of the PendingIntent I want to cancel
Intent intent = new Intent();
PendingIntent pi = PendingIntent.getBroadcast(getActivity(),
cancelCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pi.cancel();
AlarmManager am = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
From the documentation of Pending Intent FLAG_CANCEL_CURRENT in Android:
by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT
Can anyone explain what this line means?
Once you create a new PendingIntent with FLAG_CANCEL_CURRENT, anything holding a previous PendingIntent for the same Intent will no longer be able to execute that original PendingIntent.
For example, suppose we have this:
Intent i=new Intent(this, Foo.class);
i.putExtra("key", 1);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
and we use that PendingIntent with, say, a Notification.
Later on, we execute:
Intent i=new Intent(this, Foo.class);
i.putExtra("key", 2);
PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
At this point, the PendingIntent created originally (pi) is no longer valid, and whatever we use pi2 for will see the updated extra value (2).
If, instead, we did:
Intent i=new Intent(this, Foo.class);
i.putExtra("key", 2);
PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
At this point, pi and pi2 both represent the same PendingIntent, and both will see the updated extra value (2).
Or, if we did:
Intent i=new Intent(this, Foo.class);
i.putExtra("key", 2);
PendingIntent pi2=PendingIntent.getActivity(this, 0, i, 0);
At this point, pi and pi2 still represent the same PendingIntent, but the extras are unchanged, as getActivity() returns the original PendingIntent without applying the new extras.
Most times, FLAG_UPDATE_CURRENT is a fine answer when you are trying to replace extras inside a PendingIntent.
I'm creating a widget with two buttons.
One of them updates the content of the widget and the second one must launch an activity.
I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.
I've revised the code and can't understand what's wrong.
Any help will be very appreciated.
This is the code.
RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);
Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
Intent intentSettings = new Intent();
intentSettings.setClass(context,WidgetConfig.class);
PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);
PendingIntent pendingIntentSettings = PendingIntent.getActivity(context, 0, intentSettings, 0);
controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);
Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...
PendingIntent pendingIntentSettings =
PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);
and if multiple widget's are possible add the widgetId there too.
Make sure both of the activities/broadcasts are listed in the manifest file.
Moreover, try creating the Intent with this constructor:
Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
Intent intentSettings = new Intent(context,WidgetConfig.class);
add imports if needed.
Hope some of that will make you widget work.
Check this link to know which button has been clicked when there is two or more button in a widget..
https://stackoverflow.com/a/10733049/1331593
It should work... IF it does not work please let me know what is the problem...
You can try this code:
Intent read = new Intent(ctx, NotificationClick.class);
read.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
read.putExtra(Intent.EXTRA_SUBJECT, "READ");
PendingIntent readInt = PendingIntent.getActivity(ctx, 1, read, PendingIntent.FLAG_IMMUTABLE);
Intent reply = new Intent(ctx, NotificationClick.class);
reply.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
reply.putExtra(Intent.EXTRA_SUBJECT, "REPLY");
PendingIntent replyInt = PendingIntent.getActivity(ctx, 2, reply, PendingIntent.FLAG_IMMUTABLE);
NotificationManagerCompat nMgr = NotificationManagerCompat.from(ctx);
Notification newMessageNotification = new NotificationCompat.Builder(ctx, "MESSAGE_CHANNEL")
.setSmallIcon(R.drawable.user_account)
.setContentTitle(contact)
.setContentText(text)
.addAction(R.drawable.drafts, "Read", readInt)
.addAction(R.drawable.drafts, "Reply", replyInt)
.setGroup(MESSAGE_GROUP)
.build();
nMgr.notify(100, newMessageNotification);
I want my app to show a login-screen every time it is opened. With android:clearTaskOnLaunch="true" everything works fine. But there is a bug which is happening only if the phone was shutdown and the app was first started with the widget.
Intent intent = new Intent(context, StartActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
This code should start my mainactivity. At first start is does. But ending the app with HOME and restart it with the widget causes my app to not start with my startactivity but with another activity of my app without showing the loginscreen.
What can I do to fix it.
I've tried
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
But it does not work. All I want is to (re)-start the StartActivity-Intent like in startActivity(..) but I found no option to give my Widget an OnClickListener which starts a "normal" Activity.
Best regards,
Till
Have you tried adding the Flag to the intent:
Intent intent = new Intent(context, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
Intent API
setFlags(int)
FLAG_ACTIVITY_CLEAR_TASK