更新小部件中的textView

时间:2012-07-12 07:20:15

标签: android android-intent widget textview remoteview

我读了很多Q& As但我找不到答案 可能是我实施的内容有误 问题是窗口小部件中的TextView没有更新。

逻辑是:

1. setOnClickPendingIntent buttononUpdate()的{​​{1}} 2.点击此按钮将广播intent并声明action 3.最后我将更新textView

onRecieve()的文字

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  

一切似乎都是正确的,但文字永远不会改变!

2 个答案:

答案 0 :(得分:1)

widget中的密钥是您必须在调用View方法之前设置每个Intent和每个updateAppWidget()。您可以在此处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;
}

答案 1 :(得分:0)

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

你应该使用额外的东西。

相关问题