Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071249
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-02-25 13:43:21

版本:2.3.1
目的:在通话设置菜单下,添加一dect设置菜单,里面再添加一checkBOxPreference
来使能硬件模块。
-------------------------
目前做的项目,需要在系统settings里面添加一选项来使能硬件模块,里面涉及到的preference知识,请网上了解,这里记录下方法。


1,settings 应用一般在 目录:\packages\apps\Settings,我们先找到通话设置的布局位置,看看它在那个包路径下,进入\packages\apps\Settings\res\xml,打开settings.xml文件:
Java代码 复制代码 收藏代码
  1. android:key="call_settings"
  2. settings:icon="@drawable/ic_settings_call"
  3. android:title="@string/call_settings_title">
  4. android:action="android.intent.action.MAIN"
  5. android:targetPackage="com.android.phone"
  6. 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,根据类名,很容易找到布局文件。
里面内容如下:
Java代码 复制代码 收藏代码
  1. "button_misc_category_key"
  2. android:title="@string/other_settings"
  3. android:persistent="false" />
  4. android:key="dect_settings"
  5. android:title="@string/dect_module_title"
  6. android:summary="@string/dect_module_title" >
  7. android:action="android.intent.action.MAIN"
  8. android:targetPackage="com.android.phone"
  9. android:targetClass="com.android.phone.DectSettings" />
  10. android:key="button_auto_retry_key"
  11. android:title="@string/auto_retry_mode_title"
  12. android:persistent="false"
  13. 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的类名.
Java代码 复制代码 收藏代码
  1. "CdmaCallOptions"
  2. android:label="@string/cdma_options">
  3. "android.intent.action.MAIN" />
  4. "DectSettings"
  5. android:label="@string/dect_module_title">
  6. "android.intent.action.MAIN" />

3.修改好后,我们必须在此activity里添加preference布局文件。
在此目录Phone\res\xml下,新增dect_settings.xml
Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?>
  2. ""
  3. android:title="@string/dect_module_title">
  4. android:key="button_dect_module_key"
  5. android:title="@string/dect_module_title"
  6. android:defaultValue="true"
  7. android:summaryOn="@string/dect_module_start"
  8. android:summaryOff="@string/dect_module_stop"
  9. />

好了,总体布局已经完成
4.在\packages\apps\Phone\src\com\android\phone目录下
新增DectSettings.java文件

加载布局文件:
//dect xml
addPreferencesFromResource(R.xml.dect_settings);
里面涉及到的MidPhoneServce服务,是自己添加的,主要通过此服务的AIDL接口跟硬件打交道。想了解系统服务,请网上查找资料。

源码如下:
Java代码 复制代码 收藏代码
  1. package com.android.phone;
  2. import android.content.DialogInterface;
  3. import android.os.AsyncResult;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.preference.CheckBoxPreference;
  8. import android.preference.Preference;
  9. import android.preference.PreferenceActivity;
  10. import android.preference.PreferenceScreen;
  11. import android.content.SharedPreferences;
  12. import android.content.SharedPreferences.Editor;
  13. import android.content.pm.ActivityInfo;
  14. import android.content.pm.PackageManager;
  15. import android.content.pm.ResolveInfo;
  16. import android.os.Bundle;
  17. import android.os.Handler;
  18. import android.util.Log;
  19. import android.content.Context;
  20. import com.android.phone.R;
  21. import android.os.IMidPhoneService;
  22. import android.os.RemoteException;
  23. import android.os.ServiceManager;
  24. import android.provider.Settings;
  25. public class DectSettings extends PreferenceActivity {
  26. private static final String TAG = "DectSettings";
  27. private static final String BUTTON_DECT_KEY = "button_dect_module_key";
  28. private CheckBoxPreference mButtonDect;
  29. public IMidPhoneService midphoneservice = null;
  30. @Override
  31. protected void onCreate(Bundle icicle) {
  32. super.onCreate(icicle);
  33. //dect xml
  34. addPreferencesFromResource(R.xml.dect_settings);
  35. mButtonDect = (CheckBoxPreference)findPreference(BUTTON_DECT_KEY);
  36. mButtonDect.setPersistent(false);
  37. if(mButtonDect != null) {
  38. int dect_state = Settings.System.getInt(
  39. getContentResolver(),Settings.System.DECT_SAVED_STATE, 1);
  40. mButtonDect.setChecked( dect_state!= 0);
  41. Settings.System.putInt(getContentResolver(),
  42. Settings.System.DECT_SAVED_STATE,dect_state);
  43. Log.e(TAG,"settings:------------->" dect_state);
  44. }
  45. }
  46. @Override
  47. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
  48. if (preference == mButtonDect ) {
  49. int dect = mButtonDect.isChecked() ? 1 : 0;
  50. boolean state;
  51. if(dect == 1)
  52. state = true;
  53. else
  54. state = false;
  55. try{
  56. midphoneservice = IMidPhoneService.Stub.asInterface(ServiceManager.getService("midphone"));
  57. Settings.System.putInt(getContentResolver(),
  58. Settings.System.DECT_SAVED_STATE,dect);
  59. midphoneservice.setDectEnabled(state);
  60. Log.e(TAG,"settings:------------->" dect);
  61. } catch (RemoteException e) {
  62. e.printStackTrace();
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. @Override
  69. protected void onResume() {
  70. super.onResume();
  71. if (mButtonDect != null) {
  72. mButtonDect.setChecked(Settings.System.getInt(
  73. getContentResolver(),
  74. Settings.System.DECT_SAVED_STATE, 1) != 0);
  75. }
  76. }
  77. }
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.编译,烧录。
阅读(920) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~