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
Related
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.
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?
I would like to start an IntentService when pressing on my app widget.
I know how to update the widget with pressing on it but I don't have any idea how I would actually start an IntentService.
This is how to initiate the widget update
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
What would I have to change to start an INtentService instead?
ps I also started an activity based on the press, but this shows on the screen - which I do want to avoid.
Many thanks!
Use getService() instead of getBroacast(), and use an Intent that identifies your service.
Context.startService
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
PendingIntent.getService
Intent intent = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
pi.send();
Questions
When would you start a service with Context.startService vs a PendingIntent?
Why would you use one over the other?
There really is no difference.
Specifically the Context method is used to directly start it where as a PendingIntent is typically used with a notification to fire this intent when it is tapped, which is delayed until the user taps it (typically). However; you wouldn't typically send the PendingIntent directly because that is not what it is for.
A PendingIntent is an Intent that is pending, pending, meaning that its NOT supposed to happen now, but in the near future. Whereas with an Intent, it is sent at the very moment.
If a PendingIntent is not pending when it is used, then it is no longer a PendingIntent and it is infact an Intent. Defeating the purpose entirely.
PendinIntents are very much used for widgets. As the layout of a running widget doesn't "belong" to your code, but it is instead under control of the system, you can't assign directly click listeners to the interface elements. Instead you assign a PendingIntent to those elements (like buttons) so when the user touches them, the PendingIntent is "executed", something like:
// get the widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.id.widget_layout);
// prepare to listen the clicks on the refresh button
Intent active = new Intent(context, WidgetCode.UpdateService.class);
PendingIntent refreshPendingIntent = PendingIntent.getService(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonWidgetRefresh, refreshPendingIntent);
// send the changes to the widget
AppWidgetManager.getInstance(context).updateAppWidget(appwidgetid, remoteViews);
In this case a button in the widget starts a service. Usually you put extra info in the intent, with putExtras(), so the service will get any needed information to do its job.
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);