2015-04-07 83 views
0

我需要了解如何來檢測,如果用戶已經觸摸屏幕。如何,如果屏幕被觸摸的onTouchEvent檢測()

預期結果:-Whenever用戶觸摸屏幕應該跳過開機畫面,並移動到主要活動。

問題:無論用戶何時觸摸屏幕,初始屏幕都會跳過,但在try塊中的背景睡眠(10500)會繼續運行,並且隨着它過去,主活動再次開始,即它會打開兩次。

我有什麼迄今所做的:-I嘗試做while循環,並給了一個條件,如果條件滿足(的觸摸),然後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(); 
} 

if語句在try塊提供,所以如果條件滿足,將break.But的條件是未知的me.Need幫助的條件。 謝謝。

回答

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(); 
} 
+0

非常感謝!這真的幫助! –

相關問題