版本:2.3.1
目的:在通話設置菜單下,添加一dect設置菜單,裏面再添加一checkBOxPreference
來使能硬件模塊。
-------------------------
目前作的項目,須要在系統settings裏面添加一選項來使能硬件模塊,裏面涉及到的preference知識,請網上了解,這裏記錄下方法。
1,settings 應用通常在 目錄:\packages\apps\Settings,咱們先找到通話設置的佈局位置,看看它在那個包路徑下,進入\packages\apps\Settings\res\xml,打開settings.xml文件:
- <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>
<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,根據類名,很容易找到佈局文件。
裏面內容以下:
- <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"/>
<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的類名.
- <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">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- </intent-filter>
- </activity>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
3.修改好後,咱們必須在此activity裏添加preference佈局文件。
在此目錄Phone\res\xml下,新增dect_settings.xml
- <?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:title="@string/dect_module_title"
- android:defaultValue="true"
- android:summaryOn="@string/dect_module_start"
- android:summaryOff="@string/dect_module_stop"
- />
-
- </PreferenceScreen>
<?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:title="@string/dect_module_title"
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接口跟硬件打交道。想了解系統服務,請網上查找資料。
源碼以下:
- 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);
- }
- }
- }
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.編譯,燒錄。 # . build/envsetup.sh 執行 # mmm packages/apps/Phone/ 會在\out\target\product\generic\system\app 生成 Phone.apk文件 拷貝 此apk到 \out\target\product\smdkv210\system\app 目錄下 編譯就行: ./build_android 此時,才能看到修改的效果!