뒤로가기버튼

반응형

public class MainActivity extends Activity{ 

다음에 삽입해주면 끝


private static final int MSG_TIMER_EXPIRED = 1;

	

    private static final int BACKEY_TIMEOUT = 2000;



    private boolean mIsBackKeyPressed = false;



    private long mCurrentTimeInMillis = 0;

    @Override 

    public void onBackPressed() { 

        if(mIsBackKeyPressed == false){ 

            mIsBackKeyPressed = true; 

              

            mCurrentTimeInMillis = Calendar.getInstance().getTimeInMillis(); 

              

            Toast.makeText(this, "뒤로 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT).show(); 

            startTimer(); 

        } else { 

            mIsBackKeyPressed = false; 

              

            if(Calendar.getInstance().getTimeInMillis() <= (mCurrentTimeInMillis + (BACKEY_TIMEOUT))){ 

                finish(); 

            } 

        } 

    } 

      

    private void startTimer(){ 

        mTimerHander.sendEmptyMessageDelayed(MSG_TIMER_EXPIRED, BACKEY_TIMEOUT); 

    } 

      

    private Handler mTimerHander = new Handler(){ 

        public void handleMessage(Message msg){ 

            switch(msg.what){ 

                case MSG_TIMER_EXPIRED: 

                { 

                    mIsBackKeyPressed = false; 

                } 

            break; 

            } 

        } 

    };






반응형

+ Recent posts