将上下文传递给处理程序

问题描述 投票:0回答:3

是否可以将参数传递给 Android 处理程序?我有两段代码。

new Thread(){
        public void run(){
            for(;;){
                uiCallback.sendEmptyMessage(0);
                Thread.sleep(2000); //sleep for 2 seconds
            }
        }
    }.start();


    private Handler uiCallback = new Handler(){
    public void handleMessage(Message msg){
        //add a new blossom to the blossom ArrayList!!
        blossomArrayList.add(new Blossom(context, R.drawable.blossom));
    }
};

我当然得到一个错误,因为 Handler 方法看不到我的上下文。这可能是因为这段代码

public BoardView(Context context){
    super(context);

上下文在其他地方不可见,我想知道我是否可以将它作为参数传递给我的处理程序。

编辑:我发布了两个主要代码片段来回答关于为什么我的 Blossom 对象需要上下文的问题。我自己也不是 100% 确定 >.> 也许你可以看看发生了什么事。

public class Blossom{
private Bitmap blossom;
private float blossomX = 0;
private float blossomY = 0;
private Random generator = new Random();

public Blossom(Context context, int drawable)
{
    blossom = BitmapFactory.decodeResource(context.getResources(), drawable); 
    blossomX = generator.nextInt(300);
}

public Bitmap getBitmap()
{
    return blossom;
}

public float getBlossomX()
{
    return blossomX;
}

public float getBlossomY()
{
    return blossomY;
}

public void Fall(Canvas canvas, float boxY)
{
    //draws the flower falling
    canvas.drawBitmap(blossom, blossomX,
            blossomY = blossomY+3 , null);

    //collision detection, currently not working after 
    //implementing random start location

    //if(blossomY + 29 == boxY)
    //{
        //canvas.drawBitmap(blossom,0,0,null);
    //}

}
}


public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
Context mContext;

Bitmap box = 
    (BitmapFactory.decodeResource
            (getResources(), R.drawable.box));

private BoardThread thread;
private float box_x = 140;
private float box_y = 378;
private float boxWidth = box.getWidth();
private float boxHeight = box.getHeight();
private ArrayList<Blossom> blossomArrayList = new ArrayList<Blossom>();;

boolean mode = false;

RectF boxRect = new RectF(box_x,box_y, box_x + boxWidth, box_y + boxHeight);

public BoardView(Context context){
    super(context);

    //surfaceHolder provides canvas that we draw on
    getHolder().addCallback(this);

    // controls drawings
    thread = new BoardThread(getHolder(),this);

    //pass variables to instance of Blossom
    //for(int i = 0; i <= 3; i++)
    //{
        //blossomArrayList.add(new Blossom(context, R.drawable.blossom));
    //}

    new Thread(){
        public void run(){
            for(;;){
                uiCallback.sendEmptyMessage(0);
                Thread.sleep(2000); //sleep for 2 seconds
            }
        }
    }.start();

    //intercepts touch events
    setFocusable(true);

}

@Override

public void onDraw(Canvas canvas){
    canvas.drawColor(Color.WHITE);  


    //draw box and set start location
    canvas.drawBitmap(box, box_x - (boxWidth/2), 
            box_y - (boxHeight/2), null);

    for(int i = 0; i<= 3; i++)
    {
        blossomArrayList.get(i).Fall(canvas, box_y);
    }

}

@Override
public boolean onTouchEvent(MotionEvent event){

    if(event.getAction() == MotionEvent.ACTION_DOWN){
        if(boxRect.contains(event.getX(),event.getY())){
            mode = true;
        }
    }

    if(event.getAction() == MotionEvent.ACTION_MOVE) {
        if(boxRect.contains(event.getX(),event.getY())){
            mode = true;
        }
        if(mode == true){
            box_x = (int)event.getX();
            boxRect.set(box_x,box_y, box_x + boxWidth, box_y + boxHeight);
        }

    }

    if(event.getAction() == MotionEvent.ACTION_UP){
        mode = false;
    }

    invalidate();

    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, 
        int format, int width, int height ){

}

@Override
public void surfaceCreated(SurfaceHolder holder){
    thread.startRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    thread.startRunning(false);
    thread.stop();
}

private Handler uiCallback = new Handler(){
    public void handleMessage(Message msg){
        //add a new blossom to the blossom ArrayList!!
        blossomArrayList.add(new Blossom(context, R.drawable.blossom));
    }
};


}
android view handler android-context
3个回答
6
投票

创建一个扩展Handler 的类,并存储对上下文的弱引用。这将有助于防止一些内存问题。

public class SomeHandler extends Handler {

    // A weak reference to the enclosing context
    private WeakReference<Context> mContext;

    public SomeHandler (Context context) {
        mContext = new WeakReference<Context>(context);
    }

    public void handleMessage(Message msg) {

        // Get an actual reference to the DownloadActivity
        // from the WeakReference.
        Context context=mContext.get();
        ...
    }
}

4
投票

如果您创建一个扩展 Handler 的子类怎么办?这样你就可以传递任何你想要的参数。

但出于好奇,为什么 Blossom 对象需要上下文对象?通常最好将您的逻辑与 GUI 依赖项分开。


0
投票

对于上下文,activity.getBaseContext() 对我有用。 :)

final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
  public void run() {
    handler.post(new Runnable() {
      public void run() {
        notification[0] = mBuilder.build();
        notificationManagerCompat[0] = 
NotificationManagerCompat.from(activity.getBaseContext());
    
    notificationManagerCompat[0].notify(1, notification[0]);
      }
    });
  }
}, cal.getTimeInMillis() - cur_cal.getTimeInMillis() - 120000);
© www.soinside.com 2019 - 2024. All rights reserved.