码迷,mamicode.com
首页 > 移动开发 > 详细

Android5.0 长按电源键关机没有弹确定对话框直接关机

时间:2015-04-23 17:34:57      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:android5.0关机对话框

Android5.0 长按电源键关机没有弹确定对话框直接关机,但用户点击了关机选项之后,应该弹一个确定对话框给用户提示是否真的进入关机模式。

首先在frameworks层将对话框的标志位打开。

frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java

       @Override
        public void onPress() {
            final boolean quickbootEnabled = Settings.System.getInt(
                    mContext.getContentResolver(), "enable_quickboot", 0) == 1;
            // go to quickboot mode if enabled
            if (quickbootEnabled) {
                startQuickBoot();
                return;
            }
            // shutdown by making sure radio and power are handled accordingly.
            mWindowManagerFuncs.shutdown(true /* confirm */);//20150423 show the power-off dialog
        }

源码中对于显示的字符串的处理是有逻辑的所以如果不做任何修改关机的时候弹出来的是重启的对话框。修改如下:

frameworks\base\services\core\java\com\android\server\power\ShutdownThread.java


static void shutdownInner(final Context context, boolean confirm) {
        // ensure that only one thread is trying to power down.
        // any additional calls are just returned
        synchronized (sIsStartedGuard) {
            if (sIsStarted) {
                Log.d(TAG, "Request to shutdown already running, returning.");
                return;
            }
        }

        boolean showRebootOption = false;
        String[] defaultActions = context.getResources().getStringArray(
                com.android.internal.R.array.config_globalActionsList);
        for (int i = 0; i < defaultActions.length; i++) {
            if (defaultActions[i].equals("reboot") && mReboot) {// if (defaultActions[i].equals("reboot")) {20150421 for power_off dialog
                showRebootOption = true;
                break;
            }
        }
        final int longPressBehavior = context.getResources().getInteger(
                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
        int resourceId = mRebootSafeMode
                ? com.android.internal.R.string.reboot_safemode_confirm
                : (longPressBehavior == 2
                        ? com.android.internal.R.string.shutdown_confirm_question
                        : com.android.internal.R.string.shutdown_confirm);
        if (showRebootOption && !mRebootSafeMode) {
            resourceId = com.android.internal.R.string.reboot_confirm;
        }

        Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);

        if (confirm) {
            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
            if (sConfirmDialog != null) {
                sConfirmDialog.dismiss();
            }
            sConfirmDialog = new AlertDialog.Builder(context)
                    .setTitle(mRebootSafeMode
                            ? com.android.internal.R.string.reboot_safemode_title
                            : showRebootOption
                                    ? com.android.internal.R.string.reboot_title
                                    : com.android.internal.R.string.power_off)
                    .setMessage(resourceId)
                    .setPositiveButton(com.android.internal.R.string.yes,
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                        }
                    })
                    .setNegativeButton(com.android.internal.R.string.no, null)
                    .create();
            closer.dialog = sConfirmDialog;
            sConfirmDialog.setOnDismissListener(closer);
            sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            sConfirmDialog.show();
        } else {
            beginShutdownSequence(context);
        }
    }


Android5.0 长按电源键关机没有弹确定对话框直接关机

标签:android5.0关机对话框

原文地址:http://blog.csdn.net/lisineng/article/details/45223069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!