Android appwidget远程视图无法更新

时间:2013-08-08 07:08:08

标签: android android-appwidget appwidgetprovider

当我从某个活动更新窗口小部件时,列表远程视图没有更新(我的意思是刷新自己)。它出现在应用程序小部件的更新(日志显示)上。但是没有进入列表视图的适配器以使用新数据对其进行充气。

public void onUpdate(Context context, AppWidgetManager appWidgetManager,

int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Log.e("called", "onupdate");

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_lay);

        views.setTextViewText(R.id.textView1,
                "Floating Text will be displayed here .. Daily Thoughts");

        Intent intent = new Intent(context, RemoteServiceEmin.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        views.setRemoteAdapter(R.id.gridview, intent);

        // inflating list remote view

        Intent intentlist = new Intent(context, ListInflater.class);
        intentlist.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.listview, intentlist);

        // setting onclick on gridview
        Intent itemClickIntent = new Intent(context, MyWidgetProvider.class);
        itemClickIntent.setAction(ITEM_CLICK_ACTION);
        itemClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        PendingIntent itemClickPendingIntent = PendingIntent.getBroadcast(
                context, 0, itemClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.gridview,
                itemClickPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

1 个答案:

答案 0 :(得分:10)

所以最后完成它并再次感谢developer.android.com。您需要做的就是从您想要处理“更新”的位置触发广播意图。

Intent initialUpdateIntent = new Intent(
                AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        initialUpdateIntent
                .setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        sendBroadcast(initialUpdateIntent);

然后在AppReidget的onRecieve中捕获它,并使用

更新Remoteview
 notifyAppWidgetViewDataChanged(). 

类似于codenippet的东西

 else if (intent.getAction().equals(
            AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {

         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
             appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,  R.id.listviewnew);
           Log.e("finally after a whole day", "working :");  

    }