Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid appWidget multiple instances
    text
    copied!<p>This question is pretty much a duplicate of <a href="https://stackoverflow.com/questions/4225197/android-multiple-appwidgets-playing-diferent-sounds">Android - Multiple appWidgets playing different sounds</a>, but my code isn't working. I am adding an intent extra with the widget id, with log statements to prove it, but whenever I click on one of my widgets when there are multiple instances of it, I only get the last appWidgetId. It looks like the intent of the first widget is changing after the second one is added. If the code in that other post should work, then I must be overlooking something because I believe mine is set up pretty much the same way.</p> <pre><code> public class PhcaAppWidgetProvider extends AppWidgetProvider { private static final String ACTION_CLICK = "com.skipmorrow.phca.PhcaAppWidgetProvider.WIDGET_CLICKED"; private final String widgetPageName = "_widget"; private static final String PREFS_NAME = "PHCA"; private static final String PREF_PREFIX_KEY = "prefix_"; private static String MY_DEBUG = "PhcaAppWidget"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d(MY_DEBUG, "Provider.onUpdate"); } @Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction(); Log.d(MY_DEBUG, "Provider.onReceive(); action = " + intentAction); Bundle extras = intent.getExtras(); // make a list of all extras, just so I can see what is in there... if (extras!=null) { Set&lt;String&gt; ks = extras.keySet(); Iterator&lt;String&gt; iterator = ks.iterator(); while (iterator.hasNext()) { String s = iterator.next(); Log.d(MY_DEBUG, "Provider.Key found: " + s); } } else { Log.d(MY_DEBUG, "Provider.extras was null"); } int[] appWidgetIds1 = intent.getIntArrayExtra("appWidgetIds"); if (extras!=null &amp;&amp; extras.containsKey("appWidgetIds")){ // sometimes the intent comes back with an array of appWidgetIds, and sometimes the appWidgetId is stored // in the one extra "appWidgetId" Clicks never come with the array of appWidgetIds. They always come by // themselves Log.d(MY_DEBUG, "Provider.Intent extras does contain a key appWidgetIds"); if (appWidgetIds1!=null) Log.d(MY_DEBUG, "Provider.Intent int array appWidgetIds1 size is " + appWidgetIds1.length); for (int i = 0; i&lt;appWidgetIds1.length; i++) { int appWidgetId = appWidgetIds1[i]; Log.d(MY_DEBUG, "Provider.Intent appWidgetIds1[" + i + "] = " + appWidgetId); if (intentAction.equals("android.appwidget.action.APPWIDGET_UPDATE")) { Log.d(MY_DEBUG, "Provider.APPWIDGET UPDATE"); updateWidgetState(context, intentAction, appWidgetId); } else if (intentAction.equals("android.appwidget.action.APPWIDGET_DELETED")) { // do nothing } else { super.onReceive(context, intent); } } } else { String intentData = intent.getDataString(); Log.d(MY_DEBUG, "Provider.intent data = " + intentData); Integer appWidgetId = intent.getIntExtra("appWidgetId", -1); Log.d(MY_DEBUG, "Provider.appWidgetId = " + appWidgetId); if (intentAction.equals(ACTION_CLICK)) { Log.d(MY_DEBUG, "Provider.Clicked"); SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_WORLD_WRITEABLE); Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1); Log.d(MY_DEBUG, "Provider.ObjNum = " + objNum); if (objNum &gt; -1) { Log.d(MY_DEBUG, "Provider.Executing..."); PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum); pa.ExecuteActionFromWidgetClick(context); } } else { super.onReceive(context, intent); } } } public static void updateWidgetState(Context paramContext, String paramString, Integer appWidgetId) { Log.d(MY_DEBUG, "Provider.updateWidgetState; appWidgetId = " + appWidgetId + "; paramString = " + paramString); RemoteViews localRemoteViews = buildUpdate(paramContext, paramString, appWidgetId); ComponentName localComponentName = new ComponentName(paramContext, PhcaAppWidgetProvider.class); AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews); } private static RemoteViews buildUpdate(Context ctx, String paramString, Integer appWidgetId) { Log.d(MY_DEBUG, "Provider.buildUpdate(); appWidgetId = " + appWidgetId + "; paramString = " + paramString); RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.phca_appwidget); if (paramString.equals("android.appwidget.action.APPWIDGET_UPDATE")) { Log.d(MY_DEBUG, "Provider.buildUpdate().. APPWIDGET_UPDATE"); Intent intent = new Intent(ctx, PhcaAppWidgetProvider.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(ACTION_CLICK); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); Uri data = Uri.withAppendedPath( Uri.parse("ABCD" + "://widget/id/") ,String.valueOf(appWidgetId)); intent.setData(data); Log.d(MY_DEBUG, "Provider.Added intent extra \"" + AppWidgetManager.EXTRA_APPWIDGET_ID + "\" = " + appWidgetId); PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, appWidgetId, intent , PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.phca_appwidget_layout, pendingIntent); } if(paramString.equals(ACTION_CLICK)) { Log.d(MY_DEBUG, "Provider.buildUpdate().. CLICKED"); Toast.makeText(ctx, "ACTION_CLICK", Toast.LENGTH_LONG).show(); } return views; } } </code></pre> <p>I do have a configuration activity (a ListActivity) Here's my onListItemClick:</p> <pre><code>@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Toast.makeText(getApplicationContext(), "onListItemClick", Toast.LENGTH_SHORT); Log.d(MY_DEBUG, "Configurator.onListItemClick"); SharedPreferences.Editor prefs = getSharedPreferences(PREFS_NAME, 0).edit(); prefs.putInt(PREF_PREFIX_KEY + mAppWidgetId, position); prefs.commit(); Log.d(MY_DEBUG, "Configurator.Prefs.commit()"); // Push widget update to surface with newly set prefix Log.d(MY_DEBUG, "Configurator.calling updateWidgetState with appWidgetId = " + mAppWidgetId); PhcaAppWidgetProvider.updateWidgetState(getApplicationContext(), "android.appwidget.action.APPWIDGET_UPDATE", mAppWidgetId); Log.d(MY_DEBUG, "Configurator.updateWidgetState complete"); Intent resultValue = new Intent(); Uri data = Uri.withAppendedPath( Uri.parse("ABCD" + "://widget/id/") ,String.valueOf(mAppWidgetId)); resultValue.setData(data); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); Log.d(MY_DEBUG, "Configurator.Creating widget with id = " + mAppWidgetId); setResult(RESULT_OK, resultValue); finish(); } </code></pre> <p>I've been working on this for two days and I do hope that someone can point out the err of my ways. A virtual e-beer awaits the person who can solve this :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload