Sarvesh Tendulkar
Sarvesh Tendulkar

Reputation: 13

How to detect if screen is touched in onTouchEvent()

I need to understand on how to detect if user has touched the screen.

Expected Result:-Whenever user touches screen it should skip Splash Screen and move to main activity.

Problem:-Whenever user touches the screen Splash Screen is skipped but in the background sleep(10500) in try block keeps running and as it elapses the Main Activity starts again i.e. it opens two times.

What have I done so far:-I tried do while loop and gave a condition,if the condition is satisfied(of Touch) then break.But I don't seem to get the correct working condition. Splash Screen Code:-

@Override
protected void onCreate(Bundle splashState) {
    // TODO Auto-generated method stub
    super.onCreate(splashState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    ourSong.start();
    Thread timer = new Thread() {
        public void run() {
            do
            {
            try {
                //if(onTouchEvent(null))
                //  break;
                sleep(10500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                startActivity(new Intent("com.first.MAINACTIVITY"));
            }
        }while(false);
        }
    };

    timer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        startActivity(new Intent("com.first.MAINACTIVITY"));
        finish();
        ourSong.release();
    }
    return super.onTouchEvent(event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
    ourSong.release();
}

If Statement is provided in the try block so if condition is satisfied it would break.But the condition is unknown to me.Need help with the condition. Thanks.

Upvotes: 0

Views: 1849

Answers (1)

Kartik
Kartik

Reputation: 7917

private boolean isSplashRunning = true;

@Override
protected void onCreate(Bundle splashState) {
    // TODO Auto-generated method stub
    super.onCreate(splashState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    ourSong.start();
    Thread timer = new Thread() {
        public void run() {
            do
            {
            try {
                //if(onTouchEvent(null))
                //  break;
                sleep(10500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if(isSplashRunning)
                    startActivity(new Intent("com.first.MAINACTIVITY"));
            }
        }while(false);
        }
    };

    timer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        isSplashRunning = false; //or in onPause
        startActivity(new Intent("com.first.MAINACTIVITY"));
        finish();
        ourSong.release();
    }
    return super.onTouchEvent(event);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    isSplashRunning = false;
    super.onPause();
    finish();
    ourSong.release();
}

Upvotes: 1

Related Questions