Android Widget Prev and Next not working - android

i'm creating widget for my project. In that widget Prev and Next button is available.
In Same widget create two times in homepage. If I click prev for 1st widget 2nd also changing. I don't want to change another one.
my code:
Intent WidgetIntent = new Intent(this, UpdateService.class);
PendingIntent NextIntent = PendingIntent.getService(this, 1, WidgetIntent.putExtra("dir", 1), PendingIntent.FLAG_UPDATE_CURRENT);
WidgetIntent.setData(Data);
WidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Intent PrevIntent = new Intent(this, UpdateService.class);
PrevIntent.setData(Data);
PrevIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent PrevPIntent = PendingIntent.getService(this, 2, PrevIntent.putExtra("dir", -1), PendingIntent.FLAG_UPDATE_CURRENT);
If anyone know share here.
thanks,

Have a look at my answer here
How to know clicked widget id?

Related

Android: can I put 2 Pendingintents on the same view?

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.

How can I check which widget was clicked?

I need to show different data when the different widget will be clicked. For example widget one will show me activity with number 1 and widget to with numer 2 :)
Perhaps I can send some data by intent from widget, but then how to recive that data in activity class?
Widget class
Intent intent = new Intent(context, Information.class);
intent.putExtra("widget_id", appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget1x1);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
Bundle extras = intent.getBundle();
String id = extras.getString("widget_id");

Two buttons with PendingIntents - Widget

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);

Android AppWidget + android:clearTaskOnLaunch="true"

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

how to capture the clear event when clear button is pressed in notification bar in android

HI,
in Android when the clear button is pressed on the notification screen i want to capture that in my application and do some stuff. please help me in how to achieve this.
Add a deleteIntent to your Notification.
in my code i already have contentIntent and now after adding the deleteintent , when i m pressing the clear button contentintent things are getting executed.
Intent noti=new Intent(context, MyService.class);
noti.putExtra("key1", true);
contentIntent = PendingIntent.getService(context, 0, noti, 0);
Intent stopIntent = new Intent(context, Myervice.class);
notificationIntent.putExtra("Key2", true);
PendingIntent deleteIntent = PendingIntent.getService(context, 0, stopIntent, 0);
now when i m pressing the clear key i m getting the key1 and key 2 both

Categories

Resources