Android退出activity提示再按一次返回鍵:
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)//主要是對這個函數的復寫
- {
- // TODO Auto-generated method stub
-
- if((keyCode == KeyEvent.KEYCODE_BACK)&&(event.getAction() == KeyEvent.ACTION_DOWN))
- {
- if(System.currentTimeMillis() - temptime >2000) // 2s內再次選擇back鍵有效
- {
- System.out.println(Toast.LENGTH_LONG);
- Toast.makeText(this, "請在按一次返回退出", Toast.LENGTH_LONG).show();
- temptime = System.currentTimeMillis();
- }
- else {
- finish();
- System.exit(0); //凡是非零都表示異常退出!0表示正常退出!
- }
-
- return true;
-
- }
- return super.onKeyDown(keyCode, event);
- }