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