注意: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 {

publicstaticITelephony getITelephony(TelephonyManager telephony)throwsException {

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

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

return(ITelephony)getITelephonyMethod.invoke(telephony);

}

}

package com.zhouzijing.android.demo;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.telephony.TelephonyManager;

public class PhoneUtils {

public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {

Method getITelephonyMethod = 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 {

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

}

}

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;

}

}

}

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 MyPhoneBroadcastReceiverextends 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);

}

}

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

Java代码  

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;

privateMyPhoneBroadcastReceivermBroadcastReceiver;

@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);

}

}

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

@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配置权限

Xml代码  

其中:

Java代码  

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

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

必须有权限

android.permission.MODIFY_PHONE_STATE

Java代码  

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

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

必须有权限 android.permission.CALL_PHONE

android 自动挂断,[转]android 来电自动接听和自动挂断相关推荐

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

    转自http://stephen830.iteye.com/blog/1181786java android 来电自动接听和自动挂断android 注意:android2.3版本不支持下面的自动接听方 ...

  2. Android 自动接听电话和挂断

    注意:android2.3版本及以上不支持下面的自动接听方法.(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current pro ...

  3. android 拨测软件,通信网络性能管理系统中电话自动拨测技术的实现

    摘要: 当今世界,无线通信技术已经非常普遍,通信网络的覆盖区域越来越广,无线通讯技术的飞速发展为我们的生活带来了极大的便利.通信网络结构趋于复杂,伴随着通信网络使用的设备也趋于复杂,所以如何进行有效地 ...

  4. android进程自动启动时间,如何统计Android App启动时间

    随着App的逻辑不断庞大,一不注意就会将耗时的操作放置在应用启动过程之中,导致应用启动速度越来越慢,用户体验也越来越差.优化启动速度是几乎所有大型App应用开发者需要考虑的问题.优化启动速度之前首先需 ...

  5. android应用程序的自动更新升级(自身升级,通过tomcat),[SaltStack] Minion-conf自动更新...

    minion-conf配置文件自动更新, 加载 minion-conf是每个minion自身以来的配置, 为了方便我们在中心管控机上(Master)统一配置, 然后下发文件, 进而使得Minion能够 ...

  6. android 监听安装来源_Flutter插件开发之APK自动安装

    点击上方的终端研发部,右上角选择"设为星标" 每日早9点半,技术文章准时送上 公众号后台回复"学习",获取作者独家秘制精品资料 往期文章 记五月的一个Andro ...

  7. android 关闭蓝牙打电话功能,Android蓝牙开发【八】hfp接听、挂断电话

    继续研究hfp相关功能.蓝牙耳机可以控制手机接听.拒接.挂断电话,拨打电话等功能.本文主要分析下起这些操作的大致流程. 在系统应用Bluetooth中com_android_bluetooth.cpp ...

  8. android通知栏应用程序更新,Android App自动更新之通知栏下载

    本文实例为大家分享了Android App自动更新通知栏下载的具体代码,供大家参考,具体内容如下 版本更新说明 这里有调用UpdateService启动服务检查下载安装包等 1. 文件下载,下完后写入 ...

  9. 自动驾驶领域的Android?百度能否超越特斯拉和谷歌

    本文来源作者 深潜atom 12月15日,有媒体报道,百度正在考虑生产属于自己的电动汽车,或将组建一家持有多数股权的合资公司.目前百度已与包括浙江吉利.广州汽车集团和中国一汽在内的汽车制造商就可能的合 ...

最新文章

  1. 哈哈哈,这个教人写出烂代码的项目在 GitHub 上火了...
  2. Python+selenium 自动化-selenium的版本查看和升级
  3. SAP - 银企直联
  4. win8.1 mysql使用教程_windows下mysql 8.0.12安装步骤及基本使用教程
  5. childNodes详解
  6. C#备份数据和还原数据
  7. java 获取路径_java 获取当前类的路径
  8. 扩展 TextBox 控件 - 依 FormViewMode 来自行设定状态
  9. ArcGIS Engine中如何获取Map中已经选择的要素呢(转)
  10. java autointeger_【Java多线程】线程安全的Integer,AutomicInteger
  11. Hello,Java!(娱乐)
  12. 电流速断保护c语言程序,[单选] 电流电压联锁速断保护与过电流保护十分相似,只是取消了()继电器。...
  13. 利用canvas打造一个炫酷的粒子背景
  14. 在线二维码生成工具html源码
  15. 利用Jsoup爬取网页内容
  16. 深度学习 - 记忆网络
  17. 分类——LDA、QDA
  18. Markdown编辑器Editor.md插件的使用
  19. 每日新闻摘要:YouTube备受关注
  20. 关于计算机的英语名言,英语名人名言:计算机Computers/Technology

热门文章

  1. mysql sql优化器_MySQL SQL优化之‘%’
  2. 网站显示不正常服务器怎么弄,你真的知道网站出现收录不正常的原因是什么吗...
  3. 大连理工优化方法matlab,大连理工大学2016年秋季优化方法大作业.pdf
  4. 大地形pawn抖动问题
  5. 解决链接错误:error LNK2001: 无法解析的外部符号 __iob
  6. 基于TCP协议的网络程序(基础学习)
  7. 介绍最全的LVS负载均衡技术
  8. 解决方案架构师我需要懂代码吗_架构师不写代码,能行吗?
  9. 【转】二维异形件排版算法介绍(三)
  10. 【转】云服务器cvm 云服务器ecs区别