1

Please bear with me because I am new to Android development.

I am building a widget that opens up a Configuration Activity upon placement on the home screen. When I am finished configuration and have pressed "done", the widget provider (onUpdate) is supposed to call the widget service class to bind a list of drawables to a gridview. However, the service class is not being called and the gridview is coming up empty. Any help on this is appreciated, code is included below:

*Note: i have a main.xml that has the gridview in it and another xml that has the template (imageView and textView) the gridview cell.

Manifest:

<service android:name=".WidgetService" android:permission="android.permission.BIND_REMOTEVIEWS" android:exported="false" />

<receiver android:name=".WidgetProvider" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
  <meta-data android:name="android.appwidget.provider" android:resource="@xml/button_widget_provider" />
</receiver>

Provider:

public class WidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

 for (int i=0; i < appWidgetIds.length; i++){

   Intent intent = new Intent(context, WidgetService.class);
   intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
   intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

   RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
   remoteViews.setRemoteAdapter(appWidgetIds[i], R.id.gridview, intent);

   appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
 }       

   super.onUpdate(context, appWidgetManager, appWidgetIds);
 }
......(omitted code)}

Service:

public class WidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent){
            return new VZRemoteViewsFactory(this.getApplicationContext(), intent);
        }
    }

    class VZRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory{

       public VZRemoteViewsFactory(Context context, Intent intent){
            thisContext = context;
            appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
       }

       public void onCreate(){
            // code to add drawables to list here
       }

       public RemoteViews getViewAt(int position){
            RemoteViews remoteView = new RemoteViews(thisContext.getPackageName(), R.layout.vzwidget_item);

            //  setting the image view resource this way for now just for testing
            remoteView.setImageViewResource(R.id.launcher_icon, R.drawable.vzicon);
            remoteView.setTextViewText(R.id.textview, "test");

            return remoteView;
       }
.....(omitted code)}

Thank You!

1 Answers1

0

From my experience (and looking at my own just written widget) - OnUpdate is called right when the configuration activity is launched, and after you publish the results from this activity, the following intent action is given to the appWidgetProvider - android.appwidget.action.APPWIDGET_UPDATE_OPTIONS (tested on jellybean).

This might not be the most proper way to handle this, but this is how I am handling this - when the configuration activity publishes the result - I start up my remote view service and start sending views with the proper widgetid.

Sagi Antebi
  • 286
  • 1
  • 6
  • Thank you for answering Sagi! I tried it the way you said, and unfortunately it did not work for me...but you pointed me in the right direction. Thanks! – vanloren1377d Nov 27 '12 at 18:18
  • As I was going through this, I found that my configuration activity was NOT calling my OnReceive() or OnUpdate() methods even after setting the result. I found this hack that worked here: [link](http://stackoverflow.com/questions/3818545/how-do-i-force-an-update-in-a-configuration-activity) – vanloren1377d Nov 27 '12 at 21:07
  • Glad that it worked! I personally think listening to onEnabled in the WidgetProvider is the more right direction after finishing the activity - as far as i know onUpdate is called with the activity launch and periodically. – Sagi Antebi Dec 02 '12 at 22:53