我需要了解如何检测用户是否触摸过屏幕。
预期结果: - 每当用户触摸屏幕时,它应跳过启动画面并移至主要活动。
问题: - 当用户触摸屏幕时,会跳过启动画面,但在后台睡眠(10500)中,try块会继续运行,当它过去时,主活动会再次启动,即它会打开两次。
到目前为止我做了什么: - 我尝试做while循环并给出一个条件,如果条件满足(Touch)然后休息。但我似乎没有得到正确的工作条件。 启动画面代码: -
@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();
}
如果在try块中提供了Statement,那么如果条件满足则会中断。但是我不知道这个条件。需要帮助解决这个问题。 感谢。
答案 0 :(得分:1)
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();
}