android 自动挂断,android 来电自动接听和自动挂断(2.3以上)

转自http://stephen830.iteye.com/blog/1181786java

android 来电自动接听和自动挂断android

注意:android2.3版本不支持下面的自动接听方法。(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current process has android.permission.MODIFY_PHONE_STATE.)app

第一步:准备应用环境须要的系统包和aidl文件。框架

(1)在应用中建立包:android.telephonyide

将android系统框架下的\framework\telephony\java\android\telephony目录中的NeighboringCellInfo.aidl文件复制到上面建立的包(android.telephony )中;函数

(2)在应用中建立包:com.android.internal.telephonyspa

将android系统框架下的\framework\telephony\java\com\android\internal\telephony目录中的ITelephony.aidl文件复制到上面建立的包(com.android.internal.telephony )中;xml

第二步:建立一个获取ITelephony的方法blog

PhoneUtils.javaci

Java代码

982263c7f65a4e0e922d1f4c.html

packagecom.zhouzijing.android.demo;

importjava.lang.reflect.Method;

importcom.android.internal.telephony.ITelephony;

importandroid.telephony.TelephonyManager;

publicclassPhoneUtils {

/**

* 根据传入的TelephonyManager来取得系统的ITelephony实例.

* @param telephony

* @return 系统的ITelephony实例

* @throws Exception

*/

publicstaticITelephony getITelephony(TelephonyManager telephony)throwsException {

Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");

getITelephonyMethod.setAccessible(true);//私有化函数也能使用

return(ITelephony)getITelephonyMethod.invoke(telephony);

}

}

第三步:建立电话广播拦截器

MyPhoneBroadcastReceiver.java

Java代码

982263c7f65a4e0e922d1f4c.html

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 {

privatefinalstaticString TAG = MyPhone.TAG;

@Override

publicvoidonReceive(Context context, Intent intent) {

String action = intent.getAction();

Log.i(TAG, "[Broadcast]"+action);

//呼入电话

if(action.equals(MyPhone.B_PHONE_STATE)){

Log.i(TAG, "[Broadcast]PHONE_STATE");

doReceivePhone(context,intent);

}

}

/**

* 处理电话广播.

* @param context

* @param intent

*/

publicvoiddoReceivePhone(Context context, Intent intent) {

String phoneNumber = intent.getStringExtra(

TelephonyManager.EXTRA_INCOMING_NUMBER);

TelephonyManager telephony = (TelephonyManager)context.getSystemService(

Context.TELEPHONY_SERVICE);

intstate = telephony.getCallState();

switch(state){

caseTelephonyManager.CALL_STATE_RINGING:

Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);

try{

ITelephony iTelephony = PhoneUtils.getITelephony(telephony);

iTelephony.answerRingingCall();//自动接通电话

//iTelephony.endCall();//自动挂断电话

} catch(Exception e) {

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

982263c7f65a4e0e922d1f4c.html

packagecom.zhouzijing.android.demo;

importandroid.app.Activity;

importandroid.content.IntentFilter;

importandroid.os.Bundle;

importandroid.telephony.TelephonyManager;

importandroid.util.Log;

importandroid.view.View;

publicclassMyPhoneextendsActivity {

publicfinalstaticString TAG ="MyPhone";

publicfinalstaticString B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;

privateMyPhoneBroadcastReceiver mBroadcastReceiver;

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.my_phone);

}

//按钮1-注册广播

publicvoidregisterThis(View v) {

Log.i(TAG, "registerThis");

mBroadcastReceiver = newMyPhoneBroadcastReceiver();

IntentFilter intentFilter = newIntentFilter();

intentFilter.addAction(B_PHONE_STATE);

intentFilter.setPriority(Integer.MAX_VALUE);

registerReceiver(mBroadcastReceiver, intentFilter);

}

//按钮2-撤销广播

publicvoidunregisterThis(View v) {

Log.i(TAG, "unregisterThis");

unregisterReceiver(mBroadcastReceiver);

}

}

第5步:在AndroidManifest.xml配置权限

Xml代码

982263c7f65a4e0e922d1f4c.html

其中:

Java代码

982263c7f65a4e0e922d1f4c.html

iTelephony.answerRingingCall();//自动接通电话

必须有权限 android.permission.MODIFY_PHONE_STATE

Java代码

982263c7f65a4e0e922d1f4c.html

iTelephony.endCall();//自动挂断电话

必须有权限 android.permission.CALL_PHONE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值