我正在使用小部件制作应用程序。 小部件包含ImageView,当我点击它时,它将改变ImageView中的图像。
但是,当我创建多个实例并单击其中一个实例时,图像会在所有实例中更改
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;
public class MyWidgetProviderSimple extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
public static String MY_PREFS_NAME = "MyPrefsFile";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProviderSimple.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
updateAppWidget (context, appWidgetManager, widgetId, allWidgetIds);
}
}
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int widgetId, int [] allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_start);
Log.w("-widgetId:", String.valueOf(widgetId));
if (getWidgetValue(context, widgetId) == -1)
{
setWidgetValue(context, widgetId, 0);
}
// Set the text to the view with the id R.id.update
// instead of -1
remoteViews.setImageViewResource(R.id.widgetImageView, getImageToSet(context, widgetId));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProviderSimple.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetImageView, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
public int getImageToSet(Context context, int widgetId) {
int value = getWidgetValue (context, widgetId);
Log.w("-- getImageToSet:", String.valueOf(widgetId));
if (value == -1) {
Log.d("-- couldn't find", String.valueOf(widgetId));
return -1;
}
if (value == 5)
{
value = 0;
}
//TODO: must be replaced by Array list
int drawables = R.drawable.uui1 + value;
value++;
setWidgetValue(context, widgetId, value);
return drawables;
}
public static void setWidgetValue(Context context, int appWidgetId, int value) {
SharedPreferences.Editor editor = context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
editor.putInt(String.valueOf(appWidgetId), value);
editor.apply();
}
public static int getWidgetValue(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getInt(String.valueOf(appWidgetId), -1);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if( appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID )
{
super.onReceive(context, intent);
}
}
}
答案 0 :(得分:0)
“方法updateAppWidget(ComponentName提供程序,RemoteViews视图)将RemoteView设置为用于所提供的AppWidget提供程序的所有AppWidget实例。
您想要使用此方法:updateAppWidget(int appWidgetId,RemoteViews视图),它将RemoteView设置为用于指定的appWidgetId。
为此,您需要跟踪小部件的ID“
在这里查看其余的答案: multiple instances of widget with separated working clickable ImageView