我已经构建了一个带有 imageview 的小部件。我希望当用户单击图像视图时,将启动一个活动。该活动从意图(字符串,整数)中获得额外信息。它几乎可以工作,但是当小部件更新时,会获取旧的附加功能,而不是 AppWidgetProvider 中给出的新附加功能。我怎么解决这个问题?
在此先感谢,西蒙
应用小部件提供者:
package com.aquariumzoekenpro.android;
import java.io.InputStream;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.aquariumzoekenpro.android.VissenDB.Vis;
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
public static final String PREFS_NAME = "Vissen";
static SharedPreferences sharedPreferences;
PendingIntent pendingintent;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
// final int N = appWidgetIds.length;
sharedPreferences = context.getSharedPreferences(PREFS_NAME, 0);
int rbitem = sharedPreferences.getInt("RBitem", 0);
// int appWidgetId = appWidgetIds[i];
ComponentName thisWidget = new ComponentName(context, Widget.class);
VissenDB db = new VissenDB();
int maxlength = db.lijst.size();
Random r = new Random();
int random = r.nextInt(maxlength - 0);
Vis v = db.lijst.get(random);
int imageresource = context.getResources().getIdentifier(v.afbeelding, "drawable", context.getPackageName());
InputStream is = context.getResources().openRawResource(imageresource);
Bitmap bm = BitmapFactory.decodeStream(is);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setImageViewBitmap(R.id.widget_imageview, bm);
Intent clickintent = new Intent(context, VissenBeschrijving.class);
clickintent.putExtra("list", rbitem);
String text = "";
if (rbitem == 0){
text = v.naam;
}else{
text = v.latijnseNaam;
}
clickintent.putExtra("selected", text);
clickintent.putExtra("Herkomst", "Encyclo");
clickintent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingintent = PendingIntent.getActivity(context, 1, clickintent, 0);
views.setOnClickPendingIntent(R.id.widget_imageview, pendingintent);
appWidgetManager.updateAppWidget(thisWidget, views);
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
}
VissenBeschrijving(接收活动):
i = this.getIntent();
VissenDB db = new VissenDB();
String h = i.getStringExtra("Herkomst");
if (i != null && h !=null){
text = i.getStringExtra("selected");
RBitem = i.getIntExtra("list", -1);
if (RBitem == 0){
//Nederlandse naam
for (VissenDB.Vis v : db.lijst) {
if (v.naam.equals(text)){
vis = v;
break;
}
}
}
else{
//Latijnse naam
for (VissenDB.Vis v : db.lijst) {
if (v.latijnseNaam.equals(text)){
vis = v;
break;
}
}
}
}