没有在RemoteViewsService中触发的方法 - Android

时间:2013-05-20 00:13:03

标签: java android android-intent

下面是我的小部件扩展AppWidgetProvider的一些代码。放在主屏幕上时,窗口小部件呈现正确,但视图为空。使用日志记录,我发现没有从我用来扩展RemoteViewsService(BudgetWidgetService)的类调用方法,后者返回RemoteViewsService.RemoteViewsFactory的实现。

是否存在导致BudgetWidgetService永远不会被实例化或使用的错误?

我甚至找不到检查BudgetWidgetService是否绑定到我创建的RemoteViews布局的方法。如果你能想到任何我可以进一步诊断这个问题,除了我的AppWidgetProvider和RemoteViewsService扩展中的日志记录方法调用,我也很感激,

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

1 个答案:

答案 0 :(得分:10)

我遇到了同样的问题,发现我忘了在我的AndroidManifest.xml中向我的RemoteViewsService添加android.permission.BIND_REMOTEVIEWS权限:

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

我在App Widgets guide上的“带收藏的应用小部件的清单”部分找到了我的答案。