UI控件响应不更新播放 [英] Widget UI not updating in response to broadcast

查看:161
本文介绍了UI控件响应不更新播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个小部件它们基本上是我的应用程序内快速链接功能,

I have a couple of widgets which are basically quick links to functionality within my app,

这些部件基本上的ListView / GridView的这正在由主要的应用程序的执行过程中发生变化的数据支持。

These widgets are basically ListView/GridViews which are being backed by data that changes during the main App's execution.

由于数据的应用程序执行更新频率设置为使中不仅改变了:

Since the data is only changed during app execution the update frequency is set as so:

android:updatePeriodMillis="0"

和上的数据改变了我火了以下内容:

and on the data changed I fire the following:

public void updateWidget(@SuppressWarnings("rawtypes") Class provider) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
    int[] gridids = appWidgetManager.getAppWidgetIds(new ComponentName(this.getPackageName(), provider.getName()));

    Intent intent = new Intent(this, provider);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,gridids);
    sendBroadcast(intent);
}

我能够赶上这在的onReceive()的提供商,并从他们的呼叫更新机制:

I'm able to catch this in the onReceive() of the Provider and from their call the update mechanism:

public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    Log.e(this.getClass().getSimpleName(), "onReceive()");
    int[] ids = intent.getExtras().getIntArray("appWidgetIds");
    for(int id : ids) onUpdateWithService(context, id);
}

protected void onUpdateWithService(Context context, int widgetId) {     
    super.onUpdateWithService(context, widgetId);
    RemoteViews views = new RemoteViews(context.getPackageName(), getResourceLayout(context, widgetId));                    

    Intent intent = new Intent(context, GridViewWidgetServiceAdapter.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

    views.setRemoteAdapter(R.id.listView, intent);
    views.setPendingIntentTemplate(R.id.listView, PendingIntent.getActivity(context, -1, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT));
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(widgetId, views);
}

我遇到的问题是,虽然code类火灾和 updateAppWidget()被调用时,小部件的用户界面不会改变。

The problem I'm having is that although the code fires and updateAppWidget() is called, the UI of the widget doesn't change.

我缺少参与的Widget更新一招?

Am I missing a trick involved in Widget updates?

推荐答案

这个答案似乎来解决这个问题。

This answer seems to fix the problem.

不知道的为什么的就解决了这个问题,但它确实...

Not sure why it fixes the problem, but it does...

添加以下行的意图正常工作

Adding the following line to the intent works fine

Intent intent = new Intent(context, GridViewWidgetServiceAdapter.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.putExtra("Random", Math.random() * 1000); // Add a random integer to stop the Intent being ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

这篇关于UI控件响应不更新播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆