android - 无法在 Widget 中更新 Button 的文本

标签 android widget homescreen remoteview appwidgetprovider

如何访问主屏幕 Widget 的项目,以便我可以对它们进行更改,例如设置 Button 的文本?在我的 AppWidgetProvideronReceive() 方法中,我试图设置一个 Button 的文本,它位于我的主屏幕小部件中:

@Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(MainActivity.ACTION_CHANGE_BUTTONSTATE))
        {
            String state = intent.getExtras().getString("state");
            Log.d("HomescreenWidget", "received broadcast for button state");

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.id.widgetButton);
            views.setTextViewText(R.id.widgetButton, state);
            ComponentName cn = new ComponentName(context,
                    HomescreenWidget.class);
            AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
        }
        super.onReceive(context, intent);
    }

我的 Widget 的 layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/widget_margin" >

    <Button
        android:id="@+id/playButton_Widget"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:minWidth="80dp"
        android:text="@string/playButton" />
</RelativeLayout>

我收到了 Broadcast,但是我的 Widget 显示了以下文本:“Problem loading widget”。

编辑 我在目录中发现了以下消息:

W/AppWidgetHostView(135): updateAppWidget couldn't find any view, using error view
W/AppWidgetHostView(135): android.widget.RemoteViews$ActionException: can't find view: 0x7f0a0004

0x7f0a0004 是我的WidgetButton。可能是什么原因,找不到它?

最佳答案

要通过 appwidget 提供程序访问您的 View ,您可以使用此远程 View

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.widgetname, newName);

或通过服务:

public class UpdateWidgetService extends Service {

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onStart(Intent intent, int startId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
        .getApplicationContext());

    int[] allWidgetIds = intent
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    for (int widgetId : allWidgetIds) {

      RemoteViews remoteViews = new RemoteViews(this
          .getApplicationContext().getPackageName(),
          R.layout.widget_layout);

      remoteViews.setTextViewText(R.id.update,
          "Random: " + String.valueOf(number));

      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();

    super.onStart(intent, startId);
  }

} 

并且不要忘记将服务添加到 list 中

<service android:name=".UpdateWidgetService"></service> 

然后在 onUpdate 中(在您的 App Widget Provider 类中)

@Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {

    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    Intent intent = new Intent(context.getApplicationContext(),
        UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    context.startService(intent);
  }

我希望这可以帮助:)

关于android - 无法在 Widget 中更新 Button 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035041/

相关文章:

android - scrollview 在布局 Activity 的底部留下额外的空间

Android Widget Upgrade app..删除小部件

c++ - Qt:如何在委托(delegate)绘制事件中渲染时增加 QTablewidget?

android - 如何更改默认的 Android 书签图标?

Android 添加到主屏幕基础知识

Android,如何使 AVD 变小?

android - 将 Scala 2.8 移植到 Netbeans NBAandroid 项目中......我缺少哪些步骤?

android - setRetainInstance(true) + setCustomAnimations(...) = 每个方向变化的动画?

javascript - 如何将目标 ="blank"添加到 Pinboard.in Linkroll 小部件?

android - 以编程方式进入主屏幕