android 添加按电源键结束通话

简介:

首先我们发现现在我们所用的android智能手机大部分都有当你在打电话时按power键来挂断电话,一般都是在设置中。



我主要是在原生源码中添加这一功能,主要用于学习。。。。先看一张图:

  


看到那个按电源键挂断电话吧,那就是我所添加的,本来原生源码中是没有这一栏的。。。。。


大概思路:


首先我先添加这一个checkboxPreference,然后将是否选择这一功能的值(0和1)存到data/data/com.android.providers.settings


/databases/settings.db数据库的system表中


,然后再根据数据库表中的值在PhoneWindownManager.java中去处理。



具体过程:



首先找到setting的源码,在源码下我们要找到通话设置,在seting.xml中我们能找到


[java]  view plain copy
  1. <span style="font-size:14px;"> <com.android.settings.IconPreferenceScreen  
  2.             android:key="call_settings"  
  3.             settings:icon="@drawable/ic_settings_call"  
  4.             android:title="@string/call_settings_title">  
  5.             <intent  
  6.                 android:action="android.intent.action.MAIN"  
  7.                 android:targetPackage="com.android.phone"  
  8.                 android:targetClass="com.android.phone.CallFeaturesSetting" />  
  9.         </com.android.settings.IconPreferenceScreen></span>  

这个call_settings就是我们在setting(设置)中看到的通话设置,但是我们却不能在settings中的源码中找到关于call_settings的布局文件,


因此我们需要找到它,其实这个布局文件是在package/app/Phone中,也就是在Phone这个app源码的资源文件中。


因此我们在Phone的资源文件下能找到Call_feature_setting.xml文件如下:


[java]  view plain copy
  1. <span style="font-size:14px;"><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:phone="http://schemas.android.com/apk/res/com.android.phone"  
  3.         android:title="@string/call_settings">          
  4.     <PreferenceScreen  
  5.         android:key="button_fdn_key"  
  6.         android:title="@string/fdn"  
  7.         android:summary="@string/sum_fdn"  
  8.         android:persistent="false">  
  9.   
  10.         <intent android:action="android.intent.action.MAIN"  
  11.             android:targetPackage="com.android.phone"  
  12.             android:targetClass="com.android.phone.FdnSetting" />  
  13.   
  14.     </PreferenceScreen>  
  15.   
  16.     <PreferenceCategory  
  17.         android:key="button_voicemail_category_key"  
  18.         android:title="@string/voicemail"  
  19.         android:persistent="false">  
  20.       <ListPreference  
  21.           android:key="button_voicemail_provider_key"  
  22.           android:title="@string/voicemail_provider"  
  23.           android:summary="@string/sum_voicemail_choose_provider"  
  24.           android:defaultValue=""  
  25.           android:persistent="true"  
  26.       />  
  27.       <PreferenceScreen android:key="button_voicemail_setting_key"  
  28.             android:title="@string/voicemail_settings"  
  29.             android:persistent="false">  
  30.   
  31.             <!-- Note for all com.android.phone.EditPhoneNumberPreference objects  
  32.   
  33.            The last several attributes are for use with the EditText field  
  34.            in the dialog.  These attributes are forwarded to that field  
  35.            when the edittext is created.  The attributes include:  
  36.              1. android:singleLine  
  37.              2. android:autoText  
  38.              3. android:background -->  
  39.   
  40.               <com.android.phone.EditPhoneNumberPreference  
  41.                 android:key="button_voicemail_key"  
  42.                 android:title="@string/voicemail_settings_number_label"  
  43.                 android:persistent="false"  
  44.                 android:dialogTitle="@string/voicemail"  
  45.                 phone:confirmMode="confirm"  
  46.                 android:singleLine="true"  
  47.                 android:autoText="false" />  
  48.       </PreferenceScreen>  
  49.   </PreferenceCategory>  
  50. 。。。。。。。。。。。。。。。。。。  
  51. 。。。。。。。。。。。。。。。。。  
  52. </span>  

因此我们可以在最前面添加一个checkboxPreference


[java]  view plain copy
  1. <span style="font-size:14px;"><CheckBoxPreference  
  2.         android:key="press_power_end_call_key"  
  3.         android:title="@string/press_power_end_call"   
  4.         android:persistent="false"/></span>  

变成:


[java]  view plain copy
  1. <span style="font-size:14px;"><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:phone="http://schemas.android.com/apk/res/com.android.phone"  
  3.         android:title="@string/call_settings">  
  4.      <CheckBoxPreference  
  5.         android:key="press_power_end_call_key"  
  6.         android:title="@string/press_power_end_call"   
  7.         android:persistent="false"/>  
  8.           
  9.     <PreferenceScreen  
  10.         android:key="button_fdn_key"  
  11.         android:title="@string/fdn"  
  12.         android:summary="@string/sum_fdn"  
  13.         android:persistent="false">  
  14.   
  15.         <intent android:action="android.intent.action.MAIN"  
  16.             android:targetPackage="com.android.phone"  
  17.             android:targetClass="com.android.phone.FdnSetting" />  
  18.   
  19.     </PreferenceScreen>  
  20. 。。。。。。。  
  21. 。。。。。。。  
  22. 。。。。。。。</span>  

在这里有自己定义的:


android:title="@string/press_power_end_call" 


所以我们要在资源的string.xml文件中添加相关的信息:


package/app/Phone/res/values/string.xml中添加:


<string name="press_power_end_call">press_power_end_call</string>


package/app/Phone/res/values-zh-rCN/string.xml中添加:


<string name="press_power_end_call" msgid="4676390750360727396">按电源键挂断电话</string>


到这里就算添加好了UI上的东西,接下来就是代码了:


package/app/Phone/src/com/android/phone下找到CallFeatureSetting.java文件,


在 public boolean onPreferenceChange(Preference preference, Object objValue) 方法中要增加一个如果选择了按power键挂电话


的事件:



[java]  view plain copy
  1. <span style="font-size:14px;">//add by xxnan  
  2.         else if (preference == press_power_end_call) {  
  3.               //如果勾选就将1存到system表的press_power_end_call中  
  4.               Settings.System.putInt(getContentResolver(),  
  5.                     "press_power_end_call",  
  6.                     press_power_end_call.isChecked() ? 1 : 0);  
  7.          //end by xxnan       </span>  

在OnCreate添加如下代码之后:



[java]  view plain copy
  1. protected void onCreate(Bundle icicle) {  
  2.         super.onCreate(icicle);  
  3.         if (DBG) log("Creating activity");          
  4.         mPhone = PhoneFactory.getDefaultPhone();  
  5.   
  6.         addPreferencesFromResource(R.xml.call_feature_setting);  
  7.          //add by xxnan  
  8.         ContentResolver resolver = getContentResolver();  
  9.         press_power_end_call= (CheckBoxPreference)findPreference(press_power_end_call_key);  
  10.      press_power_end_call.setOnPreferenceChangeListener(this);  
  11. // 获的数据库system表里press_power_end_call的值,也就是是否选择了checkboxpreference  
  12.      int    press_power_end_call_key=Settings.System.getInt(getContentResolver(),  
  13.                     "press_power_end_call",0);  
  14. //如果得到的值是1,则下次打开setting的话,选项框要勾选  
  15.      if(press_power_end_call_key==1)  
  16.         press_power_end_call.setChecked(true);  
  17.      //end by xxnan   
  18.         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  19.   
  20.         // get buttons  
  21.         PreferenceScreen prefSet = getPreferenceScreen();  
  22.         mSubMenuVoicemailSettings = (EditPhoneNumberPreference)findPreference(BUTTON_VOICEMAIL_KEY);  
  23. 。。。。。。。  
  24. 。。。。。。。  


这样就算差不多完成了到获取是否开启这一功能存放和取出到系统数据库中,接下来就是到framework/base/policy/src/com/android


/internal/policy/impl下的

PhoneWindowManager.java中去处理了,之前我们就有分析到PhoneWindowManager.java中的

public int interceptKeyBeforeQueueing(long whenNanos, int action, int flags, int keyCode, int scanCode, int policyFlags,


boolean isScreenOn)方法来接受按power键的事件,在这个方法里我们只需要添加很少代码:

原来代码是:

[java]  view plain copy
  1. case KeyEvent.KEYCODE_POWER: {  
  2.                 result &= ~ACTION_PASS_TO_USER;           
  3.                 if (down) {  
  4.                Log.i("xxnan","xxnan"+"xiaxiangnan");  
  5.                     ITelephony telephonyService = getTelephonyService();      
  6.              boolean hungUp = false;                      
  7.                     if (telephonyService != null) {  
  8.                         try {  
  9.                             if (telephonyService.isRinging()) {  
  10.                                 // Pressing Power while there's a ringing incoming  
  11.                                 // call should silence the ringer.  
  12.                                 telephonyService.silenceRinger();   
  13.                             } else if ((mIncallPowerBehavior  
  14.                                     & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0  
  15.                                     && telephonyService.isOffhook()) {  
  16.                                 // Otherwise, if "Power button ends call" is enabled,  
  17.                                 // the Power button will hang up any current active call.  
  18.                                 hungUp = telephonyService.endCall();  
  19.                             }  
  20.                            
  21.                         } catch (RemoteException ex) {  
  22.                             Log.w(TAG, "ITelephony threw RemoteException", ex);  
  23.                         }  
  24.                     }  
  25.                     interceptPowerKeyDown(!isScreenOn || hungUp);  
  26. 。。。。。。。。。。。。  
  27. 。。。。。。。。。。。。  

修改后


[java]  view plain copy
  1. case KeyEvent.KEYCODE_POWER: {  
  2.                 result &= ~ACTION_PASS_TO_USER;           
  3.                 if (down) {  
  4.                Log.i("xxnan","xxnan"+"xiaxiangnan");      
  5.                int end_call_key=Settings.System.getInt(mContext.getContentResolver(),  
  6.                     "press_power_end_call",0); //取出数据库中是否打开这一功能的值    
  7.                Log.i("end_call_key","end_call_key="+end_call_key);  
  8.                     ITelephony telephonyService = getTelephonyService();      
  9.              boolean hungUp = false;                      
  10.                     if (telephonyService != null) {  
  11.                         try {  
  12.                             //如果是电话正在打且开启了这一功能,当按power键就挂掉电话    
  13.                             if (telephonyService.isRinging()&&end_call_key==1) {  
  14.                                 // Pressing Power while there's a ringing incoming  
  15.                                 // call should silence the ringer.  
  16.                                // telephonyService.silenceRinger();                        
  17.                               hungUp=telephonyService.endCall();  
  18.                             } else if ((mIncallPowerBehavior  
  19.                                     & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0  
  20.                                     && telephonyService.isOffhook()) {  
  21.                                 // Otherwise, if "Power button ends call" is enabled,  
  22.                                 // the Power button will hang up any current active call.  
  23.                                 hungUp = telephonyService.endCall();  
  24.                             }  
  25.                            
  26.                         } catch (RemoteException ex) {  
  27.                             Log.w(TAG, "ITelephony threw RemoteException", ex);  
  28.                         }  
  29.                     }  
  30.                     interceptPowerKeyDown(!isScreenOn || hungUp);  
  31. 。。。。。。。。。。。  
  32. 。。。。。。。。。。。  
由于我这个开发板上是不能插电话卡的也就没能实验成功,但是原理应该就这样的!


最后修改过的地方都要重新编译,那么我们要在源码下编译app下的Phone以及framework下的policy


最后生成的out/。。。/system/app/Phone.apk和out/。。。。/system/framework/android.policy.jar都要替换


手机里的相同(adb shell 进入你的手机,要有root权限)文件应该就可以了。
相关文章
|
17天前
|
Web App开发 网络协议 Android开发
Android平台一对一音视频通话方案大比拼:WebRTC VS RTMP VS RTSP,谁才是王者?
【9月更文挑战第4天】本文详细对比了在Android平台上实现一对一音视频通话时常用的WebRTC、RTMP及RTSP三种技术方案。从技术原理、性能表现与开发难度等方面进行了深入分析,并提供了示例代码。WebRTC适合追求低延迟和高质量的场景,但开发成本较高;RTMP和RTSP则在简化开发流程的同时仍能保持较好的传输效果,适用于不同需求的应用场景。
38 1
|
Java Android开发 API
Android电源管理系列之PowerManagerService(二)
WakeLock机制 PowerManager.WakeLock 为了延长电池的使用寿命,Android设备会在一段时间后使屏幕变暗,然后关闭屏幕显示,直至停止CPU进入休眠。
1805 0
|
4月前
|
Java Android开发
Android 长按电源键弹出的GlobalActions菜单
Android 长按电源键弹出的GlobalActions菜单
93 1
|
Java Android开发
android 7.1 屏蔽按压两次电源键(KEYCODE_POWER)打开相机
android 7.1 屏蔽按压两次电源键(KEYCODE_POWER)打开相机
282 0
|
Web App开发 编解码 网络协议
Android平台一对一音视频通话方案对比:WebRTC VS RTMP VS RTSP
Android平台一对一音视频通话方案对比:WebRTC VS RTMP VS RTSP
343 0
|
Java 机器人 Android开发
Android10.0(Q) 实现通话中播放音乐/通话背景音(答录机/魔音功能)
Android10.0(Q) 实现通话中播放音乐/通话背景音(答录机/魔音功能)
912 0
|
Java Android开发
Android8.1 MTK平台 Dialer修改(通话常亮、按钮接听)
Android8.1 MTK平台 Dialer修改(通话常亮、按钮接听)
330 0
|
XML Java Android开发
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
85 0
|
Web App开发 网络协议 Java
android WebRtc 视频通话(P2P)
android WebRtc 视频通话(P2P)
850 0
android WebRtc 视频通话(P2P)
|
开发工具 Android开发
产品百科 | RTC Android SDK 如何在通话前进行网络测速
阿里云 RTC SDK 为您提供网络测速的接口方法,帮助您在音视频通话前进行网络测速。您可以根据当前的网络质量切换网络,获取更高质量的音视频通话。
产品百科 | RTC Android SDK 如何在通话前进行网络测速