Android

두번 누르면 종료

그란. 2017. 11. 20. 10:37

복붙하면 편합니다

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
public class BackPressCloseHandler {
    private long backKeyPressedTime = 0;
    private Toast toast;
 
    private Activity activity;
 
    public BackPressCloseHandler(Activity context) {
        this.activity = context;
    }
 
    public void onBackPressed() {
        if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
            backKeyPressedTime = System.currentTimeMillis();
            showGuide();
            return;
        }
        if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
            toast.cancel();
 
            activity.moveTaskToBack(true);
            activity.finish();
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
 
    public void showGuide() {
        toast = Toast.makeText(activity, "'뒤로가기' 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT);
        toast.show();
    }
 
}
 
 
cs




1
2
3
4
5
6
7
8
9
10
 private BackPressCloseHandler backPressCloseHandler;
 
        backPressCloseHandler = new BackPressCloseHandler(this);
 
 
 
@Override
    public void onBackPressed() {
        backPressCloseHandler.onBackPressed();
    }
cs