What is the correct way for sharing data among AppWidgetProvider and RemoteViewsService.RemoteViewsFactory

前端 未结 1 1729
日久生厌
日久生厌 2021-01-03 07:31

Currently, my AppWidgetProvider is having a static data. It is used for information passing around AppWidgetProvider & RemoteViewsService

相关标签:
1条回答
  • 2021-01-03 07:57

    RemoteViewsFactory -> AppWidgetProvider

    The communication from the RemoteViewsFactory to the AppWidgetProvider can be done using Broadcasts, e.g. like this:

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

    The AppWidgetProvider receives the event like so:

    @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
    

    Of course the broadcast action needs to be defined in the 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

    One way to communicate with the RemoteViewsFactory (and in your case probably the best one) is to send the information in the intent of the service you're passing to the 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);
    

    The RemoteViewsService can easily retrieve the information from the intent and pass it along to the RemoteViewsService.RemoteViewsFactory.

    I'm not 100% sure when and how your widget decides to sort the data but I'd assume if the user picks a column to sort then you have to go through the update cycle with notifyAppWidgetViewDataChanged and then you would pass that column along. If you need that information later on then you'd have to store the information somehow (SharedPreferences).

    0 讨论(0)
提交回复
热议问题