android 添加按(power键)电源键结束通话(挂断电话)

 更新时间:2013年01月08日 17:50:05   作者:  
首先我们发现现在我们所用的android智能手机大部分都有当你在打电话时按power键来挂断电话,一般都是在设置中
首先我们发现现在我们所用的android智能手机大部分都有当你在打电话时按power键来挂断电话,一般都是在设置中。
我主要是在原生源码中添加这一功能,主要用于学习。。。。先看一张图:
 
看到那个按电源键挂断电话吧,那就是我所添加的,本来原生源码中是没有这一栏的。。。。。

大概思路
首先我先添加这一个checkboxPreference,然后将是否选择这一功能的值(0和1)存到data/data/com.android.providers.settings
/databases/settings.db数据库的system表中
,然后再根据数据库表中的值在PhoneWindownManager.java中去处理。

具体过程
首先找到setting的源码,在源码下我们要找到通话设置,在seting.xml中我们能找到
复制代码 代码如下:

<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>

这个call_settings就是我们在setting(设置)中看到的通话设置,但是我们却不能在settings中的源码中找到关于call_settings的布局文件, 因此我们需要找到它,其实这个布局文件是在package/app/Phone中,也就是在Phone这个app源码的资源文件中。

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

<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>

因此我们可以在最前面添加一个checkboxPreference
复制代码 代码如下:

<SPAN style="FONT-SIZE: 14px"><CheckBoxPreference
android:key="press_power_end_call_key"
android:title="@string/press_power_end_call"
android:persistent="false"/></SPAN>

变成
复制代码 代码如下:

<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>

在这里有自己定义的
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键挂电话的事件:
复制代码 代码如下:

<SPAN style="FONT-SIZE: 14px">//add by xxnan
else if (preference == press_power_end_call) {
//如果勾选就将1存到system表的press_power_end_call中
Settings.System.putInt(getContentResolver(),
"press_power_end_call",
press_power_end_call.isChecked() ? 1 : 0);
//end by xxnan </SPAN>

在OnCreate添加如下代码之后
复制代码 代码如下:

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);
// 获的数据库system表里press_power_end_call的值,也就是是否选择了checkboxpreference
int press_power_end_call_key=Settings.System.getInt(getContentResolver(),
"press_power_end_call",0);
//如果得到的值是1,则下次打开setting的话,选项框要勾选
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);
。。。。。。。
。。。。。。。

这样就算差不多完成了到获取是否开启这一功能存放和取出到系统数据库中,接下来就是到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键的事件,在这个方法里我们只需要添加很少代码:
原来代码是
复制代码 代码如下:

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();
} 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);
。。。。。。。。。。。。
。。。。。。。。。。。。

修改后
复制代码 代码如下:

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); //取出数据库中是否打开这一功能的值
Log.i("end_call_key","end_call_key="+end_call_key);
ITelephony telephonyService = getTelephonyService();
boolean hungUp = false;
if (telephonyService != null) {
try {
//如果是电话正在打且开启了这一功能,当按power键就挂掉电话
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();
} 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);
。。。。。。。。。。。
。。。。。。。。。。。

由于我这个开发板上是不能插电话卡的也就没能实验成功,但是原理应该就这样的!
最后修改过的地方都要重新编译,那么我们要在源码下编译app下的Phone以及framework下的policy
最后生成的out/。。。/system/app/Phone.apk和out/。。。。/system/framework/android.policy.jar都要替换
手机里的相同(adb shell 进入你的手机,要有root权限)文件应该就可以了。

相关文章

  • android几种不同对话框的实现方式

    android几种不同对话框的实现方式

    这篇文章介绍了android几种不同对话框的实现,主要包括:1、显示提示消息的对话框.2、简单列表项对话框。3、单选列表项对话框。4、多选列表对话框。5、自定义列表项对话框。6、自定义View的对话框,需要的朋友可以参考下
    2015-08-08
  • Android 获取IP和UA实现示例详解

    Android 获取IP和UA实现示例详解

    这篇文章主要为大家介绍了Android 获取IP和UA实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android开发Jetpack Compose元素Modifier特性详解

    Android开发Jetpack Compose元素Modifier特性详解

    这篇文章主要为大家介绍了Android开发Jetpack Compose元素Modifier特性详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android视图绑定viewBinding的使用介绍

    Android视图绑定viewBinding的使用介绍

    最近这段时间在学习Kotlin,突然发现谷歌已经把kotlin-android-extensions插件废弃,目前推荐使用ViewBinding来进行替代,接下来通过本文给大家分享Android使用ViewBinding的详细步骤,感兴趣的朋友一起学习吧
    2023-01-01
  • Hook实现Android 微信、陌陌 、探探位置模拟(附源码下载)

    Hook实现Android 微信、陌陌 、探探位置模拟(附源码下载)

    这篇文章主要介绍了Hook实现Android 微信、陌陌 、探探位置模拟(附源码下载)的相关资料,需要的朋友可以参考下
    2017-03-03
  • Android基于方法池与回调实现登录拦截的场景

    Android基于方法池与回调实现登录拦截的场景

    这篇文章主要为大家介绍了Android基于方法池与回调实现登录拦截的场景详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android O添加桌面快捷方式的示例

    Android O添加桌面快捷方式的示例

    本篇文章主要介绍了AndroidO添加桌面快捷方式的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • SimpleCommand实现图片下载(二)

    SimpleCommand实现图片下载(二)

    这篇文章主要为大家详细介绍了SimpleCommand实现图片下载,并显示到ImageView控件上,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android Studio Kotlin代码和java代码相互转化实例

    Android Studio Kotlin代码和java代码相互转化实例

    这篇文章主要介绍了Android Studio Kotlin代码和java代码相互转化实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android如何实现URL转换成二维码

    Android如何实现URL转换成二维码

    这篇文章主要为大家详细介绍了Android实现URL转换成二维码的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04

最新评论