android:实现退出确认框对话

自以为很正确的写法:

           AlertDialog dialog = new AlertDialog.Builder(BatteryDmrListenService.this).setTitle("警告")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setPositiveButton("确定", null)
                    .show();
            Log.e(TAG, "AlertDialog");
            dialog.setCanceledOnTouchOutside(false);
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    exit();
                }
            });

更正错误后:

 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("警告")
                                .setIcon(android.R.drawable.ic_dialog_info)
                                .setPositiveButton("确定", new OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Log.d(TAG, "onClick");
                                        MainActivity.this.finish();
                                        System.exit(0);
                                    }
                                }).show();
                        Log.e(TAG, "AlertDialog");
                        dialog.setCanceledOnTouchOutside(false);
以此提醒,多思,多想,多调试!!!

你可能感兴趣的:(android)