MeesterPatat
MeesterPatat

Reputation: 2811

Android Runnable not working on android 2.3?

I have the following code:

    btn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.post(pressed); // put the runnable 'pressed' in the
                                        // asynchrone message queue
                }

                return false;
            }
        });

private Runnable pressed = new Runnable() {

        @Override
        public void run() {


            if (btn.isPressed()) {
                sky.drawEverything();
btn.postDelayed(pressed, 10);
            }
        }

    };

This works like a charm for android 4.1.2, but if i try it on android 2.3 the Runnable seems to stay untriggered. Everything i'm using is api 1 or above, so why doesn't it work?

What is the alternative way of archieving the same result and making it work on older android versions?

Upvotes: 0

Views: 181

Answers (1)

Marko Lazić
Marko Lazić

Reputation: 883

He is not sure :) this code works perfectly on 4+ and in 2.3.6 I tested now is having issues with line if(btn.isPressed()), for some reason this doesn't return true. Here is a working hack, on Samsung Galaxy S 2.3.6

    btn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setPressed(true);
                v.post(pressed); // put the runnable 'pressed' in the
                                    // asynchrone message queue
            }
            if(event.getAction() == MotionEvent.ACTION_UP){
                v.setPressed(false);
            }

            return false;
        }
    });
}

private Runnable pressed = new Runnable() {

        @Override
        public void run() {
            if (btn.isPressed()) {
                //sky.drawEverything();
                btn.postDelayed(pressed, 10);
            }
            else {
                                    //Print something or whatever
            }
        }

    };

Hope this helps and enjoy your work.

Upvotes: 1

Related Questions