android 来电自动接听和自动挂断
来源:互联网 发布:域名的注册费用 编辑:程序博客网 时间:2024/06/10 19:38
转自: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
- package com.zhouzijing.android.demo;
- import java.lang.reflect.Method;
- import com.android.internal.telephony.ITelephony;
- import android.telephony.TelephonyManager;
- public class PhoneUtils {
- /**
- * 根据传入的TelephonyManager来取得系统的ITelephony实例.
- * @param telephony
- * @return 系统的ITelephony实例
- * @throws Exception
- */
- public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
- Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
- getITelephonyMethod.setAccessible(true);//私有化函数也能使用
- return (ITelephony)getITelephonyMethod.invoke(telephony);
- }
- }
第三步:创建电话广播拦截器
MyPhoneBroadcastReceiver.java
- package com.zhouzijing.android.demo;
- import com.android.internal.telephony.ITelephony;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- public class MyPhoneBroadcastReceiver extends BroadcastReceiver {
- private final static String TAG = MyPhone.TAG;
- @Override
- public void onReceive(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
- */
- public void doReceivePhone(Context context, Intent intent) {
- String phoneNumber = intent.getStringExtra(
- TelephonyManager.EXTRA_INCOMING_NUMBER);
- TelephonyManager telephony = (TelephonyManager)context.getSystemService(
- Context.TELEPHONY_SERVICE);
- int state = telephony.getCallState();
- switch(state){
- case TelephonyManager.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;
- case TelephonyManager.CALL_STATE_IDLE:
- Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);
- break;
- case TelephonyManager.CALL_STATE_OFFHOOK:
- Log.i(TAG, "[Broadcast]通话中="+phoneNumber);
- break;
- }
- }
- }
第四部:注册电话广播拦截器
MyPhone.java
- package com.zhouzijing.android.demo;
- import android.app.Activity;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.view.View;
- public class MyPhone extends Activity {
- public final static String TAG = "MyPhone";
- public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;
- private MyPhoneBroadcastReceiver mBroadcastReceiver;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.my_phone);
- }
- //按钮1-注册广播
- public void registerThis(View v) {
- Log.i(TAG, "registerThis");
- mBroadcastReceiver = new MyPhoneBroadcastReceiver();
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(B_PHONE_STATE);
- intentFilter.setPriority(Integer.MAX_VALUE);
- registerReceiver(mBroadcastReceiver, intentFilter);
- }
- //按钮2-撤销广播
- public void unregisterThis(View v) {
- Log.i(TAG, "unregisterThis");
- unregisterReceiver(mBroadcastReceiver);
- }
- }
第5步:在AndroidManifest.xml配置权限
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
- <uses-permission android:name="android.permission.CALL_PHONE"/>
其中:
- iTelephony.answerRingingCall();//自动接通电话
必须有权限 android.permission.MODIFY_PHONE_STATE
- iTelephony.endCall();//自动挂断电话
必须有权限 android.permission.CALL_PHONE
- android 来电自动接听和自动挂断
- android 来电自动接听和自动挂断
- android 来电自动接听和自动挂断
- android 来电自动接听和自动挂断(2.3以上)
- 自动接听或者挂断来电
- Android 自动接听来电
- Android 自动接听来电
- Android 自动接听来电
- Android 来电自动接听
- android自动接听和挂断电话
- Android 电话自动接听和挂断详解
- android 自动挂断来电
- android电话自动接听/挂断
- 自动接听和拒绝来电
- Android 实现自动接听和挂断电话功能
- Android自动接听和挂断电话实现原理
- Android自动接听和挂断电话实现原理
- android低版本自动接听/挂断实现
- srand((unsigned)time(NULL))
- lpc2200移植ucos-II总结(一 移植环境)
- 怎样写一个拼写检查器
- 关于一则线程与进程的微博
- 解决“ .dll(或.ocx)控件已加载,但对DllregisterServer的调用失败,错误代码为0x80004005”问题的两种终极方法:SubInACL安装 VS 命令提示符
- android 来电自动接听和自动挂断
- 调整分区容量
- wpf使用资源字典组织资源
- 单片机+wifi的10个实验
- Java解析XML的四种方法
- Linux环境进程间通信: 共享内存
- 解决nginx 504 Gateway Time-out的一些方法
- lpc2200移植ucos-II总结(二 移植简介)
- SAX解析xml文件