gpt4 book ai didi

android - 更新小部件中的 textView

转载 作者:太空狗 更新时间:2023-10-29 14:26:56 25 4
gpt4 key购买 nike

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

逻辑是这样的:

1.setOnClickPendingIntentonUpdate()
中的特定 button2.单击此按钮将广播一个带有已声明的action
intent3. 最后,我将在 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);
}

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

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

最佳答案

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

更新View:(这里也可以根据需要改变这个实例的布局)

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

更新 Intent :

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

关于android - 更新小部件中的 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11446832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com