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



放在程序下面即可,不需要調用,因爲是重寫


   //捕獲按下鍵盤上返回按鈕  
    @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());  
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章