First, we found that most of the android smartphones we use now are hanging up when you call the phone by pressing the power key, which is generally being set.
I mainly add this function to the native source code for learning .... First look at a picture:
You can see the one that hangs up by pressing the power key, that is, the one I added, which is not included in the native source code .....
General ideas:
First, I add this checkboxPreference, and then save the value (0 and 1) of this function to data/com. android. providers. settings.
/Databases/settings. db database system table
And then process the data according to the value in the database table in phonedomainwnmanager. java.
Specific Process:
First, find the setting source code. In the source code, we need to find the call settings, which can be found in seting. xml.Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 14px"> <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> </SPAN>
This call_settings is the call settings we see in setting (settings), but we cannot find the layout file about call_settings in the source code in settings, so we need to find it, in fact, the layout file is in package/app/Phone, that is, in the resource file of the app source code of Phone.
Therefore, we can find the Call_feature_setting.xml file in the Phone resource file as follows:Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 14px"> <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: phone = "http://schemas.android.com/apk/res/com.android.phone"
Android: title = "@ string/call_settings">
<PreferenceScreen
Android: key = "button_fdn_key"
Android: title = "@ string/fdn"
Android: summary = "@ string/sum_fdn"
Android: persistent = "false">
<Intent android: action = "android. intent. action. MAIN"
Android: targetPackage = "com. android. phone"
Android: targetClass = "com. android. phone. FdnSetting"/>
</PreferenceScreen>
<PreferenceCategory
Android: key = "button_voicemail_category_key"
Android: title = "@ string/voicemail"
Android: persistent = "false">
<ListPreference
Android: key = "button_voicemail_provider_key"
Android: title = "@ string/voicemail_provider"
Android: summary = "@ string/sum_voicemail_choose_provider"
Android: defaultValue = ""
Android: persistent = "true"
/>
<PreferenceScreen android: key = "button_voicemail_setting_key"
Android: title = "@ string/voicemail_settings"
Android: persistent = "false">
<! -- Note for all com. android. phone. EditPhoneNumberPreference objects
The last several attributes are for use with the EditText field
In the dialog. These attributes are forwarded to that field
When the edittext is created. The attributes include:
1. android: singleLine
2. android: autoText
3. android: background -->
<Com. android. phone. EditPhoneNumberPreference
Android: key = "button_voicemail_key"
Android: title = "@ string/voicemail_settings_number_label"
Android: persistent = "false"
Android: dialogTitle = "@ string/voicemail"
Phone: confirmMode = "confirm"
Android: singleLine = "true"
Android: autoText = "false"/>
</PreferenceScreen>
</PreferenceCategory>
..................
.................
</SPAN>
Therefore, we can add a checkboxPreference at the beginning.Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 14px"> <CheckBoxPreference
Android: key = "press_power_end_call_key"
Android: title = "@ string/press_power_end_call"
Android: persistent = "false"/> </SPAN>
Change:Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 14px"> <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: phone = "http://schemas.android.com/apk/res/com.android.phone"
Android: title = "@ string/call_settings">
<CheckBoxPreference
Android: key = "press_power_end_call_key"
Android: title = "@ string/press_power_end_call"
Android: persistent = "false"/>
<PreferenceScreen
Android: key = "button_fdn_key"
Android: title = "@ string/fdn"
Android: summary = "@ string/sum_fdn"
Android: persistent = "false">
<Intent android: action = "android. intent. action. MAIN"
Android: targetPackage = "com. android. phone"
Android: targetClass = "com. android. phone. FdnSetting"/>
</PreferenceScreen>
.......
.......
....... </SPAN>
Here we have our own definition:
Android: title = "@ string/press_power_end_call"
Therefore, we need to add relevant information to the string. xml file of the Resource:
Add the following content to package/app/Phone/res/values/string. xml:
<String name = "press_power_end_call"> press_power_end_call </string>
Add the following content to package/app/Phone/res/values-zh-rCN/string. xml:
<String name = "press_power_end_call" msgid = "4676390750360727396"> press the power key to stop the call. </string>
Now, even if something on the UI has been added, the Code is as follows:
Find the CallFeatureSetting. java file in package/app/Phone/src/com/android/phone,
In the public boolean onPreferenceChange (Preference preference, Object objValue) method, add an event if you select to press the power key to hold the phone:Copy codeThe Code is as follows: <SPAN style = "FONT-SIZE: 14px"> // add by xxnan
Else if (preference = press_power_end_call ){
// If this option is selected, 1 is saved to the press_power_end_call of the system table.
Settings. System. putInt (getContentResolver (),
"Press_power_end_call ",
Press_power_end_call.isChecked ()? 1: 0 );
// End by xxnan </SPAN>
After adding the following code in OnCreate:Copy codeThe Code is as follows: protected void onCreate (Bundle icicle ){
Super. onCreate (icicle );
If (DBG) log ("Creating activity ");
MPhone = PhoneFactory. getdefaphone phone ();
AddPreferencesFromResource (R. xml. call_feature_setting );
// Add by xxnan
ContentResolver resolver = getContentResolver ();
Press_power_end_call = (CheckBoxPreference) findPreference (press_power_end_call_key );
Press_power_end_call.setOnPreferenceChangeListener (this );
// Obtain the value of press_power_end_call in the database system table, that is, whether checkboxpreference is selected
Int press_power_end_call_key = Settings. System. getInt (getContentResolver (),
"Press_power_end_call", 0 );
// If the value is 1, the option box should be checked if setting is enabled next time.
If (press_power_end_call_key = 1)
Press_power_end_call.setChecked (true );
// End by xxnan
MAudioManager = (AudioManager) getSystemService (Context. AUDIO_SERVICE );
// Get buttons
PreferenceScreen prefSet = getPreferenceScreen ();
MSubMenuVoicemailSettings = (EditPhoneNumberPreference) findPreference (BUTTON_VOICEMAIL_KEY );
.......
.......
In this way, even if the function is almost complete to obtain whether to enable this function for storage and retrieval to the system database, the next step is to the framework/base/policy/src/com/android
/Internal/policy/impl
PhoneWindowManager. java is processed. We have analyzed it in PhoneWindowManager. java.
Public int interceptKeyBeforeQueueing (long whenNanos, int action, int flags, int keyCode, int scanCode, int policyFlags,
Boolean isScreenOn) method to accept press power events, in this method we only need to add a few code:
The original code is:Copy codeThe Code is as follows: case KeyEvent. KEYCODE_POWER :{
Result & = ~ ACTION_PASS_TO_USER;
If (down ){
Log. I ("xxnan", "xxnan" + "xiaxiangnan ");
ITelephony telephonyService = getTelephonyService ();
Boolean hungUp = false;
If (telephonyService! = Null ){
Try {
If (telephonyService. isRinging ()){
// Pressing Power while there's a ringing incoming
// Call shoshould silence the ringer.
TelephonyService. silenceRinger ();
} Else if (mIncallPowerBehavior
& Settings. Secure. INCALL_POWER_BUTTON_BEHAVIOR_HANGUP )! = 0
& TelephonyService. isOffhook ()){
// Otherwise, if "Power button ends call" is enabled,
// The Power button will hang up any current active call.
HungUp = telephonyService. endCall ();
}
} Catch (RemoteException ex ){
Log. w (TAG, "ITelephony threw RemoteException", ex );
}
}
InterceptPowerKeyDown (! IsScreenOn | hungUp );
............
............
After modification:Copy codeThe Code is as follows: case KeyEvent. KEYCODE_POWER :{
Result & = ~ ACTION_PASS_TO_USER;
If (down ){
Log. I ("xxnan", "xxnan" + "xiaxiangnan ");
Int end_call_key = Settings. System. getInt (mContext. getContentResolver (),
"Press_power_end_call", 0); // retrieves the value of whether the function is enabled in the database.
Log. I ("end_call_key", "end_call_key =" + end_call_key );
ITelephony telephonyService = getTelephonyService ();
Boolean hungUp = false;
If (telephonyService! = Null ){
Try {
// If the call is in progress and this function is enabled, the call will be suspended when you press the power key.
If (telephonyService. isRinging () & end_call_key = 1 ){
// Pressing Power while there's a ringing incoming
// Call shoshould silence the ringer.
// TelephonyService. silenceRinger ();
HungUp = telephonyService. endCall ();
} Else if (mIncallPowerBehavior
& Settings. Secure. INCALL_POWER_BUTTON_BEHAVIOR_HANGUP )! = 0
& TelephonyService. isOffhook ()){
// Otherwise, if "Power button ends call" is enabled,
// The Power button will hang up any current active call.
HungUp = telephonyService. endCall ();
}
} Catch (RemoteException ex ){
Log. w (TAG, "ITelephony threw RemoteException", ex );
}
}
InterceptPowerKeyDown (! IsScreenOn | hungUp );
...........
...........
Because I cannot plug in a phone card on this development board, the experiment fails, but the principle should be like this!
The last modified parts must be re-compiled, so we need to compile the Phone of the app and the policy of the framework in the source code.
The last generated out /... /System/app/Phone.apk and out /.... /System/framework/android. policy. jar must be replaced.
The same file in the mobile phone (adb shell enters your mobile phone and requires the root permission.