Matan Shavit
Matan Shavit

Reputation: 43

No methods firing in RemoteViewsService - Android

Below is some code for my widget extending AppWidgetProvider. The widget renders correctly when put on a home screen, but the view is empty. Using logging, I found that no methods are called from the classes I used to extend RemoteViewsService (BudgetWidgetService), which returns an implementation of RemoteViewsService.RemoteViewsFactory.

Is there a mistake causing BudgetWidgetService to never be instantiated or used?

I can't even find a way to check if the BudgetWidgetService is even bound to the RemoteViews layout I create. If you can think of any I can further diagnose this problem, other than logging method calls in my AppWidgetProvider and extension of RemoteViewsService, I would appreciate that as well,

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.i("Budget Widget", "Provider onUpdate called");
    // Update each of the widgets with the remote adapter
    for (int i = 0; i < appWidgetIds.length; ++i) {
        RemoteViews layout = buildLayout(context, appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], null);
        appWidgetManager.updateAppWidget(appWidgetIds[i], layout);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews buildLayout(Context context, int appWidgetId) {
    Log.i("Budget Widget", "Provider buildLayout called");
    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_budget);

    RemoteViews list_item = new RemoteViews(context.getPackageName(), R.layout.widget_budget_item);
    list_item.setTextViewText(R.id.widget_budget_item_text, "Test #1");

    final Intent intent = new Intent(context, BudgetWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    rv.setRemoteAdapter(R.id.widget_budget_list, intent);

    rv.setEmptyView(R.id.widget_budget_list, R.id.empty_view);

    final Intent refreshIntent = new Intent(context, BudgetWidgetProvider.class);
    refreshIntent.setAction(BudgetWidgetProvider.REFRESH_ACTION);
    final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0,
            refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    rv.setOnClickPendingIntent(R.id.refresh_button, refreshPendingIntent);

    return rv;
}

Upvotes: 3

Views: 1849

Answers (1)

dgmltn
dgmltn

Reputation: 3903

I was having the same problem, and discovered that I had forgotten to add the android.permission.BIND_REMOTEVIEWS permission to my RemoteViewsService in my AndroidManifest.xml:

<service android:name="MyWidgetService"
...
android:permission="android.permission.BIND_REMOTEVIEWS" />

I found my answer under the section "Manifest for app widgets with collections" on the App Widgets guide.

Upvotes: 11

Related Questions