I tried to solve my problem using the link
update - I realized that something is wrong with the installer of the expecting intent - every time I click on the image - the intent sends the details of the last installed widget - that other expecting intentions, which, if defined in pre-added widgets, were launched by newer widgets
I have an application widget that displays an image selected by the user. (many widgets - many images) my problem: no matter which widget I click on the screen, only the last added widget is updated: here is my code
The code of my xml widget provider
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget_Test" android:minHeight="146dip" android:minWidth="146dip" android:updatePeriodMillis="0" />
My manifest xml code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.BIND_REMOTEVIEWS" > </uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name=".Activities.WidgetConfig" android:screenOrientation="portrait" > <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </activity> <receiver android:name=".Simple.Widget" android:label="@string/app_widget_Test" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_Test_provider" /> </receiver> <service android:name=".Simple.Widget$TestWidgetService" /> </application> </manifest>
Widget provider
public class Widget extends AppWidgetProvider { public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget"; public static int[] widgets; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Intent svcIntent = new Intent(context, TESTWidgetService.class); widgets = appWidgetIds; context.startService(svcIntent); } public static class TESTWidgetService extends Service { @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId);
my configuration
public class WidgetConfig extends ListActivity // implements { private Bundle m_extras; private ArrayList<Test> m_tests = null; private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; ImageAdapter m_adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imagelist); Resources res = getResources(); Mngr mngr = Mngr.getInstance(); mngr.setResources(res); Context ctx = getApplicationContext(); mngr.setContext(ctx); getTests(); m_adapter = new ImageAdapter(ctx, R.layout.row, m_tests); ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT; m_adapter.getImageDownloader().setMode(mode); setListAdapter(m_adapter); Intent intent = getIntent(); m_extras = intent.getExtras(); } private void getTests() { m_tests = new ArrayList<Test>(); Resources res = getResources(); String[] TestNames = res.getStringArray(R.array.fav_Test_array); TypedArray imgs = getResources().obtainTypedArray( R.array.fav_Test_integer); for (int i = 0; i < TestNames.length; i++) { Test o1 = new Test(); String TestName = TestNames[i]; int resID = imgs.getResourceId(i, -1); o1.setTestName(TestName); o1.setIMGID(resID); m_tests.add(o1); } imgs.recycle(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ListAdapter adapt = l.getAdapter(); Object obj = adapt.getItem(position); if (obj != null) { Mngr mngr = Mngr.getInstance(); Context context = getApplicationContext(); String key = context.getString(R.string.TestWidget_string) + "_" + AppWidgetManager.EXTRA_APPWIDGET_ID; Test Test = (Test) obj; String val = Test.getIMGID().toString(); mngr.putString(context, key, val); updateWidget(); } super.onListItemClick(l, v, position, id); } private void updateWidget() { Context ctx = getApplicationContext(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx); setResult(RESULT_CANCELED); if (m_extras != null) { Intent resultValue = new Intent(); int numberOfWidgets = m_extras.getInt( AppWidgetManager.EXTRA_APPWIDGET_ID + "number", AppWidgetManager.INVALID_APPWIDGET_ID); for (int i = 0; i < numberOfWidgets; i++) { String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID + i; mAppWidgetId = m_extras.getInt(stringID, AppWidgetManager.INVALID_APPWIDGET_ID); Uri data = Uri.withAppendedPath(Uri.parse("ABCD" + "://widget/id/"), String.valueOf(mAppWidgetId)); resultValue.setData(data); RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.widget_Test); Mngr.getInstance().updateTestWidget(ctx, views); appWidgetManager.updateAppWidget(mAppWidgetId, views); resultValue.putExtra(stringID, mAppWidgetId); } setResult(RESULT_OK, resultValue); finish(); } } }
My Mngr Code
public void updateTestWidget(Context context, RemoteViews remoteViews) { String key = context.getString(R.string.TestWidget_string) + "_" + AppWidgetManager.EXTRA_APPWIDGET_ID; String s = getString(context, key, ""); if (s != null && s.equals("") == false) { int resID = Integer.valueOf(s); remoteViews.setImageViewResource(R.id.TestWidgetImage, resID); } } public void pushUpdate(RemoteViews remoteView, Context ctx, Class<?> cls) { ComponentName myWidget = new ComponentName(ctx, cls); AppWidgetManager manager = AppWidgetManager.getInstance(ctx); manager.updateAppWidget(myWidget, remoteView); }