版本:2.3.1
目的:在通话设置菜单下,添加一dect设置菜单,里面再添加一checkBOxPreference
来使能硬件模块。
-------------------------
目前做的项目,需要在系统settings里面添加一选项来使能硬件模块,里面涉及到的preference知识,请网上了解,这里记录下方法。
1,settings 应用一般在 目录:\packages\apps\Settings,我们先找到通话设置的布局位置,看看它在那个包路径下,进入\packages\apps\Settings\res\xml,打开settings.xml文件:
-
- android:key="call_settings"
- settings:icon="@drawable/ic_settings_call"
- android:title="@string/call_settings_title">
-
- android:action="android.intent.action.MAIN"
- android:targetPackage="com.android.phone"
- android:targetClass="com.android.phone.CallFeaturesSetting" />
-
android:targetPackage="com.android.phone"
android:targetClass="com.android.phone.CallFeaturesSetting" targetPackage:表示包名,根据此我们可以找到通话设置的路径。
targetClass:表示此布局文件被那个类所引用,根据此类,我们可以知道在那个文件里面管理我们的通话设置功能。
2.根据包名,我们可以看到在\packages\apps\Phone 目录下,进入\res\xml目录下
找到通话布局文件:call_feature_setting.xml,根据类名,很容易找到布局文件。
里面内容如下:
- "button_misc_category_key"
- android:title="@string/other_settings"
- android:persistent="false" />
-
-
-
-
-
- android:key="dect_settings"
- android:title="@string/dect_module_title"
- android:summary="@string/dect_module_title" >
-
- android:action="android.intent.action.MAIN"
- android:targetPackage="com.android.phone"
- android:targetClass="com.android.phone.DectSettings" />
-
-
-
- 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的类名.
- "CdmaCallOptions"
- android:label="@string/cdma_options">
-
- "android.intent.action.MAIN" />
-
-
-
- "DectSettings"
- android:label="@string/dect_module_title">
-
- "android.intent.action.MAIN" />
-
-
3.修改好后,我们必须在此activity里添加preference布局文件。
在此目录Phone\res\xml下,新增dect_settings.xml
- "1.0" encoding="utf-8"?>
- ""
- android:title="@string/dect_module_title">
-
-
- android:key="button_dect_module_key"
- android:title="@string/dect_module_title"
- android:defaultValue="true"
- android:summaryOn="@string/dect_module_start"
- android:summaryOff="@string/dect_module_stop"
- />
-
-
好了,总体布局已经完成
4.在\packages\apps\Phone\src\com\android\phone目录下
新增DectSettings.java文件
加载布局文件:
//dect xml
addPreferencesFromResource(R.xml.dect_settings); 里面涉及到的MidPhoneServce服务,是自己添加的,主要通过此服务的AIDL接口跟硬件打交道。想了解系统服务,请网上查找资料。
源码如下:
- 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.os.Bundle;
- import android.os.Handler;
- 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);
-
-
- 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);
- }
- }
-
- @Override
- 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);
- }
- }
- }
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.os.Bundle; import android.os.Handler; 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); } } @Override 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.编译,烧录。