先来一个比较简单的一种吧。直接重写onBackPressed();
@Override public void onBackPressed() { }
再来一种可以监听。多个按键的。并实现功能点击两次返回键退出public class MainActivity extends AppCompatActivity { private long mExitTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if((System.currentTimeMillis()-mExitTime) > 2000) //System.currentTimeMillis()无论何时调用,肯定大于2000 { Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show(); mExitTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); } }