点击意图的小工具没有看到

时间:2013-02-23 11:21:51

标签: android android-intent android-widget android-pendingintent

我已经使用imageview构建了一个小部件。我想当用户点击imageview时,将启动一个活动。该活动从intent(字符串,整数)中获取额外内容。它几乎可以工作,但是当小部件更新时,旧的附加内容是获取,而不是AppWidgetProvider中给出的新附加内容。我怎么解决这个问题?

提前致谢, 西蒙

的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;
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:6)

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

尝试在点击意图中添加唯一。 例如:

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

这假设你知道appWidgetId。 否则你可以使用别的东西。像 UUID.randomUUID()。toString()

答案 1 :(得分:3)

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

所以,在这种情况下,它将是:

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

这种方式似乎用于打开活动的旧待处理意图会使用窗口小部件提供程序发送的新内容进行更新,包括内部意图。

答案 2 :(得分:0)

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

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);