简体   繁体   中英

Stopping a Thread when ACTION_UP occurs

I'm having real trouble working with my thread. In simple terms I want a red square to appear on the screen when it is touched and disappear when nothing is touching the screen. I managed to get this to work.

Now I'm trying to implement it so that when the screen is touched the red square appears but then starts moving towards the finger. I thought I would do this by having a thread run when the screen is being touched which updates the X & Y coords of the square. I will try this once I get the thread starting and stopping on ACTION_DOWN and ACTION-UP.

I have tried to implement the thread that just prints to the system output, and it is called when the screen is touched but does not stop when I remove my finger. I tried only running the thread when a boolean is true. (this is set when the ACTION_DOWN happens. Then I set the ACTION_UP to make it false.

I read somewhere that it doesn't work because the thread has to complete before it stops or something. But I don't understand this.

If someone could show me where I'm going wrong and explain it to me It would be much appreciated.

package com.mr.mwood.thumbinput;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {
    boolean screenIsBeingTouched = false;

    Thread th=new Thread(){ 
        @Override
        public void run(){
            while (screenIsBeingTouched == true) {
                 try {
                    Thread.sleep(2000);
                    System.out.println("Thread is running ");
                 } catch (InterruptedException e) {
                    e.printStackTrace();
                 }
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    class SampleView extends SurfaceView {

        int timer =0;
        private final SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // CONSTRUCTOR
        public SampleView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.RED);
            paint.setStrokeWidth(3);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {   

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:{
                    screenIsBeingTouched = true;
                    System.out.println("screenIsBeingTouched = true");
                    Canvas canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.BLACK);
                    canvas.drawRect(30, 30, 80, 80, paint);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                    th.start();
                    break;
                }
                case MotionEvent.ACTION_UP:{
                    screenIsBeingTouched = false;
                    System.out.println("screenIsBeingTouched = false");
                    Canvas canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.BLACK);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                    break;
                }
            }

            return true;
        }

    }   
}

just update here and try

@Override
        public boolean onTouchEvent(MotionEvent event) {   

           //your code

            return false; // UPDATE HERE
        }

I have a few hints for you:

  1. You shouldn't stop thread like that. You should stop thread with th.interrupt(). It will throw an InterruptedException while being in sleep. When you don't have sleep you should check isInterrupted() flag.
  2. Remember you won't be able to do any UI changes from different thread than UI Thread. After all you will have to post some Runnable on Handler which will update the UI. You actually don't even need this different thread. You can solve everything using Handler.post + Handler.removeCallbacks only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM