按返回键,提示是否退出程序



放在程序下面即可,不需要调用,因为是重写


   //捕获按下键盘上返回按钮  
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if (keyCode == KeyEvent.KEYCODE_BACK) {  
            new AlertDialog.Builder(this)  
                    // .setIcon(R.drawable.services)  
                    .setTitle("警告")  
                    .setMessage("您是否要退出程序吗?")  
                    .setNegativeButton("取消",  
                            new DialogInterface.OnClickListener() {  
 
                                public void onClick(DialogInterface dialog,  
                                        int which) {  
                                    // TODO Auto-generated method stub  
                                      
                                }  
//                                @Override  
//                                public void onClick(DialogInterface dialog,  
//                                        int which) {  
//                                }  
                            })  
                    .setPositiveButton("确定",  
                            new DialogInterface.OnClickListener() {  
                                public void onClick(DialogInterface dialog,  
                                        int whichButton) {  
                                    finish();  
                                }  
                            }).show();  
            return true;  
        } else {  
            return super.onKeyDown(keyCode, event);  
        }  
    }  
    //彻底退出程序  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        System.exit(0);  
        // 或者下面这种方式  
        // android.os.Process.killProcess(android.os.Process.myPid());  
    }



你可能感兴趣的:(dialog)