如何在 onTouchEvent() 中检测屏幕是否被触摸
How to detect if screen is touched in onTouchEvent()
我需要了解如何检测用户是否触摸了屏幕。
预期结果:-每当用户触摸屏幕时,它应该跳过启动画面并移至主屏幕 activity。
问题:-每当用户触摸屏幕时,启动画面都会被跳过,但在后台睡眠(10500)中,try 块会保持 运行 并且随着它的流逝,Main Activity 会再次启动,即它会打开两次。
到目前为止我做了什么:-我尝试做 while 循环并给出一个条件,如果条件满足(Touch)然后 break.But 我似乎没有得到正确的工作条件。
初始屏幕代码:-
@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 块中提供了语句,那么如果满足条件,它将 break.But 未知条件 me.Need 帮助解决条件。
谢谢
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();
}
我需要了解如何检测用户是否触摸了屏幕。
预期结果:-每当用户触摸屏幕时,它应该跳过启动画面并移至主屏幕 activity。
问题:-每当用户触摸屏幕时,启动画面都会被跳过,但在后台睡眠(10500)中,try 块会保持 运行 并且随着它的流逝,Main Activity 会再次启动,即它会打开两次。
到目前为止我做了什么:-我尝试做 while 循环并给出一个条件,如果条件满足(Touch)然后 break.But 我似乎没有得到正确的工作条件。 初始屏幕代码:-
@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 块中提供了语句,那么如果满足条件,它将 break.But 未知条件 me.Need 帮助解决条件。 谢谢
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();
}