android - Passing context to Handler -


Is it possible to argue an Android handler ?? I have two pieces of code

  New thread () {public void run () {for (;;) {uiCallback.sendEmptyMessage (0); Thread.Sleep (2000); // sleep for 2 seconds}}}. Start (); Personal Handler ui Callback = New Handler () {Public Zero Handle Message (Message Message) {// Add a New Blossom to Blossom Arrayist !! BlossomArrayList.add (New Blossom (Context, R. Drewable Blossom)); }};   

I certainly get an error because the handler method can not see my reference. This is probably due to this part of the code.

  Public Boardview (Reference Reference) {Super (Reference);   

The context is not visible anywhere else, and I am thinking that I can pass it as a logic for my handler.

EDIT: I am posting two main pieces of code to answer a question about why my blossom object needs references. I'm 100% sure myself & gt; & Gt; Maybe you can see and see what's going on.

  public class buys {private bitmap blossom; Personal Float Blossom X = 0; Personal Float Blossom = 0; Private random generator = new random (); Public Blossom (Reference Reference, Incredible) {blossom = bitmapfactory.decodeResource (context.getResources (), Drawable); BlossomX = generator.nextInt (300); } Public Bitmap getBitmap () {Return Blossom; } Public Float getBlossomX () {return blossomX; } Public float matching ballosum () {return blossom; } Public Zero Fall (Canvas Canvas, Float Box Y) {// Flower Falling Canvas Drave Draw Bitmate (Blossom, Blossom X, Blossom = Blossom + 3, Blank); // Detection of conflicts, is not working after // // Implementing randomized start position // If (blosomai + 29 == box y) // // // scanner = ); }}}} Increases the public class board Surfaceview's SurfaceHolder.Callback {Context mContext; Bitmap Box = (Bitmapfinterator.dtd Datasource (getResources (), R.drawable.box)); Private board thread thread; Private boat box_x = 140; Private boat box_y = 378; Private flat boxwidth = box .getWidth (); Private float boxhigh = box.gethit (); Private Array List & Lieutenant; Blossom & gt; BlossomArrayList = New Array's & lt; Blossom & gt; () ;; Boolean mode = false; Rectifty Boxtrack = New Rectum (box_x, box_a, box_x + boxwidth, box_a + box-height); Public Boardview (Reference Reference) {Super (Reference); // Surfaceholder provides canvas which we draw on getHolder (). AddClallback (this); // Control Dogs Thread = New BoardTrade (Together Holder (), this); // For Blossom // for the nearby variable (int i = 0; i & lt; = 3; i ++) // {//blossomArrayList.add (new blossom (references, R.drawable.blossom )); //} new thread () {public zero run () {for (;;) {uiCallback.sendEmptyMessage (0); Thread.Sleep (2000); // sleep for 2 seconds}}}. Start (); // Touchscreen Touch Events Set Focusable (True); } @ Override Public Wide On Draw (Canvas Canvas) {Canvas Drey Color (Color. WHITE); // Set the Draw Box and Start Location canvas.drawBitmap (box, box_x - (boxwidth / 2), box_e - (box-hit / 2), tap); (BluesomArrayList.get (i). (Canvas, box_a);}} @ override public boolean on touch event (motion event event) {if for ii = 0; i & lt; = 3; i ++) {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 (); Boxcrack.set (box_x, box_a, box_x + boxwidth, box_a + box-height); }} If (event.getAction () == MotionEvent.ACTION_UP} {mode = false; } Invalid (); Back true; } @ Override Public Wide Successful Changed (Surface holder holder, int format, int width, intensity height) {} @ Override made public void successful (Surfal holder holder) {thread. Starting (true); Thread.start (); } @ Override public nil surface was destroyed (ground holder holder) {thread.startRunning (wrong); Thread.stop (); } Private Handler ui Callback = New Handler () {Public Zero Handle Message (Message Message) {// Blossom Arrayist Add a New Blossom !! BlossomArrayList.add (New Blossom (Context, R. Drewable Blossom)); }}; }    

What if you create a subclass that extends the handler? In this way you can pass any parameter that you want.

But just out of curiosity, why is the blossom object reference object required? Usually it is best to separate your argument from the GUI dependency.

Comments