gpt4 book ai didi

java - 列表未更新的应用程序小部件 - 为什么会这样?

转载 作者:行者123 更新时间:2023-12-02 03:44:42 25 4
gpt4 key购买 nike

我有一个小部件,其中 ListView 中包含两个文本(为了允许长文本滚动)。当应用程序运行时,当我单击小部件中的“刷新小部件”按钮时,它会起作用。但是,当应用程序未运行时,小部件的标题会更新,而 ListView 不会更新。

这是我的代码

MyAppWidget.java

public class MyAppWidget extends AppWidgetProvider {
private static final String RELOAD_CLICKED = "MyAppWidget#btnReload";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, getClass());
}
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (RELOAD_CLICKED.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
updateAppWidget(context, appWidgetManager, appWidgetId, getClass());
}
}


static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Class<?> cls) {

WidgetViewModel viewModel = DbManager.getInstance(context).getRandomWidget();

// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_app_widget);
views.setTextViewText(R.id.txtTitle, viewModel.title);
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
String texts[] = new String[] {viewModel.widgetText1, widgetText2};
intent.putExtra("texts", texts);
views.setRemoteAdapter(R.id.widgetListTexts, intent);
views.setOnClickPendingIntent(R.id.btnReload,
getPendingSelfIntent(context, RELOAD_CLICKED, appWidgetId, cls));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);

Intent updateIntent = new Intent(WidgetRemoteViewsFactory.UPDATE_WIDGET_ACTION);
updateIntent.putExtra("texts", texts);
context.sendBroadcast(updateIntent);
}

private static PendingIntent getPendingSelfIntent(Context context, String action,
int appWidgetId, Class<?> cls) {
Intent intent = new Intent(context, cls);
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}

WidgetRemoteViewsFactory.java

public class WidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

public static final String UPDATE_WIDGET_ACTION = "WidgetRemoteViewsFactory#update";

private final Context context;
private final int appWidgetId;

private String[] texts;
private BroadcastReceiver receiver;

public WidgetRemoteViewsFactory(Context context, Intent intent) {
this.context = context;
texts = intent.getStringArrayExtra("texts");
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);

context.registerReceiver(receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
texts = intent.getStringArrayExtra("texts");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widgetListTexts);
}
}, new IntentFilter(UPDATE_WIDGET_ACTION));
}

@Override
public int getCount() {
return 2;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public RemoteViews getLoadingView() {
return null;
}

@Override
public RemoteViews getViewAt(int position) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
position == 0 ?
R.layout.app_widget_textview1
: R.layout.app_widget_textview2);

remoteView.setTextViewText(
position == 0 ?
R.id.widgetText1
: R.id.widgetText2,
texts[position]);

return remoteView;
}

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public void onCreate() {
}

@Override
public void onDataSetChanged() {
}

@Override
public void onDestroy() {
context.unregisterReceiver(receiver);
}
}

和WidgetService.java:

public class WidgetService extends RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new WidgetRemoteViewsFactory(this.getApplicationContext(), intent);
}
}

最佳答案

通过删除广播并仅将最后一个数据集保存在 updateAppWidget() 中并在调用 appWidgetManager.updateAppWidget 后还调用 appWidgetManager.notifyAppWidgetViewDataChanged 来修复更新列表。在工厂中,只需从文件加载最后一个数据集。

关于java - 列表未更新的应用程序小部件 - 为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814963/

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