关闭

android开发之增加永不暗屏菜单

标签: android开发settings功能添加
303人阅读 评论(0) 收藏 举报
分类:

     在ODM开发过程中,不可避免的需要根据客户的需求所有调整手机的菜单功能等,今天为大家介绍我在实际开发中遇到的问题,客户要求在手机设置-显示-休眠中增加永不暗屏的菜单。具体的整理文件如下:

       首先此功能菜单在设置模块,因此它修改的文件地址为:

alps/packages/apps/Settings   

修改的文件主要有String.xml 、arrays.xml、以及DisplaySettings.java文件,

下面来看具体的改动处:

1.alps/packages/apps/Settings/res/values/arrays.xml

此文件中增加一个item,同时设置其Value值

    <!-- Display settings.  The delay in inactivity before the screen is turned off. These are shown in a list dialog. -->
    <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 timeout</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>
        <!-- Do not translate. -->
        <item>-1</item>
    </string-array>


2.alps/packages/apps/Settings/res/values/strings.xml

此文件中增加一个新的字串

<string name="screen_never_timeout">Never timeout</string>

注意:若手机兼容多国言语,则需在不同的value文件中重复上述动作,我的项目语言主要为英语、中文简体、中文繁体,因此任须修改如下路径中的arrays.xml,string.xml文件

alps/packages/apps/Settings/res/values-zh-rCN

alps/packages/apps/Settings/res/values-zh-rTW

同样在arrays.xml中增加相应的item,String.xml中增加相应的显示字串


3.最重要的一步修改,代码逻辑的修改:

alps/packages/apps/Settings/src/com/android/settings/DisplaySettings.java

一、在onPreferenceChange()方法中定义

        if (KEY_SCREEN_TIMEOUT.equals(key)) {
            try {
                int value = Integer.parseInt((String) objValue);
                if(value==-1){
                    value=Integer.MAX_VALUE;
                }
                Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
                updateTimeoutPreferenceDescription(value);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }

二、在onCreate()方法给休眠设置时做修改

       final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
                FALLBACK_SCREEN_TIMEOUT_VALUE);
        if(Integer.MAX_VALUE==currentTimeout){
            mScreenTimeoutPreference.setValue(String.valueOf(-1));
        }else{
            mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
        }
        mScreenTimeoutPreference.setOnPreferenceChangeListener(this);

三、在updateTimeoutPreferenceDescription()方法中修改,主要设置summary的显示

 

    private void updateTimeoutPreferenceDescription(long currentTimeout) {
        ListPreference preference = mScreenTimeoutPreference;
        String summary;
        if (currentTimeout < 0||Integer.MAX_VALUE==currentTimeout) {
            // Unsupported value
            summary = preference.getContext().getString(R.string.screen_never_timeout);
        } else {
            final CharSequence[] entries = preference.getEntries();
            final CharSequence[] values = preference.getEntryValues();
            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>0) {
                        best = i;
                    }
                }
                summary = preference.getContext().getString(R.string.screen_timeout_summary,
                        entries[best]);
            }
        }
        preference.setSummary(summary);
    }

四、在updateState()方法中修改

        // update screen timeout
        if (mScreenTimeoutPreference != null) {
            final long screenTimeout = Settings.System.getLong(getContentResolver(), SCREEN_OFF_TIMEOUT,
                    FALLBACK_SCREEN_TIMEOUT_VALUE);
            if(Integer.MAX_VALUE==screenTimeout){
                mScreenTimeoutPreference.setValue(String.valueOf(-1));
            }else{
                mScreenTimeoutPreference.setValue(String.valueOf(screenTimeout));
            }
            updateTimeoutPreferenceDescription(screenTimeout);
        }

之后单遍push之后可做验证,好了本期就我遇到问题分享到这里,谢谢!


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7827次
    • 积分:251
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类