אVי
אVי

Reputation: 1973

Android widget send Data to onRecive via intent

I'm creating an android widget that by clicking, every instance of the widget will go to a different url.

I'm having a problem sending the url from the 'onUpdate' to the 'onReceive' method.

The onUpdate code:

 List<String> urls = Arrays.asList("google.com", "yahoo.com", "bing.com", "msn.com");

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    int cnt = 0;

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {
        // create some random data

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
        Log.w("WidgetExample", urls.get(cnt));
        // Set the text
        remoteViews.setTextViewText(R.id.urlData, urls.get(cnt));

        // Register an onClickListener
        Intent intent = new Intent(context, MyWidgetProvider.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        intent.putExtra("url",urls.get(cnt));

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.open, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);

        cnt++;
    }
}

The onReceive code:

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

    String TAG = "onReceive";

        Bundle extras = intent.getExtras();
        if(extras!=null) {
            String url = extras.getString("url");
            Log.d(TAG, "url is : "+url);
        }else {
            Log.d(TAG, "no url");
        }
}

The problem is that i allwas get the same url (the last one in the list - 'msn.com').

Thank's allot

Avi

Upvotes: 1

Views: 702

Answers (2)

nstosic
nstosic

Reputation: 2614

You're using PendingIntent.FLAG_UPDATE_CURRENT which, as the documentation states, "[...]if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent[...]"

So since you're trying to create multiple instances of the same class (PendingIntent) that flag is causing the unwanted behaviour. It is updating the previously instantiated object (the first time you called PendingIntent.getBroadcast(...) method) and changes the field(s) of that object and returns it. So that all your calls end up with the last extra (last URL you supplied) .

Upvotes: 1

Ivan
Ivan

Reputation: 978

I think that this append because you override everytime the intent storage in PendingIntent because the requestCode doesn't change.

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,**0**, intent, PendingIntent.FLAG_UPDATE_CURRENT);

If you want set more PendingIntent you must change the requestCode (0 in your case)

Try

 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, cnt, intent, PendingIntent.FLAG_UPDATE_CURRENT);

In this way all PendingIntents are different...

Upvotes: 1

Related Questions