views:

126

answers:

1

Hey there,

I'm working on a widget that will change the data it is displaying when a user taps on it. This would normally be easy to deal with, but my app widget provider is handling multiple widget instances that have different sets of data. With that being said, when a user taps on the widget, a PendingIntent is launched that calls the changeData() method. Inside the intent that is launched is where I store the widget ID so changeData() knows which data set to use. At first I was storing widget IDs in the intent's extras, but I found out that there is a bug in how Android widgets handled widget intent extras so now I am trying to store the widget by passing a URL using the setData() method(formatted like content:widgetId I.E. content:24, etc.).

This is how I am setting the data for the PendingIntent:

Intent changeData = new Intent("com.tonycosentini.mintdroid.CHANGE_DATA");
changeData.setData(Uri.parse("content:" + currentWidgetId));
PendingIntent changeDataPendingIntent = PendingIntent.getBroadcast(this, 0, changeData, 0);

However once I try to test this, the onRecieve() method that is usually called when a widget is tapped is no longer called. Is there something I need to setup in my Android Manifest file for this work properly or is there something I'm doing completely wrong?

Thanks for taking the time to read this, Tony

A: 

You need to match

<data android:scheme="content" />

or

IntentFilter filter = new IntentFilter("com.tonycosentini.mintdroid.CHANGE_DATA");
filter.addDataScheme("content");

in your intent filter.

Depends if you declare the filter in xml or java.

http://developer.android.com/guide/topics/intents/intents-filters.html

An Intent object that contains a URI but no data type (and a type cannot be inferred from the URI) passes the test only if its URI matches a URI in the filter and the filter likewise does not specify a type. This will be the case only for URIs like mailto: and tel: that do not refer to actual data.

gabe
Thanks for the clarification, makes total sense. The only problem now though is when I add the data tag to my manifest, the widget disappears from the menu to choose a widget.
tonyc
This fixed it! Thanks gabe!Heads up: if you run into the same problem I did with the widget not appearing on the menu, make sure you make a separate intent filter for the data.
tonyc
Ah ok. Thanks for updating the issue.
gabe