onBackPressed可判断返回键是否按下
onKeyDown可判断任意键按下

public void onBackPressed() {
            Log.v("back","退出");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("exit app");
            builder.setMessage("你将退出app!");
            builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    System.exit(0);

                }
            });
        builder.show();
 }

   public boolean onKeyDown(int KeyCode,KeyEvent event){
    if(KeyCode==KeyEvent.KEYCODE_BACK &&event.getRepeatCount()==0){
        //do something...
    }

}


Chao
127 声望1 粉丝