2013-05-20 57 views
2

下面是我的小部件擴展AppWidgetProvider的一些代碼。小部件呈現在主屏幕上時正確呈現,但視圖爲空。使用日誌記錄,我發現沒有方法從我用來擴展RemoteViewsService(BudgetWidgetService)的類中調用,它返回RemoteViewsService.RemoteViewsFactory的實現。RemoteViewsService中沒有方法觸發 - Android

是否有錯誤導致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; 
} 

回答

10

我有同樣的問題,並發現我已經忘了給android.permission.BIND_REMOTEVIEWS權限添加到我的RemoteViewsService在我的AndroidManifest.xml:

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

我發現部分下的「清單與收藏的應用小部件」上App Widgets guide我的答案。