I'm writing an android soundboard wich allow the user to create multiple desktop widgets, one for each sound. I'm using an activity for the user to choose wich sound he wants to create the widget for. For each widget created i store a shared preference in the form of
key => "WIDGET_FILENAME_"+widgetId, value=> fileName
To play the sounds, i did override the onRecieve
method on the widgetProvider class. When the desktop widget is clicked, it triggers a broadcast to this method, wich gets the widget id from the intent and then loads the shared preference associated with the widget:
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
The problem is: The widgetId is always the same, no mather wich widget is clicked, causing the same sound to be played.
Any idea or guidance on this?
I had this Problem right now and with that code the problem is gone, Think that the widgetAppId in the Intent and the flag Create new will make this Intent always unique.
I had the same problem and solved it like this:
In your AppWidgetProviderClass, declare your Intent as follows:
...and PendingIntent
In the Activity class, after getting the appWidgetId you want to update:
...you can use a function similar to this one: