OnClick not working after acore restart - android

I have a widget with 2 different OnClick intents, and they both work as expected. Except after an acore restart. I think it has something to do with my pendingintent not getting refreshed after the restart.
Can anyone help me with this issue?
This is from my onRecieve()
final RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
views.setOnClickPendingIntent(R.id.TopRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmClockIntent);

As I've written here you should only produce a single instance of the RemoteView, and share it within the class.

Related

Configure Widgets with difference content

My app creates many widgets of the same type, but with different content. When the user clicks on one widget, it is important to know the widgetId, so I know the content to update.
My way seems to be totaly wrong, because my PendingIntent gets overridden. So I always get the same EXTRA_APPWIDGET_ID instead of n different ones.
Here is my code, the part when I set the PendingIntent for my OnClick.
// edit intent
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.base_info_widget);
Intent editIntent = new Intent(context,MainActivity.class);
editIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
editIntent.putExtra(GlobalVariables.COMMAND_KEY_ACTION,GlobalVariables.COMMAND_VALUE_EDIT);
editIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
Log.d(MY_TAG, "putExtra EXTRA_APPWIDGET_ID="+ Integer.toString(appWidgetId));
PendingIntent pendingEditIntent = PendingIntent.getActivity(context, 0, editIntent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.layout_wrapper, pendingEditIntent);
I hope my question was clear. How can I solve that problem?
Make your intent unique:
editIntent.setData(Uri.parse(editIntent.toUri(Intent.URI_INTENT_SCHEME)));
That's why you get always same id on all widgets

How do I cancel Pending Intents in a Android Widget?

I've got some weird behavior and I can only assume is because of the Pending intents I am using.
Scenario
I have a widget (4x1) which has 4 buttons. Within onUpdate of the widget, I add an pending intent for each button. My intents fires a Service with a bundeled parameter and depending on this parameter starts something. I attach intents as this:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Bundle b = new Bundle();
b.putString("myVariable", someVariable);
Intent intent = new Intent(context, AppStarterService.class);
intent.putExtras(b);
PendingIntent pendingIntent = PendingIntent.getService(context, buttopnPosition, intent, 0);
views.setOnClickPendingIntent(R.id.btnOne, pendingIntent);
The problem
The code works just fine, until the user decides to update the content of the button. Then, a new Pending Intent is done. So, when I press again the button, sometimes it still executes the old intent and not the new one. I don't know how to explain this better. Let's say for my first intent the parameter is "TestOne", after my update, the new intent has parameter "TestX". When the user clicks on the button, on my service I get in intent extras still "TestOne" instead "TestX". So, my guess is that somehow, I need to cancel the previous intent, when the widget button content changes.
Is this the issue ? Am I doing something wrong ? How do I cancel the old intent, I need to recreate it and then cancel it ?
Thank you for your time.
I you keep having this problem even with FLAG_UPDATE_CURRENT, try defining a different requestCode each time, with something like this:
private static int request = 0;
...
PendingIntent pendingIntent = PendingIntent.getService(context, request++, intent, 0);
So each time a new PendingIntent is created, a new requestCode is used, at least during class life.
I hope it helps.
I think you want to set the flag http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT as the last parameter to PendingIntent.getService

Calling an activity on HomeScreenWidget click

I am trying to open my activity when a user clicks on a button in my HomeScreen Widget. But I guess the app is not responding to my onclick. Here is my code snippet :
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.layout_appwidget);
Intent launchActivity = new Intent();
launchActivity.setClass(context,UpdatesScreen.class);
launchActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0, launchActivity, 0);
remoteViews.setOnClickPendingIntent(R.id.iv_widget, pendingIntent);
ComponentName thisWidget = new ComponentName(context, MyAppWidgetProvider.class.getName());
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);
I have copied the same code in onUpdate as well as onEnabled method of MyAppWidgetProvider class. But still it's not working at all.
I have read many post regarding this issue but all are using the same code. Am I doing something wrong?? What is the right way to do the same.
Thanks in advance.
I have found the mistake. Posting my answer to help someone facing the same issue.
Actually, I was overriding both the method onUpdate() as well as onReceive(). If we override onReceive(), in that case onUpdate() never gets called. and I wasn't using ACTION_APPWIDGET_UPDATE as an action in onReceive() too.

Reconfigure Widget After It Has Been Added

How do I reopen the configuration activity for a widget after it has been added to the homescreen?
The following code from a Google search does not work because the widget id in the extra does not carry through to the activity:
String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
Intent configIntent = new Intent(context, Configuration.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
views.setOnClickPendingIntent(R.id.editButton, configPendingIntent);
OK, I'm a year too late, but I was just looking for the same answer as you and noticed a missing flag in your code, which was the answer to a different problem I had a couple of days ago concerning pending intents, extras and widgets. Try changing this line of your code:
PendingIntent configPendingIntent
= PendingIntent.getActivity(context, 0, configIntent, 0);
to this:
PendingIntent configPendingIntent
= PendingIntent.getActivity(context, 0, configIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent will reuse existing cached intents that match new ones and the "match" does not consider extras, so new extras can be ignored. Adding FLAG_UPDATE_CURRENT makes PendingIntent write the extras into the cached intent (or something like that).
Anyway, the above worked for me just now (though I also used AppWidgetManager.ACTION_APPWIDGET_CONFIGURE as the action name, if that makes any difference).
Unfortunately you don't: the widget spec as it currently exists does not support reconfiguration of widgets through the widget configuration activity.
What you can do is launch your widget configuration activity and track the specific widget ID manually: once the new settings are in place you can then update the widget manually using the appropiate manager method. You won't be able to use the same code path as a new widget does, however.

Launching an activity from an AppWidget

How do i launch an activity from an AppWidget? I want to give the user the ability to edit its settings.
All the AppWidget methods have a Context: you can use that to create an Intent to launch your Activity.
EDIT: strictly speaking, you don't even need the Context (just create an Intent and then call startActivity).
you can find a lot of examples around the internet this example is excellent courtesy of Mark Murphy
https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget
You have to do something like this:
Intent intent = new Intent(context, MainWidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.appwidget);
rv.setOnClickPendingIntent(R.id.button, pendingIntent);
while MainWidgetActivity.class is the Activity you want to launch

Categories

Resources