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

时间:2014-03-21 15:39:14

标签: android

目前,我的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,我们不应该在其中放置可共享的静态数据?

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

1 个答案:

答案 0 :(得分:14)

RemoteViewsFactory - &gt;的AppWidgetProvider

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

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

当然,需要在清单中定义广播操作:

<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 - &gt; RemoteViewsFactory

与RemoteViewsFactory通信的一种方式(在您的情况下可能是最好的方法)是将信息发送到您传递给RemoteViewsAdapter的服务的意图中:

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可以轻松地从意图中检索信息并将其传递给RemoteViewsService.RemoteViewsFactory。

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