복붙하면 편합니다
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 |
'Android' 카테고리의 다른 글
BLE 통신 개념 + 통신 과정 + Notification 설정 (9) | 2018.06.21 |
---|---|
GridLayoutManager + Spacing (0) | 2017.11.20 |
CustomTitlebar 재활용 (0) | 2017.11.20 |
슬라이더 무한 + 자동 스크롤 + Indicator 연동 (ViewPager) (0) | 2017.11.20 |
리사이클러뷰 리스트 비었을때 EmptyView 보여주기 (0) | 2017.11.11 |