Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android5.0 長按電源鍵關機沒有彈確定對話框直接關機

Android5.0 長按電源鍵關機沒有彈確定對話框直接關機

編輯:關於Android編程

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);
        }
    }


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved