3

我已经阅读了很多问答,但我找不到我的答案。
可能是我的实施中有问题。
问题是我TextView在小部件中没有得到更新。

逻辑是这样的:

1.setOnClickPendingIntent在特定buttononUpdate()
2.单击此按钮将广播intent带有声明的action
3.最后我将更新中的textView文本onRecieve()

小部件2.class:

public class Widget2 extends AppWidgetProvider {

private static final String SCROLL_LEFT = "widget2.SCROLL_LEFT";

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

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);

        Intent scrollLeft = new Intent(Widget2.SCROLL_LEFT);
        PendingIntent leftScrollPendingIntent = PendingIntent.getBroadcast(context,
                appWidgetId, scrollLeft, appWidgetId);
        remoteViews.setOnClickPendingIntent(R.id.left_scroller, leftScrollPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

@Override
public void onReceive(Context context, Intent intent) {

    Log.e(getClass().getSimpleName(), "onReceive()");
    int appWidgetId = intent.getFlags();
    if (intent.getAction().equals(SCROLL_LEFT)) {
        updateCurrentWidget(context, appWidgetId);
        Log.i("onReceive", SCROLL_LEFT + "   appWidgetId = " + appWidgetId);
    }
            super.onReceive(context,intent);  
}

private void updateCurrentWidget(Context context, int appWidgetId) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);
    remoteViews.setString(R.id.name_of_the_app, "setText", "android os");
    remoteViews.setTextViewText(R.id.description, "best ever");

    Log.i("updateCurrentWidget", "the text have been set");
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(appWidgetId, remoteViews);
}  

清单.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".Widget2" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="widget2.SCROLL_LEFT" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_small" />
        </receiver>
    </application>

</manifest>  

这是简化的 logcat 日志:

   onReceive()
   "myPackageName".Widget2.SCROLL_LEFT
   the text have been set
   "myPackageName".Widget2.SCROLL_LEFT appWidgetId = 268435456  

一切似乎都是正确的,但文字从未改变!

4

2 回答 2

3

中的关键widget是您必须在调用方法之前设置每View一个。在这里您可以从您的方法中调用:IntentupdateAppWidget()updateWidgetInstance(...)onReceive()

// action = the String that you get from your intent in the onRecive() method
// id = the id of the appWidget instance that you want to update
// you can get the id in the onReceive() method like this:
// int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
private static void updateWidgetInstance(Context context, AppWidgetManager manager, String action, int id) {
    RemoteViews remoteViews = updateCurrentWidget(context, id);
    remoteViews = setIntents(remoteViews, context, id);
    manager.updateAppWidget(id, remoteViews);
}

更新视图:(这里您也可以根据需要更改此实例的布局)

  private static RemoteViews updateCurrentWidget(Context context, int appWidgetId) {
    RemoteViews remoteViews;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small_layout);
        remoteViews.setTextViewText(R.id.widget_name_of_the_app, "Android OS");
        remoteViews.setTextViewText(R.id.widget_app_description, "best ever");
        remoteViews.setImageViewUri(R.id.widget_icon_of_the_app, ...);

    return remoteViews;
}

更新意图:

private static RemoteViews setIntents(RemoteViews rm, Context context, int appWidgetId) {

    Intent click = new Intent(context, Widget.class);
    click.setAction("[name of the action]");
    click.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent pendingIntent =
            PendingIntent.getBroadcast(context, appWidgetId, click, PendingIntent.FLAG_UPDATE_CURRENT);
    rm.setOnClickPendingIntent(R.id.button1, pendingIntent);

    return rm;
}
于 2013-05-24T05:45:45.473 回答
0

您正在 PendingIntent 标志中设置您的 appWidgetId。这是错误的,因为 PendingIntet 标志是有意义的,它们并不是为了保存一些数据。

您应该为此使用附加功能。

于 2012-07-12T14:19:12.200 回答