天天看点

android-如何在系统settings里添加设置选项

版本:2.3.1 

目的:在通话设置菜单下,添加一dect设置菜单,里面再添加一checkboxpreference 

来使能硬件模块。 

------------------------- 

目前做的项目,需要在系统settings里面添加一选项来使能硬件模块,里面涉及到的preference知识,请网上了解,这里记录下方法。 

1,settings 应用一般在 目录:\packages\apps\settings,我们先找到通话设置的布局位置,看看它在那个包路径下,进入\packages\apps\settings\res\xml,打开settings.xml文件: 

java代码  

android-如何在系统settings里添加设置选项

<com.android.settings.iconpreferencescreen  

    android:key="call_settings"  

    settings:icon="@drawable/ic_settings_call"  

    android:title="@string/call_settings_title">  

    <intent  

        android:action="android.intent.action.main"  

        android:targetpackage="com.android.phone"  

        android:targetclass="com.android.phone.callfeaturessetting" />  

</com.android.settings.iconpreferencescreen>  

android:targetpackage="com.android.phone"  

android:targetclass="com.android.phone.callfeaturessetting" 

targetpackage:表示包名,根据此我们可以找到通话设置的路径。 

targetclass:表示此布局文件被那个类所引用,根据此类,我们可以知道在那个文件里面管理我们的通话设置功能。 

2.根据包名,我们可以看到在\packages\apps\phone 目录下,进入\res\xml目录下 

找到通话布局文件:call_feature_setting.xml,根据类名,很容易找到布局文件。 

里面内容如下: 

android-如何在系统settings里添加设置选项

<preferencecategory android:key="button_misc_category_key"  

        android:title="@string/other_settings"  

        android:persistent="false" />  

<!-- dect settings -->  

    <preferencescreen  

        android:key="dect_settings"  

        android:title="@string/dect_module_title"  

        android:summary="@string/dect_module_title" >  

        <intent  

            android:action="android.intent.action.main"  

            android:targetpackage="com.android.phone"  

            android:targetclass="com.android.phone.dectsettings" />              

    </preferencescreen>  

    <checkboxpreference  

        android:key="button_auto_retry_key"  

        android:title="@string/auto_retry_mode_title"  

        android:persistent="false"  

        android:summary="@string/auto_retry_mode_summary"/>  

dect setting 就是新添加进入的设置菜单,我们的原则尽量不大量修改源码,所以添加一个preferencescreen,新增一个类文件来管理dect菜单选项。 

android:targetpackage="com.android.phone" 

android:targetclass="com.android.phone.dectsettings" 

我们指明了包名,类名后,因这是个activity,所以我们需要到phone目录下修改 

androidmanifest.xml文件,指明启动的activity的类名. 

android-如何在系统settings里添加设置选项

      <activity android:name="cdmacalloptions"  

          android:label="@string/cdma_options">  

          <intent-filter>  

              <action android:name="android.intent.action.main" />  

          </intent-filter>  

      </activity>  

<!-- dect activity -->  

      <activity android:name="dectsettings"  

          android:label="@string/dect_module_title">  

3.修改好后,我们必须在此activity里添加preference布局文件。 

  在此目录phone\res\xml下,新增dect_settings.xml 

android-如何在系统settings里添加设置选项

<?xml version="1.0" encoding="utf-8"?>  

<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"  

        android:title="@string/dect_module_title">  

      <checkboxpreference  

        android:key="button_dect_module_key"  

        android:defaultvalue="true"  

        android:summaryon="@string/dect_module_start"  

        android:summaryoff="@string/dect_module_stop"  

        />    

</preferencescreen>  

好了,总体布局已经完成 

4.在\packages\apps\phone\src\com\android\phone目录下 

新增dectsettings.java文件 

加载布局文件:       

//dect xml 

addpreferencesfromresource(r.xml.dect_settings); 

里面涉及到的midphoneservce服务,是自己添加的,主要通过此服务的aidl接口跟硬件打交道。想了解系统服务,请网上查找资料。 

源码如下: 

android-如何在系统settings里添加设置选项

package com.android.phone;  

import android.content.dialoginterface;  

import android.os.asyncresult;  

import android.os.bundle;  

import android.os.handler;  

import android.os.message;  

import android.preference.checkboxpreference;  

import android.preference.preference;  

import android.preference.preferenceactivity;  

import android.preference.preferencescreen;  

import android.content.sharedpreferences;  

import android.content.sharedpreferences.editor;  

import android.content.pm.activityinfo;  

import android.content.pm.packagemanager;  

import android.content.pm.resolveinfo;  

import android.util.log;  

import android.content.context;  

import com.android.phone.r;  

import android.os.imidphoneservice;  

import android.os.remoteexception;  

import android.os.servicemanager;  

import android.provider.settings;  

public class dectsettings extends preferenceactivity {  

    private static final string tag = "dectsettings";  

    private static final string button_dect_key  = "button_dect_module_key";  

    private checkboxpreference mbuttondect;  

    public imidphoneservice midphoneservice = null;  

    @override  

    protected void oncreate(bundle icicle) {  

        super.oncreate(icicle);  

            //dect xml  

        addpreferencesfromresource(r.xml.dect_settings);  

        mbuttondect = (checkboxpreference)findpreference(button_dect_key);  

        mbuttondect.setpersistent(false);  

        if(mbuttondect != null) {  

            int dect_state = settings.system.getint(  

                getcontentresolver(),settings.system.dect_saved_state, 1);  

            mbuttondect.setchecked( dect_state!= 0);  

            settings.system.putint(getcontentresolver(),  

                        settings.system.dect_saved_state,dect_state);  

            log.e(tag,"settings:------------->" + dect_state);  

        }  

    }  

    public boolean onpreferencetreeclick(preferencescreen preferencescreen, preference preference) {  

        if (preference == mbuttondect ) {  

            int dect = mbuttondect.ischecked() ? 1 : 0;       

            boolean state;  

            if(dect == 1)  

                state = true;  

            else  

                state = false;  

            try{  

                    midphoneservice = imidphoneservice.stub.asinterface(servicemanager.getservice("midphone"));  

                    settings.system.putint(getcontentresolver(),  

                        settings.system.dect_saved_state,dect);  

                    midphoneservice.setdectenabled(state);  

                    log.e(tag,"settings:------------->" + dect);  

                } catch (remoteexception e) {  

                    e.printstacktrace();  

                }  

            return true;  

        return false;  

  @override  

    protected void onresume() {  

        super.onresume();  

        if (mbuttondect != null) {  

            mbuttondect.setchecked(settings.system.getint(  

                getcontentresolver(),  

                settings.system.dect_saved_state, 1) != 0);  

}  

5.编译,烧录。 

# . build/envsetup.sh 

执行 # mmm packages/apps/phone/ 

会在\out\target\product\generic\system\app 生成 phone.apk文件 

拷贝 此apk到 \out\target\product\smdkv210\system\app 目录下 

编译就行: ./build_android 

此时,才能看到修改的效果!

继续阅读