4

我已经构建了一个带有 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;
                }
            }
        }
    }
4

3 回答 3

7

如果我没记错的话,这是因为如果内容没有任何变化,它将重用旧意图。

尝试为您的点击意图添加一些独特的东西。例如:

clickintent.setData(Uri.withAppendedPath(Uri.parse("myapp://widget/id/#togetituniqie" + appWidgetId), String.valueOf(appWidgetId)));

这是假设您知道 appWidgetId。否则,您可以使用其他独特的东西。像UUID.randomUUID().toString()

于 2013-02-23T11:41:33.900 回答
5

对我来说,只需将 FLAG_UPDATE_CURRENT 添加到小部件提供程序中的待处理意图即可。

因此,在这种情况下,它将是:

pendingintent = PendingIntent.getActivity(context, 1, clickintent, PendingIntent.FLAG_UPDATE_CURRENT);

这样,用于打开活动的旧待处理意图似乎会使用从小部件提供者发送的新内容进行更新,包括内部意图。

于 2014-11-03T18:22:54.747 回答
0

我遇到了同样的问题,我解决了简单地添加PendingIntent.FLAG_ONE_SHOT标志(如果你希望意图被触发一次)或PendingIntent.FLAG_UPDATE_CURRENT(如果你希望它将在每个点击事件上执行):

Intent intent = new Intent(context, YourWidgetProvider.class);
intent.setAction(ACTION_NAME);
intent.putExtra("extraName", extraValue);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
remoteViews.setOnClickPendingIntent(R.id.view_id, pendingIntent);
于 2017-08-24T10:59:55.410 回答