Android Add press (Power key) to end the call (hang up the phone) _android

Source: Internet
Author: User
First of all, we found that most of the Android smartphones we use now have the power button to hang up when you're on the phone, usually in the setup.
I mainly in the original source code to add this function, mainly for learning .... First look at a picture:

See that press the power button to hang up the phone, that is what I added, originally source code is not this column ...

probably thinking
First I add this checkboxpreference, and then I save the value of this feature (0 and 1) to Data/data/com.android.providers.settings
In the system table of the/DATABASES/SETTINGS.DB database
, and then processed in Phonewindownmanager.java according to the values in the database table.

Specific Process
First find setting source code, in the source we want to find the call settings, in the seting.xml we can find
Copy Code code 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 what we see in the setting (Setup), but we can't find the layout file about call_settings in the settings source, so we need to find it, In fact, this layout file is in the Package/app/phone, that is, in the phone this app source resource file.

So we can find the Call_feature_setting.xml file under the phone's resource file as follows:
Copy Code code 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 to all Com.android.phone.EditPhoneNumberPreference objects
The last several attributes are to 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>

So we can add a checkboxpreference to the front.
Copy Code code 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>

become
Copy Code code 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>

There's a definition here.
android:title= "@string/press_power_end_call"
So we're going to add the relevant information to the String.xml file of the resource:
Add in Package/app/phone/res/values/string.xml:
<string name= "Press_power_end_call" >press_power_end_call</string>
Add in Package/app/phone/res/values-zh-rcn/string.xml:
<string name= "Press_power_end_call" msgid= "4676390750360727396" > Press the power button to hang up the phone </string>
Here, even if you add something to the UI, the next step is the code:
Find the Callfeaturesetting.java file under Package/app/phone/src/com/android/phone,
In the public boolean onpreferencechange (preference preference, Object ObjValue) method, add an event that if you chose to hang the phone by power key:
Copy Code code as follows:

<span style= "font-size:14px" >//add by Xxnan
else if (preference = = Press_power_end_call) {
If checked, save 1 to Press_power_end_call in the system table
Settings.System.putInt (Getcontentresolver (),
"Press_power_end_call",
Press_power_end_call.ischecked ()? 1:0);
End by Xxnan </SPAN>

after oncreate add the following code
Copy Code code as follows:

protected void OnCreate (Bundle icicle) {
Super.oncreate (Icicle);
if (DBG) log ("Creating activity");
Mphone = Phonefactory.getdefaultphone ();
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);
The value of the 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 resulting value is 1, the next time you open setting, the option box should be checked
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);
。。。。。。。
。。。。。。。

So it's almost done. To get whether or not to open this function to store and remove to the system database, next is to Framework/base/policy/src/com/android
/internal/policy/impl under the
Phonewindowmanager.java, we've been analyzing the Phonewindowmanager.java.
public int interceptkeybeforequeueing (long whennanos, int action, int flags, int keycode, int scancode, int policyflags,
Boolean Isscreenon) method to accept events by the Power key, in which we only need to add a few code:
The original code is
Copy Code code 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 should silence the ringer.
Telephonyservice.silenceringer ();
Or else if (mincallpowerbehavior
& Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP)!= 0
&& Telephonyservice.isoffhook ()) {
Otherwise, if "power button ends called" is enabled,
The power button would 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 Code code 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); Remove the value from the database to open this feature
LOG.I ("End_call_key", "end_call_key=" +end_call_key);
Itelephony Telephonyservice = Gettelephonyservice ();
Boolean hungup = false;
if (Telephonyservice!= null) {
try {
If the phone is playing and this function is turned on, hang up the phone when you press the Power key
if (telephonyservice.isringing () &&end_call_key==1) {
Pressing power while there ' s a ringing incoming
Call should silence the ringer.
Telephonyservice.silenceringer ();
Hungup=telephonyservice.endcall ();
Or else if (mincallpowerbehavior
& Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP)!= 0
&& Telephonyservice.isoffhook ()) {
Otherwise, if "power button ends called" is enabled,
The power button would hang up any current active call.
Hungup = Telephonyservice.endcall ();
}
catch (RemoteException ex) {
LOG.W (TAG, "Itelephony threw RemoteException", ex);
}
}
Interceptpowerkeydown (!isscreenon | | hungup);
。。。。。。。。。。。
。。。。。。。。。。。

Because I this development board is not able to plug in the phone card will not be able to experiment successfully, but the principle should be so!
The last modified place has to recompile, then we want to compile under the source code under the app's phone and the framework under the policy
The last generated out/... /system/app/phone.apk and out/.... /system/framework/android.policy.jar have to be replaced.
The same file on the phone (the ADB shell enters your cell phone and has root privileges) should be available.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.