백버튼 두번 눌러 앱종료하기


 

2초 이내에 백버튼을 2번 누르면 앱이 종료된다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //앱종료시간체크
    long backKeyPressedTime;    //앱종료 위한 백버튼 누른시간    
    ...
 
    //뒤로가기 2번하면 앱종료
    @Override
    public void onBackPressed() {
        //1번째 백버튼 클릭
        if(System.currentTimeMillis()>backKeyPressedTime+2000){
            backKeyPressedTime = System.currentTimeMillis();
            Toast.makeText(this, getString(R.string.APP_CLOSE_BACK_BUTTON), Toast.LENGTH_SHORT).show();
        }
        //2번째 백버튼 클릭 (종료)
        else{
            AppFinish();
        }
    }
 
    //앱종료
    public void AppFinish(){
        finish();
        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
cs

 

* 첫번째 백버튼을 누른 시간을 변수에 넣은 후 2000초 이내에 두번째 백버튼을 클릭했는지 체크하여 2초이내에 2번 백버튼을 눌렀으면 앱을 종료합니다

+ Recent posts