Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an array of integers from an AppWidget to a preexisting RemoteViewsService to be rendered by the RemoteViewsFactory

I have an AppWidget that has a StackView contained within it. Along with the AppWidget, I have a service that is started when a user adds my AppWidget to their homescreen that polls for new data every two hours. When the service finds new data, I need for it alert my AppWidget, and also pass that data to the RemoteViewsService (which has my RemoteViewsFactory) so that it can create the necessary views for the new data.

Currently, when the service finds new data, I have it broadcast to my AppWidget that new data has been found, passing that data as an array of integers within an intent.

My onReceive method in my AppWidgetProvider pulls that data out, and I then go through the process of setting up a new intent that is passed to my RemoteViews for the AppWidget. Sample code below:

public void onReceive( Context context, Intent intent )
{
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds( new ComponentName( context,  SampleAppWidgetProvider.class ) );

    int[] sampleData = intent.getIntArrayExtra( Intent.EXTRA_TITLE );
    for ( int i = 0; i < appWidgetIds.length; i++ )
    {
        // RemoteViewsFactory service intent
        Intent remoteViewsFactoryIntent = new Intent( context, SampleAppWidgetService.class );
        remoteViewsFactoryIntent.putExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i] );
        remoteViewsFactoryIntent.setData( Uri.parse( remoteViewsFactoryIntent.toUri( Intent.URI_INTENT_SCHEME ) ) );
        remoteViewsFactoryIntent.putExtra( Intent.EXTRA_TITLE, sampleData );

        RemoteViews rv = new RemoteViews( context.getPackageName(), R.layout.widget_layout );

        // Sync up the remoteviews factory
        rv.setRemoteAdapter( appWidgetIds[i], R.id.sample_widget, remoteViewsFactoryIntent );           
        rv.setEmptyView( R.id.sample_widget, R.id.empty_view );

        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
    }

    super.onUpdate( context, appWidgetManager, appWidgetIds );
}

This works the first time. The data is displayed by the RemoteViewsService/RemoteViewsFactory. Subsequent new data doesn't hit the RemoteViewsFactory's constructor because the service for that factory is already running.

How do I go about updating the data? I feel like I should be using onDataSetChanged, but how would I access the intent that I passed from the AppWidget's onReceive?

I'd appreciate any insight as to how to go about this properly. Thanks.

like image 268
Steven Avatar asked Sep 09 '11 20:09

Steven


4 Answers

My solution to an integer array being passed to the RemoteViewsFactory was solved using a BroadcastReceiver. I extended my RemoteViewsFactory to implement the BroadcastReceiver (specifically onReceive), and registered it with my application in the constructor for the factory (this also means I unregistered it in the onDestroy). With that, I was able to broadcast the intent with the integer array that I had in the onReceive of my AppWidgetProvider and receive it within the RemoteViewsFactory.

Make sure to also call the AppWidgetManager's notifyAppWidgetViewDataChanged so that the RemoteViewsFactory knows that the data it was using previously has been invalidated and a new integer array is presented to create new RemoteViews off of.

like image 152
Steven Avatar answered Sep 20 '22 23:09

Steven


To elaborate further on Steve's solution above for the benefit of arne.jans and others, I implemented Steven's solution by:

  • Extracting the StackRemoteViewsFactory into a class file of its own
  • Extending it with a BroadcastReceiver
  • Implementing the onReceive() method to save new data to a static variable in which the StackRemoteViewsFactory class could also access when its getViewAt required the data
  • Passing in all widget ids via the Intent so I could call a notifyAndUpdate of ALL widgets in the onReceive() method.
  • Finally I added the standard definition for a receiver to the AndroidManifest.xml
like image 23
JavaJedi Avatar answered Sep 18 '22 23:09

JavaJedi


I did not extend RemoteViewFactory with a BroadcastReceiver, but implement dynamic BroadcastReceiver on it to pass a string from AppWidgetProvider to RemoteViewFactory.

like this:

public class MyWidgetProvider extends AppWidgetProvider {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        String action = intent.getAction();
        if (action.equals("android.appwidget.action.APPWIDGET_UPDATE")) {
            .....
        } else if (action.equals("some.string.you.want.ACTION_TODO")) {
            String url = intent.getStringExtra("some.string.you.want.EXTRA_URL");

            // Send broadcast intent to pass mUrl variable to the MyContentFactory.
            Intent broadcastIntent = new Intent("some.string.you.want.ACTION_UPDATE_URL");
            broadcastIntent.putExtra("some.string.you.want.EXTRA_URL", url);
            context.sendBroadcast(broadcastIntent);
        }
        .....
    }
}

public class MyContentFactory implements RemoteViewsService.RemoteViewsFactory {
    private Context mContext;
    private String mUrl; // target to update
    private BroadcastReceiver mIntentListener;

    public MyContentFactory(Context context, Intent intent) {
        mContext = context;
        setupIntentListener();
    }

    @Override
    public void onDestroy() {
        teardownIntentListener();
    }

    private void setupIntentListener() {
        if (mIntentListener == null) {
            mIntentListener = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // Update mUrl through BroadCast Intent
                    mUrl = intent.getStringExtra("some.string.you.want.EXTRA_URL");
                }
            };
            IntentFilter filter = new IntentFilter();
            filter.addAction("some.string.you.want.ACTION_UPDATE_URL");
            mContext.registerReceiver(mIntentListener, filter);
        }
    }

    private void teardownIntentListener() {
        if (mIntentListener != null) {
            mContext.unregisterReceiver(mIntentListener);
            mIntentListener = null;
        }
    }
}
like image 3
sMiLo Avatar answered Sep 20 '22 23:09

sMiLo


Another alternative is to use the file system or an sqlite database, e.g.

In AppWidgetProvider:

 File file = new File(context.getCacheDir(), "filename that service also knows");
 writeFile(file, "my content");  // impl. not shown; uses BufferedWriter.java

In RemoteViewsService:

 File file = new File(context.getCacheDir(), "filename that service also knows");
 if (file.exists()) {
     // handle params contained within file
     file.delete(); // clear for next run
 } 
like image 1
larham1 Avatar answered Sep 18 '22 23:09

larham1