閱讀647 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android 添加按電源鍵結束通話

首先我們發現現在我們所用的android智能手機大部分都有當你在打電話時按power鍵來掛斷電話,一般都是在設置中。



我主要是在原生源碼中添加這一功能,主要用於學習。。。。先看一張圖:

  


看到那個按電源鍵掛斷電話吧,那就是我所添加的,本來原生源碼中是沒有這一欄的。。。。。


大概思路:


首先我先添加這一個checkboxPreference,然後將是否選擇這一功能的值(0和1)存到data/data/com.android.providers.settings


/databases/settings.db數據庫的system表中


,然後再根據數據庫表中的值在PhoneWindownManager.java中去處理。



具體過程:



首先找到setting的源碼,在源碼下我們要找到通話設置,在seting.xml中我們能找到


[java] view plaincopy
  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 plaincopy
  1. <span style="font-size:14px;"><PreferenceScreen xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     xmlns:phone="https://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 plaincopy
  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 plaincopy
  1. <span style="font-size:14px;"><PreferenceScreen xmlns:android="https://schemas.android.com/apk/res/android"  
  2.     xmlns:phone="https://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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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權限)文件應該就可以了。

最後更新:2017-04-04 07:03:32

  上一篇:go 提升Android應用視覺效果的10個UI技巧
  下一篇:go 詳談Ubuntu軟件管理丟失的功能