android4.3 Setting里面添加永不休眠

设置 > 显示 > 休眠

a. res/values/arrays.xml
    <string-array name="screen_timeout_entries">

        <item>15 seconds</item>

        <item>30 seconds</item>

        <item>1 minute</item>

        <item>2 minutes</item>


        <item>5 minutes</item>

        <item>10 minutes</item>

        <item>30 minutes</item>

        <item>Never </item>

    </string-array>


    <!-- Do not translate. -->

    <string-array name="screen_timeout_values" translatable="false">

        <!-- Do not translate. -->

        <item>15000</item>

        <!-- Do not translate. -->


        <item>30000</item>

        <!-- Do not translate. -->

        <item>60000</item>

        <!-- Do not translate. -->

        <item>120000</item>

        <!-- Do not translate. -->

        <item>300000</item>

        <!-- Do not translate. -->

        <item>600000</item>

        <!-- Do not translate. -->

        <item>1800000</item>


        <item>-1</item>


    </string-array>








b. 在其它有定义<string-array name="screen_timeout_entries">
的地方添加一行,用于显示不同语言文字的内容,这是为了解决在不同语言时,如果没有定义,会出现异常退出的情况。大概有50个arrays.xml文件需要修改。
如:res/values-zh-rCN/arrays.xml中(此路径中绿色表示国家语言)
  <string-array name="screen_timeout_entries">


    <item msgid="3342301044271143016">"15 秒"</item>


    <item msgid="8881760709354815449">"30 秒"</item>


    <item msgid="7589406073232279088">"1 分钟"</item>


    <item msgid="7001195990902244174">"2 分钟"</item>


    <item msgid="7489864775127957179">"5 分钟"</item>


    <item msgid="2314124409517439288">"10 分钟"</item>


    <item msgid="6864027152847611413">"30 分钟"</item>


    <item msgid="6864027952847611413">"从不"</item>


  </string-array>
c.在作出以上修改后,主界面的显示会不正常,需要在代码中加以下处理:
找到如下方法:/android/packages/apps/Settings/src/com/android/settings/ DisplaySettings.java
private void updateTimeoutPreferenceDescription(long currentTimeout) {
        ListPreference preference = mScreenTimeoutPreference;
        String summary;
        final CharSequence[] entries = preference.getEntries();//modify by hcm
        final CharSequence[] values = preference.getEntryValues(); //modify by hcm
        if (currentTimeout < 0) {
            // Unsupported value
            //summary = "";
        	summary = entries[7].toString();//modify by hcm
        } else {
            if (entries == null || entries.length == 0) {
                summary = "";
            } else {
                int best = 0;
                for (int i = 0; i < values.length; i++) {
                    long timeout = Long.parseLong(values[i].toString());
                    if (currentTimeout >= timeout&&timeout != -1) {//modify by hcm
                        best = i;
                    }
                }
                summary = preference.getContext().getString(R.string.screen_timeout_summary,
                        entries[best]);
            }
        }
        preference.setSummary(summary);
    }
//注意与原来代码的区别:
//1) 修改 0 改为-1, 为保证,在界面中,显示的文字不为空。
//2)将 >=改为 ==是作一个准确判断,找到所设定的值便返回,退出循环;
//3)不加此句内容,在界面上会显示: 无操作 从不 后, 加了以后则显示: 从不

//在4.2及以上稍微有些不同, 主要是修改了对时间值的判断。
/services/java/com/android/server/power/PowerManagerService.java
    private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
    private static final int MINIMUM_SCREEN_OFF_TIMEOUT = 10 * 1000;
    private int getScreenOffTimeoutLocked() {
        int timeout = mScreenOffTimeoutSetting;
        if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
            timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
        }
        if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
            timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
        }
        return Math.max(timeout, MINIMUM_SCREEN_OFF_TIMEOUT);
    }




 /**
     * Updates the value of mUserActivitySummary to summarize the user requested
     * state of the system such as whether the screen should be bright or dim.
     * Note that user activity is ignored when the system is asleep.
     *
     * This function must have no other side-effects.
     */
    private void updateUserActivitySummaryLocked(long now, int dirty) {
        // Update the status of the user activity timeout timer.
        if ((dirty & (DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {
            mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);


            long nextTimeout = 0;
            if (mWakefulness != WAKEFULNESS_ASLEEP) {
                final int screenOffTimeout = getScreenOffTimeoutLocked();
                final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);


                mUserActivitySummary = 0;
                if (mLastUserActivityTime >= mLastWakeTime) {
                    nextTimeout = mLastUserActivityTime
                            + screenOffTimeout - screenDimDuration;
                    if (now < nextTimeout) {
                        mUserActivitySummary |= USER_ACTIVITY_SCREEN_BRIGHT;
                    } else {
                        nextTimeout = mLastUserActivityTime + screenOffTimeout;
                        if (now < nextTimeout) {
                            mUserActivitySummary |= USER_ACTIVITY_SCREEN_DIM;
                        }
                    }
                }
                if (mUserActivitySummary == 0
                        && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {
                    nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;
                    if (now < nextTimeout
                            && mDisplayPowerRequest.screenState
                                    != DisplayPowerRequest.SCREEN_STATE_OFF) {
                        mUserActivitySummary = mDisplayPowerRequest.screenState
                                == DisplayPowerRequest.SCREEN_STATE_BRIGHT ?
                                USER_ACTIVITY_SCREEN_BRIGHT : USER_ACTIVITY_SCREEN_DIM;
                    }
                }
                if (mUserActivitySummary != 0&&screenOffTimeout != MINIMUM_SCREEN_OFF_TIMEOUT) {//modify by hcm 20150916 超时待机广播
                    Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
                    msg.setAsynchronous(true);
                    mHandler.sendMessageAtTime(msg, nextTimeout);
                }
            } else {
                mUserActivitySummary = 0;
            }


            if (DEBUG_SPEW) {
                Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
                        + wakefulnessToString(mWakefulness)
                        + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
                        + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
            }
        }
    }

你可能感兴趣的:(android4.3 Setting里面添加永不休眠)