Android Collection Widget not working on KitKat - android

I have a collection widget with a ListView on Android and it works fine for versions above 19. Nevertheless, with KitKat (which is the minimum supported version in our app) it doesn't work.
I can see the widget -there's an ImageView and a FrameLayout with another ImageView and an animated-rotate for when the widget updates- but it has an empty ListView and the clicks on the aforementioned ImageView doesn't work.
Is there anything special to do so the widget runs on KitKat? Am I missing something?
I realised that the setRemoteAdapter works, and even the RemoteViewsService.RemoteViewsFactory gets called, but it's just not reflecting the changes in the screen, nor are the click events working on static views -not the ListView I'm using, which doesn't even show items.
This is the way I'm doing it:
private fun buildWidget(appWidgetIds: IntArray, appWidgetManager: AppWidgetManager, context: Context) {
val remoteViews = getRemoveViews(context)
// Set up the intent that starts the AppWidgetService, which will provide the views for this collection.
for (i in appWidgetIds.iterator()) {
val intent = Intent(context, AppWidgetService::class.java)
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, i)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))
// Check for a valid session in order to either show the positions list or a view to ask the user to log in
if (userSession.hasValidSession()) {
// Set up the RemoteViews object to use a RemoteViews adapter. This adapter connects to a RemoteViewsService through the specified intent. This is how you populate the data.
remoteViews.setRemoteAdapter(R.id.app_widget_portfolio_list, intent)
// The empty view is displayed when the collection has no items. It should be in the same layout used to instantiate the RemoteViews object above.
remoteViews.setEmptyView(R.id.app_widget_portfolio_list, R.id.app_widget_empty_view)
setOpenAppOnClickPendingIntent(context, remoteViews, R.id.app_widget_empty_view)
// Bind a click listener template for the contents of the weather list. Note that we
// need to update the intent's data if we set an extra, since the extras will be
// ignored otherwise.
val onClickIntent = Intent(ACTION_LIST_ITEM_CLICK, null, context, AppWidget::class.java)
val onClickPendingIntent = PendingIntent.getBroadcast(context, number++, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT)
remoteViews.setPendingIntentTemplate(R.id.app_widget_portfolio_list, onClickPendingIntent)
val refreshPositionsIntent = Intent(ACTION_REFRESH, null, context, AppWidget::class.java)
refreshPositionsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, i)
val refreshPositionsPendingIntent = PendingIntent.getBroadcast(context, 0, refreshPositionsIntent, 0)
remoteViews.setOnClickPendingIntent(R.id.app_widget_refresh_button, refreshPositionsPendingIntent)
setOpenAppOnClickPendingIntent(context, remoteViews, R.id.app_widget_logo)
appWidgetManager.partiallyUpdateAppWidget(i, remoteViews)
}
private fun setOpenAppOnClickPendingIntent(context: Context, remoteViews: RemoteViews, #IdRes viewId: Int) {
val openAppIntent = MainActivity.newNavigateIntent(context, AppUrl.getHomeUrl())
val loginPendingIntent = PendingIntent.getActivity(context, 0, openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT)
remoteViews.setOnClickPendingIntent(viewId, loginPendingIntent)
}
I have tried so far to change the requestCode of the PendingIntent.getBroadcast() and use random numbers, but still, it doesn't work. I also tried appWidgetManager.updateAppWidget(i, remoteViews) instead of appWidgetManager.partiallyUpdateAppWidget(i, remoteViews) but still, no luck.
Thanks a lot in advance

Related

Mvvmcross Android Notification Pass Parameter to Existing ViewModel

I have a foreground service with a notification. When the user clicks the notification, it will bring the user back to the main screen viewmodel (or load it again if unloaded).
I want to add 2 actions (pause, stop) to the notification. It is preferred to call the same viewmodel with a parameter to indicate the action type, and let the main screen viewmodel to handle the action. As the main screen viewmodel may have been loaded already, no more initialization will be executed. Passing parameter like showing viewmodel does not work. The existing viewmodel does not know it was triggered from the notification indeed.
How can I pass a different parameter for each action type to the viewmodel, and retrieve it in the viewmodel to act accordingly? Or it should be done in a different way?
This is the code of the notification:
var request = MvxViewModelRequest<RouteLayoutViewModel>.GetDefaultRequest();
var intent = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>().GetIntentFor(request);
const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, pendingIntentId, intent, PendingIntentFlags.UpdateCurrent);
var builder = new NotificationCompat.Builder(this);
builder
.SetContentTitle(AppConstants.AppName)
.SetContentText("notification_text")
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentIntent(pendingIntent); ..........
var notification = builder.Build();
StartForeground(AppConstants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
Thanks,
Nick
MvvmCross 5 changed the way the ViewModel parameters are serialized internally. MvvmCross 5 hasn't been updated yet to handle this scenario AFAIK. Here is some sample code demonstrating how to workaround the issue using a BroadcastReceiver currently. This approach will allow you to navigate with parameters after the user clicks on the Android Notification.
When you're building the notification:
var intent = new Intent(context, typeof(YourBroadcastReceiver));
intent.SetAction("YOUR_ACTION");
// Put your other parameters here...
intent.PutExtra("id", id);
var pendingIntent = PendingIntent.GetBroadcast(context, _notificationId, intent, PendingIntentFlags.UpdateCurrent);
...
notificationBuilder.SetContentIntent(pendingIntent);
Then you add a BroadcastReceiver class like so:
[BroadcastReceiver]
public class YourBroadcastReceiver : MvxBroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == "YOUR_ACTION")
{
// TODO: Extract the contents from the intent.
var id = intent.GetIntExtra("id", 0);
// TODO: Navigate using IMvxNavigationService using the parameters pulled from the Intent.
}
}
}

AppWidget refresh Uri for RemoteViews

I have created an Appwidget that displays an image file (test.png) that is provided to it's RemoteViews via Uri.
In onUpdate i run a service that changes the content of the file. I have also set an onClickListener for the image that will call onUpdate.
-If I create an instance of the AppWidget it displays the most recently changed version of the Uri file.
-If I click the widget, my service makes the approporaite changes to the file (which I can verify with a file explorer), but it does not update the image displayed in the AppWidget.
-(and most importantly)If I delete the AppWidget and create a new one, It displays the current/correct version of the image file.
I'm aware that my service may be taking too long to take effect on the first pass, but it should display the most recent image on the next onClick/call of onUpdate.
As it stands now, the AppWidget only displays the version of the image file that exists on the first call of onUpdate.
Question:
What is the proper way to refresh the RemoteView content of an Appwidget, am I missing something in my Approach here?
thanks for your time!
Update:
I have tried calling the AppWidgetManager.notifyAppWidgetViewDataChanged() method from AppWidgetProvider.onReceive(), and still not change to the RemoteViews content after onUpdate().
public class CCWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds)
{
// Get all ids
ComponentName thisWidget = new ComponentName(context,CCWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout04);
/*
* it's here that I run a service that changes the content of the file /test/test.png
*/
RelativeLayout RL_widget = new RelativeLayout(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
RL_widget = (RelativeLayout)inflater.inflate(R.layout.widget_main, null);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/test/test.png");
remoteViews.setImageViewUri(R.id.IV_widget_image,uri);
Intent intent = new Intent(context, CCWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
//PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.IV_widget_image, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
There are various things that I have found can make widgets hard.
A. onUpdate isn't really an update mechanism
Contrary to how it sounds, onUpdate is only called in two situations:
When the widget is created.
Whenever the update cycle time (updateMillis defined in the xml definition file file for the widget) elapses.
Key point: onUpdate is never called at other times. (As far as I have ever seen in practice).
If you want the widget to update at another time, it is necessary to create a separate mechanism with knowledge of the widget and the capacity to be triggered. Typically this would be a Service which you start in the onUpdate routine. This might look like:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{
// start the service in case it ain't started yet, this also force a widget update to ensure correct status on them
Intent intent = new Intent(context, MyService.class);
intent.putExtra('service.startCode', /*A number to identify what is going on*/);
context.startService(intent);
// Everything else is triggered from the service!!
}
The service then sets the content sof the widget, and updates them as necessary, either through internal timekeeping or through the use of the Broadcast mechanism.
B. You can't really update a bit of the widget
It might seem logical to create a remoteViews when you create the widget and then update that, or the Views in it, when things change. In my experience this doesn't work predictably. When you want to change anything in a widget, create a new remoteViews, fill it out correctly and then assign it to the widget.
I ran into some device dependency with the way RemoteView was handling URIs, and found my way to a solution like this:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
Uri uri = Uri.parse("file://"+Environment.getExternalStorageDirectory().getPath()+"/test/test.png");
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.crap);
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
remoteViews.setImageViewBitmap(R.id.IV_widget_image, bitmap);
It also eliminated the need to cycle RemoteViews, as in my other answer.
Certainly not elegant, or memory efficient, but I found that if I duplicated my RemoteViews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout01);
RemoteViews remoteViews2 = new RemoteViews(context.getPackageName(),R.layout.widget_layout02);
duplicate the code surrounding them, and swap them intermittently between image updates (kind of like a buffer), resetting the RemoteViews each time forces an update on their content!
Works for me for now, please feel free chime in with a more correct solution.

Android widget won't switch layouts more than once

I'm adding a widget to an old app which I'm updating from a service I'm using to poll for data in the background (on an alarm). I update the widget every time the service gets a result. This is currently working correctly.
// Called from inside my service when it has results
private void updateWidget(List<Earthquake> earthquakes) {
AppWidgetManager manager = AppWidgetManager.getInstance(this);
int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(this, WhatsShakingWidgetProvider.class));
if (appWidgetIds == null || appWidgetIds.length == 0)
return;
Earthquake earthquake = earthquakes.get(0);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_detail);
// Update views
views.setTextViewText(R.id.widget_detail_latest_magnitude, earthquake.getFormattedMagnitude());
// etc...
// Update each widget
for(int appWidgetId : appWidgetIds) {
manager.updateAppWidget(appWidgetId, views);
}
}
This polling service is optional; it can be turned on or off in the app's settings.
If the service is off when the user adds the widget, the widget_error layout is shown, as expected. The user can tap on the widget to enter the settings and turn the background updates on. When they do this (turn the setting on or off), I broadcast ACTION_APPWIDGET_UPDATE. The widget enters onUpdate correctly, and is updated correctly by the service the next time it runs (I've set it up so the widget triggers a service call in onUpdate - see below).
The widget does not correctly display the widget_error layout when the service becomes disabled after being enabled - it leaves the old layout in place, even though all the disabled-case code is run.
This is the code that gets called when the user toggles the setting (Source):
// If our user has widgets, we should update those - let the widget do the updating depending on the prefs, though.
Intent intent = new Intent(this, WhatsShakingWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = { R.xml.widget_info };
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
And this is the code in onUpdate which should be updating the widgets, but isn't:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean backgroundUpdatesEnabled = prefs.getBoolean(PreferenceActivity.KEY_PREF_ALLOW_BG_NOTIFICATIONS,
DefaultPrefs.BG_NOTIFICATIONS_ENABLED);
if (!backgroundUpdatesEnabled) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_error);
// Update click to take to preferences
Intent intent = new Intent(context, PreferenceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_error_parent_container, pendingIntent);
// Update each widget
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
// Let's get some data for the user! Service does the work of updating the views.
WakefulIntentService.sendWakefulWork(context, GeonetService.class);
}
}
There are no errors logged in Logcat. Stepping through this, I correctly enter each part of the if when expected (that is, if the user turned the setting off, then I create RemoteViews views as widget_error, otherwise I start the service).
Why does the widget_error layout display correctly the first time through onUpdate, but not when the user enables, then disables, the background update setting?
I've tried wrapping this in a RelativeLayout and setting the visibility of the error message/the content, but that exhibited the same behaviour - I couldn't get the error message to show back up after initially hiding it.
I ended up duplicating the code in two places (the preferences activity and the widget provider) and it worked. The only variable appears to be the Context object.
It appears that for some reason the Context instance you get in the AppWidgetProvider (that is, in onUpdate) only works the first time - or, doesn't work when I send the broadcast myself. I'm not sure why.
I pulled my duplicated code out to a separate class and just pass in the Context instance I have available, whether it's the Service, an Activity, or the AppWidgetProvider (which is a BroadcastReceiver). This correctly updates the widget, and I can call it from anywhere I have a Context.
Source is available here.

Switch status from android widget

I have an app with a widget that has a switch on it and a class that extends service to manage the content on the widget. I'm using RemoteViews to access the views to edit them but there are somethings that I want to do dependant on the sate of the switch. Is there a way to get the state of the switch? I've had a look through the api and couldn't find a way to do it.
Preemptively: I can't use findViewById because its not an activity
I can post code if needed.
For clarity the type of switch I want to use is one like this
Are you following the App Widgets Guide? I don't think the Switch widget is supported in RemoteViews.
Anyways, since you can't get any information on demand, you need to use setOnClickPendingIntent() and have the click send an intent to update your app widget. Stick an extra in the Intent specifying what state the button is in (or what state it would change to when clicked, either one). In the update logic, read the extra, change the UI, and set a new PendingIntent with the extra changed.
public class MyAppWidgetProvider extends AppWidgetProvider {
public static final String ACTION_UPDATE_SWITCH = "MyAppWidgetProvider.UPDATE_SWITCH";
public static final String EXTRA_SWITCH_ON = "MyAppWidgetProvider.EXTRA_SWITCH_ON";
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_UPDATE_SWITCH.equals(action)) {
int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWDIGET_ID, 0);
if (id != 0) {
updateAppWidgetSwitch(context, intent, id);
}
}
super.onReceive(context, intent);
}
private void updateAppWidgetSwitch(Context context, Intent intent, int appWidgetId) {
boolean switchOn = intent.getBooleanExtra(EXTRA_SWITCH_ON, false);
// take some action based on the switch being clicked
RemoteViews views = new RemoteViews (context.getPackageName(), R.layout.app_widget_layout);
// normal RemoteViews stuff
// use switchOn var to set your switch state
// make new on click pending intent
Intent intent = new Intent(ACTION_UPDATE_SWITCH);
intent.putExtra(AppWidgetManager.EXTRA_APPWDIGET_ID, id);
intent.putExtra(EXTRA_SWITCH_ON, !switchOn); // new state
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.switch_id, pendingIntent);
// update widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
// other AppWidgetProvider methods ...
}
In your manifest, add this intent filter to your AppWidgetProvider's <receiver> element. Note that if you change the value of the action string above, make sure you change it here as well.
<intent-filter>
<action android:name="MyAppWidgetProvider.EXTRA_SWITCH_ON" />
</intent-filter>

Android widget buttons stop working

I have an Android application with a widget, that has buttons. This code works.
The buttons on the widget stop working when something happens, such as changing the language of the phone. I use shared preferences, so if the user reinstalls the app (without uninstalling), the buttons are working again and the settings remain the set ones.
I have noticed the Intents in my AppWidgetProvider class (code beneath this analysis) are not fired appropriately.
I added a Toast message to the Call1 class instantiated from AppWidgetProvider, but it doesn't display.
My UpdateService.java is just getting the set preferences and customizing the widget's appearance, so I don't think it could possibly be related to my issue.
My Main.java file merely consists of spinners and saves shared preferences, which means I select "Computer" in a spinner, so that the "Computer" text appears on the widget. It also does not disappear when I change the language of the phone, and neither do images. Therefore, I believe UpdateService.java must be ok.
Here is the AppWidgetProvider class:
public class HelloWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_CONFIGURE2 = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public static String ACTION_WIDGET_RECEIVER2 = "ActionReceiverWidget";
private static final int REQUEST_CODE_FOUR = 40;
private static final int REQUEST_CODE_FIVE = 50;
private static final int REQUEST_CODE_SIX = 60;
private static final int REQUEST_CODE_SEVEN = 70;
private static final int REQUEST_CODE_EIGHT = 80;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
//Intent widgetUpdateIntent = new Intent(context, UpdateService.class);
//context.startService(widgetUpdateIntent );
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);
//P1 starts Call1.class
Intent configIntent4 = new Intent(context, Call1.class);
configIntent4.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent4 = PendingIntent.getActivity(context, REQUEST_CODE_FOUR, configIntent4, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView01, configPendingIntent4);
//P2 starts Call2.class
Intent configIntent5 = new Intent(context, Call2.class);
configIntent5.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent5 = PendingIntent.getActivity(context, REQUEST_CODE_FIVE, configIntent5, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView02, configPendingIntent5);
//P3 starts Call3.class
Intent configIntent6 = new Intent(context, Call3.class);
configIntent6.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent6 = PendingIntent.getActivity(context, REQUEST_CODE_SIX, configIntent6, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView03, configPendingIntent6);
//P4 starts Call4.class
Intent configIntent7 = new Intent(context, Call4.class);
configIntent7.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent7 = PendingIntent.getActivity(context, REQUEST_CODE_SEVEN, configIntent7, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView04, configPendingIntent7);
//P5 starts Call5.class
Intent configIntent8 = new Intent(context, Call5.class);
configIntent8.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent8 = PendingIntent.getActivity(context, REQUEST_CODE_EIGHT, configIntent8, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView05, configPendingIntent8);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
//Log.e("Error", "msg = null");
}
}
super.onReceive(context, intent);
}
}
}
I also have an EditPreferences.java, GlobalVars.java and some other now meaningless classes. The names of the classes speak for themselves.
One other thing. I also have a Widgetmain.java:
public class WidgetMain extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetmain2);
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain2);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
Edit: How about this:
When I install this app on my colleague's ZTE Blade the textviews on the widget are not loaded with the appropriate text, just with the one determined in the strings.xml.
When I reinstall the app (without uninstalling), the textviews are loaded and everything is fine. This problem doesn't emerge on my HTC Desire HD.
The textviews are load in the aforementioned UpdateService.java like this (part of the code):
RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.widget_textview, name);
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
Even if "name" is static (e.g. String name="Something"), that textview is still not loaded at the first install.
Try to update the RemoteViews with the click listeners whenever you create new instance by "new RemoteViews". Maybe the RemoteViews are freshly loaded from the XML in some circumstances, therefor the click listeners needs to be re-assigned.
My UpdateService.java is just getting the set preferences and customizing the widget's appearance, so I don't think it could possibly be related to my issue.
It is possible it is related, in as much that you could use it to "refresh" the pending intent. I have a similar issue in my appwidget that an image button stops responding to clicks after some random run time (hours).
I found this thread:
AppWidget Button onClick stops working
And this quote:
The pending intent is "burned" after each use. You need to set it again. Or wait for the widget to get refreshed, then it happens, too, but that's probably not the desired way.
Given that the widget update time normally is set at many hours or days (mine is 86400000 milli seconds) in order to prevent the phone going out of suspend every so many minutes your widget will not often run onUpdate. It is possible that setting the pending intent ALSO in the update service will prevent the problem you describe.Each time the update service runs the pending intent is re-created.
I have today added this possible fix to my appwidget and I have to wait and see if the fix really works, but so far so good.
I added the following code in the update service' loop where it refreshes each widget:
for (int i=0; i<appWidgetIds.length; i++)
{
appWidgetId=appWidgetIds[i];
/* other stuff to do */
RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
/* here you "refresh" the pending intent for the button */
Intent clickintent=new Intent("net.example.appwidget.ACTION_WIDGET_CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
views.setOnClickPendingIntent(R.id.example_appwidget_button, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, views);
/* then tell the widget manager to update */
appWidgetManager.updateAppWidget(appWidgetId, views);
}
The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView. (Partiall updates are only available for API14 and up...).
The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService. Otherwise, it's just won't set the pendingIntents again.
So just add the code setting the pendingIntents to the service and your problem will be solved =]
I think the PendingIntents may need a flag passed to them, maybe try changing:
PendingIntent.getActivity(context, REQUEST_CODE, configIntent, 0);
to:
PendingIntent.getActivity(context, REQUEST_CODE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
From the PendingIntent documentation, I think code '0' is undefined. In this case FLAG_UPDATE_CURRENT would work best, as you probably want to update the Intent every time the button is clicked.
Given all the information you gave, I'd say your update method is not triggered properly when the preferences are changed.
I expect after so much tests, you have verified your Manifest file contains:
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
Have you confirmed onUpdate ever runs? It seems to me that if reinstalling the application without deinstalling solves your issues, it might be because it forces an update call.
After careful check, it turns out that ScanPlayGames has a point: the official documentation's example uses super.onUpdate(). Note that it uses it at the end of the method, but several examples on Internet state you're better served using it at the start of your method.
I've had that problem for long time. My widget has button #(onUpdate). The widget has a service for updates. The button on the widget stop working when something happens, like: changing the font, etc..
When i re-install the app, the button works again. Finally, I realized that i never called onUpdate in my Service class.
Calling onUpdate from the service class fixed the problem.
If someone still has this problem try setting the attribute android:updatePeriodMillis in your AppWidgetProviderInfo;
The operating system can kill the pending intent for various reasons and your buttons can stop to work. When you set this attribute, you are telling Android when it should call the onUpdate method in the AppWidgetProvider, so all pending intents will be re-created.
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
...
android:updatePeriodMillis="3600000">
</appwidget-provider>

Categories

Resources