gpt4 book ai didi

android - 在 AppWidgetProvider 和 RemoteViewsService.RemoteViewsFactory 之间共享数据的正确方法是什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:06 26 4
gpt4 key购买 nike

目前,我的 AppWidgetProvider有一个静态数据。用于传递信息 AppWidgetProvider & RemoteViewsService.RemoteViewsFactory

public class MyAppWidgetProvider extends AppWidgetProvider {
// Key will be widget id
private static Map<Integer, Holder> holderMap = new java.util.concurrent.ConcurrentHashMap<Integer, Holder>();

public static int getClickedColumn(int appWidgetId) {
Holder holder = holderMap.get(appWidgetId);
if (holder == null) {
return -1;
}
return holder.clickedColumn;
}

public class AppWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
@Override
public void onDataSetChanged() {
int clickedColumn = MyAppWidgetProvider.getClickedColumn(mAppWidgetId);

调用 AppWidgetProvider的静态方法在大多数情况下都能正常工作。

但是,有时,如果我将小部件放在主屏幕上,让它在那里停留几个小时。当我回来并搜索 ListView 时,我可能会随机得到以下错误。

java.lang.ExceptionInInitializerError
at org.yccheok.project.gui.widget.AppWidgetRemoteViewsFactory.onDataSetChanged(AppWidgetRemoteViewsService.java:390)
at android.widget.RemoteViewsService$RemoteViewsFactoryAdapter.onDataSetChanged(RemoteViewsService.java:142)
at com.android.internal.widget.IRemoteViewsFactory$Stub.onTransact(IRemoteViewsFactory.java:49)
at android.os.Binder.execTransact(Binder.java:367)
at dalvik.system.NativeStart.run(Native Method)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at org.yccheok.project.gui.widget.MyAppWidgetProvider.<clinit>(MyAppWidgetProvider.java:564)

来自 <clinit> , 我怀疑 MyAppWidgetProvider被操作系统破坏了吗?这个原因AppWidgetRemoteViewsFactory想在调用静态函数之前执行类初始化?

这是否意味着,MyAppWidgetProvider可以随时被操作系统销毁,我们不应该在其中放置可共享的静态数据吗?

如果是这样,在 AppWidgetProvider 之间共享数据的正确方法是什么?和 RemoteViewsService.RemoteViewsFactory ? (除了使用文件或 SharedPreferences)

最佳答案

RemoteViewsFactory -> AppWidgetProvider

从 RemoteViewsFactory 到 AppWidgetProvider 的通信可以使用广播来完成,例如像这样:

Intent intent = new Intent(ACTION_PROGRESS_OFF);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);

AppWidgetProvider 像这样接收事件:

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

// here goes the check if the app widget id is ours

final String action = intent.getAction();
if (ACTION_PROGRESS_OFF.equals(action)) {
// turn off the refresh spinner

当然广播 Action 需要在manifest中定义:

<receiver
android:name="...">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="myPackage.ACTION_PROGRESS_OFF" />
</intent-filter>
<meta-data ... />
</receiver>

AppWidgetProvider -> RemoteViewsFactory

与 RemoteViewsFactory 通信的一种方式(在您的情况下可能是最好的方式)是按照您要传递给 RemoteViewsAdapter 的服务的 Intent 发送信息:

Intent intentRVService = new Intent(context, RemoteViewsService.class);
intentRVService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

// let's put in some extra information we want to pass to the RemoteViewsFactory
intentRVService.putExtra("HELLO", "WORLD");

// when intents are compared, the extras are ignored, so we need to
// embed the extras into the data so that the extras will not be ignored
intentRVService.setData(Uri.parse(intentRVService.toUri(Intent.URI_INTENT_SCHEME)));

rv.setRemoteAdapter(appWidgetId, R.id.my_list, intentRVService);
rv.setEmptyView(R.id.my_list, android.R.id.empty);

// create the pending intent template for individual list items
...
rv.setPendingIntentTemplate(R.id.my_list, pendingIntentTemplate);

appWidgetMgr.notifyAppWidgetViewDataChanged(appWidgetId, R.id.my_list);

RemoteViewsService 可以轻松地从 Intent 中检索信息并将其传递给 RemoteViewsService.RemoteViewsFactory。

我不是 100% 确定您的小部件何时以及如何决定对数据进行排序,但我假设如果用户选择要排序的列,那么您必须使用 notifyAppWidgetViewDataChanged 完成更新周期,然后您将通过它列沿。如果您稍后需要该信息,则必须以某种方式存储该信息 (SharedPreferences)。

关于android - 在 AppWidgetProvider 和 RemoteViewsService.RemoteViewsFactory 之间共享数据的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563153/

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