android 来电自动接听和自动挂断
转自:http://stephen830.iteye.com/blog/1181786
//-----------------------------------------------------------------------------------------------------------------------------------//
android 来电自动接听和自动挂断
注意:android2.3版本不支持下面的自动接听方法。(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current process has android.permission.MODIFY_PHONE_STATE.)
第一步:准备应用环境需要的系统包和aidl文件。
(1)在应用中创建包:android.telephony
将android系统框架下的\framework\telephony\java\android\telephony目录中的NeighboringCellInfo.aidl文件复制到上面创建的包(android.telephony )中;
(2)在应用中创建包:com.android.internal.telephony
将android系统框架下的\framework\telephony\java\com\android\internal\telephony目录中的ITelephony.aidl文件复制到上面创建的包(com.android.internal.telephony )中;
第二步:创建一个获取ITelephony的方法
PhoneUtils.java
Java代码- packagecom.zhouzijing.android.demo;
- importjava.lang.reflect.Method;
- importcom.android.internal.telephony.ITelephony;
- importandroid.telephony.TelephonyManager;
- publicclassPhoneUtils{
- /**
- *根据传入的TelephonyManager来取得系统的ITelephony实例.
- *@paramtelephony
- *@return系统的ITelephony实例
- *@throwsException
- */
- publicstaticITelephonygetITelephony(TelephonyManagertelephony)throwsException{
- MethodgetITelephonyMethod=telephony.getClass().getDeclaredMethod("getITelephony");
- getITelephonyMethod.setAccessible(true);//私有化函数也能使用
- return(ITelephony)getITelephonyMethod.invoke(telephony);
- }
- }
第三步:创建电话广播拦截器
MyPhoneBroadcastReceiver.java
Java代码- packagecom.zhouzijing.android.demo;
- importcom.android.internal.telephony.ITelephony;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.telephony.TelephonyManager;
- importandroid.util.Log;
- publicclassMyPhoneBroadcastReceiverextendsBroadcastReceiver{
- privatefinalstaticStringTAG=MyPhone.TAG;
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- Stringaction=intent.getAction();
- Log.i(TAG,"[Broadcast]"+action);
- //呼入电话
- if(action.equals(MyPhone.B_PHONE_STATE)){
- Log.i(TAG,"[Broadcast]PHONE_STATE");
- doReceivePhone(context,intent);
- }
- }
- /**
- *处理电话广播.
- *@paramcontext
- *@paramintent
- */
- publicvoiddoReceivePhone(Contextcontext,Intentintent){
- StringphoneNumber=intent.getStringExtra(
- TelephonyManager.EXTRA_INCOMING_NUMBER);
- TelephonyManagertelephony=(TelephonyManager)context.getSystemService(
- Context.TELEPHONY_SERVICE);
- intstate=telephony.getCallState();
- switch(state){
- caseTelephonyManager.CALL_STATE_RINGING:
- Log.i(TAG,"[Broadcast]等待接电话="+phoneNumber);
- try{
- ITelephonyiTelephony=PhoneUtils.getITelephony(telephony);
- iTelephony.answerRingingCall();//自动接通电话
- //iTelephony.endCall();//自动挂断电话
- }catch(Exceptione){
- Log.e(TAG,"[Broadcast]Exception="+e.getMessage(),e);
- }
- break;
- caseTelephonyManager.CALL_STATE_IDLE:
- Log.i(TAG,"[Broadcast]电话挂断="+phoneNumber);
- break;
- caseTelephonyManager.CALL_STATE_OFFHOOK:
- Log.i(TAG,"[Broadcast]通话中="+phoneNumber);
- break;
- }
- }
- }
第四部:注册电话广播拦截器
MyPhone.java
Java代码- packagecom.zhouzijing.android.demo;
- importandroid.app.Activity;
- importandroid.content.IntentFilter;
- importandroid.os.Bundle;
- importandroid.telephony.TelephonyManager;
- importandroid.util.Log;
- importandroid.view.View;
- publicclassMyPhoneextendsActivity{
- publicfinalstaticStringTAG="MyPhone";
- publicfinalstaticStringB_PHONE_STATE=TelephonyManager.ACTION_PHONE_STATE_CHANGED;
- privateMyPhoneBroadcastReceivermBroadcastReceiver;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.my_phone);
- }
- //按钮1-注册广播
- publicvoidregisterThis(Viewv){
- Log.i(TAG,"registerThis");
- mBroadcastReceiver=newMyPhoneBroadcastReceiver();
- IntentFilterintentFilter=newIntentFilter();
- intentFilter.addAction(B_PHONE_STATE);
- intentFilter.setPriority(Integer.MAX_VALUE);
- registerReceiver(mBroadcastReceiver,intentFilter);
- }
- //按钮2-撤销广播
- publicvoidunregisterThis(Viewv){
- Log.i(TAG,"unregisterThis");
- unregisterReceiver(mBroadcastReceiver);
- }
- }
第5步:在AndroidManifest.xml配置权限
Xml代码- <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
- <uses-permissionandroid:name="android.permission.MODIFY_PHONE_STATE"/>
- <uses-permissionandroid:name="android.permission.CALL_PHONE"/>
其中:
Java代码- iTelephony.answerRingingCall();//自动接通电话
必须有权限 android.permission.MODIFY_PHONE_STATE
Java代码
- iTelephony.endCall();//自动挂断电话
必须有权限 android.permission.CALL_PHONE
更多相关文章
- Android(安卓)studio 学习3:实现倒计时、画板
- view的隐藏和显示
- 2011.09.20(4)——— android SpannableString 文字处理
- 全屏
- 第一行代码Android第三课
- Android(安卓)开源源代码收集(不断更新中...)
- 我的Android进阶之旅------>Android电话实例
- Android(安卓)打开浏览器的几种方法
- android打电话,接电话,挂电话过程