在小部件中更新textView

3

我阅读了很多问答,但是没有找到我的答案。
也许是我的实现有问题。
问题在于我的小部件中的TextView没有得到更新。

逻辑如下:

1. 在 onUpdate() 中对一个特定的 button 设置 setOnClickPendingIntent
2. 点击此按钮将广播声明了一个 actionintent
3. 最后在 onRecieve() 中更新 textView 的文本内容

Widget2.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);
}  

manifest.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  

一切似乎都正确,但文本从未更改!


由于您没有调用super.onReceive,因此您的onUpdate方法从未被调用。 - Ran
它被称为,我忘记复制它了。现在它是正确的。 - SAbbasizadeh
2个回答

3

widget中的关键是在调用updateAppWidget()方法之前设置每个View和每个Intent。您可以从onReceive()方法中调用updateWidgetInstance(...)

// 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;
}

0

你正在将appWidgetId设置在PendingIntent标志中。这是错误的,因为PendingIntent标志具有特定含义,它们不是用来保存数据的。

你应该使用extras来保存数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接