woong's

android back key & 뒤로가기 두번 종료 본문

Develop/Android

android back key & 뒤로가기 두번 종료

dlsdnd345 2016. 2. 14. 16:10

android back key & 뒤로가기 두번 종료



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    private boolean m_bFlag = false;
    private Handler m_hHandler;
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
            m_hHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    m_bFlag = false;
                }
            }
        };
       
    }
    
        @Override
    public boolean onKeyDown(int KeyCode, KeyEvent event) {
        super.onKeyDown(KeyCode, event);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (KeyCode) {
            case KeyEvent.KEYCODE_BACK: // `뒤로` 키와 같은 기능을 한다.
                if (!m_bFlag) {
                    Toast.makeText(getApplicationContext(), "'뒤로' 버튼을 한번 더 누르시면 종료됩니다", Toast.LENGTH_SHORT).show();
                    m_bFlag = true;
                    m_hHandler.sendEmptyMessageDelayed(0, 2000);
                    return false;
                }
                else {
                    finish();
                }
                return true;
            }
        }
        return false;
    }


Comments