I have build a widget with an imageview. I want when the user clicks on the imageview, an activity will be started. That activity gets extras from the intent(strings, ints). It almost works, but when the widget updates, the old extras are get, not the new ones, given in the AppWidgetProvider. How can I solve this problem?
Thanks in advance, Simon
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 (receiving activity):
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;
}
}
}
}
If I recall correctly this is because it will reuse the old intent if nothing has change in the content.
Try adding something unique to your click intent. For example:
clickintent.setData(Uri.withAppendedPath(Uri.parse("myapp://widget/id/#togetituniqie" + appWidgetId), String.valueOf(appWidgetId)));
This is assuming you know the appWidgetId. Otherwise you can use something else unique. Like UUID.randomUUID().toString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With