android实现接通和挂断电话

发布时间:2020-08-21 01:52:02

来源:脚本之家

阅读:230

作者:WillenWu

本文实例为大家分享了android实现接通和挂断电话的具体代码,供大家参考,具体内容如下

关键代码:【PhoneUtils类】

package com.ebupt.phonerecorddemo.server;

import java.lang.reflect.Method;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.util.Log;

import android.view.KeyEvent;

import com.android.internal.telephony.ITelephony;

public class PhoneUtils {

static String TAG = "PhoneUtils";

/**

* 从TelephonyManager中实例化ITelephony,并返回

*/

static public ITelephony getITelephony(TelephonyManager telMgr)

throws Exception {

Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod(

"getITelephony");

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

return (ITelephony) getITelephonyMethod.invoke(telMgr);

}

//自动接听

public static void autoAnswerPhone(Context c,TelephonyManager tm) {

try {

Log.i(TAG, "autoAnswerPhone");

ITelephony itelephony = getITelephony(tm);

// itelephony.silenceRinger();

itelephony.answerRingingCall();

} catch (Exception e) {

e.printStackTrace();

try {

Log.e(TAG, "用于Android2.3及2.3以上的版本上");

Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");

KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,

KeyEvent.KEYCODE_HEADSETHOOK);

intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);

c.sendOrderedBroadcast(intent,

"android.permission.CALL_PRIVILEGED");

intent = new Intent("android.intent.action.MEDIA_BUTTON");

keyEvent = new KeyEvent(KeyEvent.ACTION_UP,

KeyEvent.KEYCODE_HEADSETHOOK);

intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent);

c.sendOrderedBroadcast(intent,

"android.permission.CALL_PRIVILEGED");

Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);

localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

localIntent1.putExtra("state", 1);

localIntent1.putExtra("microphone", 1);

localIntent1.putExtra("name", "Headset");

c.sendOrderedBroadcast(localIntent1,

"android.permission.CALL_PRIVILEGED");

Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);

KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,

KeyEvent.KEYCODE_HEADSETHOOK);

localIntent2.putExtra("android.intent.extra.KEY_EVENT",

localKeyEvent1);

c.sendOrderedBroadcast(localIntent2,

"android.permission.CALL_PRIVILEGED");

Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);

KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,

KeyEvent.KEYCODE_HEADSETHOOK);

localIntent3.putExtra("android.intent.extra.KEY_EVENT",

localKeyEvent2);

c.sendOrderedBroadcast(localIntent3,

"android.permission.CALL_PRIVILEGED");

Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);

localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

localIntent4.putExtra("state", 0);

localIntent4.putExtra("microphone", 1);

localIntent4.putExtra("name", "Headset");

c.sendOrderedBroadcast(localIntent4,

"android.permission.CALL_PRIVILEGED");

} catch (Exception e2) {

e2.printStackTrace();

Intent meidaButtonIntent = new Intent(

Intent.ACTION_MEDIA_BUTTON);

KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP,

KeyEvent.KEYCODE_HEADSETHOOK);

meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);

c.sendOrderedBroadcast(meidaButtonIntent, null);

}

}

}

//自动挂断

public static void endPhone(Context c,TelephonyManager tm) {

try {

Log.i(TAG, "endPhone");

ITelephony iTelephony;

Method getITelephonyMethod = TelephonyManager.class

.getDeclaredMethod("getITelephony", (Class[]) null);

getITelephonyMethod.setAccessible(true);

iTelephony = (ITelephony) getITelephonyMethod.invoke(tm,

(Object[]) null);

// 挂断电话

iTelephony.endCall();

} catch (Exception e) {

e.printStackTrace();

}

}

}

需要用到的ITelephony.aidl:

package com.android.internal.telephony;

/**

* Interface used to interact with the phone. Mostly this is used by the

* TelephonyManager class. A few places are still using this directly.

* Please clean them up if possible and use TelephonyManager instead.

* {@hide}

*/

interface ITelephony {

/**

* End call or go to the Home screen

* @return whether it hung up

*/

boolean endCall();

/**

* Answer the currently-ringing call.

*

* If there's already a current active call, that call will be

* automatically put on hold. If both lines are currently in use, the

* current active call will be ended.

*

* TODO: provide a flag to let the caller specify what policy to use

* if both lines are in use. (The current behavior is hardwired to

* "answer incoming, end ongoing", which is how the CALL button

* is specced to behave.)

*

* TODO: this should be a oneway call (especially since it's called

* directly from the key queue thread).

*/

void answerRingingCall();

/**

* Silence the ringer if an incoming call is currently ringing.

* (If vibrating, stop the vibrator also.)

*

* It's safe to call this if the ringer has already been silenced, or

* even if there's no incoming call. (If so, this method will do nothing.)

*

* TODO: this should be a oneway call too (see above).

* (Actually *all* the methods here that return void can

* probably be oneway.)

*/

void silenceRinger();

/**

* Allow mobile data connections.

*/

boolean enableDataConnectivity();

/**

* Disallow mobile data connections.

*/

boolean disableDataConnectivity();

/**

* Report whether data connectivity is possible.

*/

boolean isDataConnectivityPossible();

}

监听通话广播【PhoneReceiver.java】:

package com.ebupt.phonerecorddemo.server;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.util.Log;

public class PhoneReceiver extends BroadcastReceiver {

String TAG = "PhoneReceiver";

@Override

public void onReceive(Context context, Intent intent) {

TelephonyManager tm = (TelephonyManager) context

.getSystemService(Service.TELEPHONY_SERVICE);

switch (tm.getCallState()) {

case TelephonyManager.CALL_STATE_OFFHOOK:// 电话打进来接通状态;电话打出时首先监听到的状态。

Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK");

break;

case TelephonyManager.CALL_STATE_RINGING:// 电话打进来状态

Log.i("onCallStateChanged", "CALL_STATE_RINGING");

PhoneUtils.autoAnswerPhone(context,tm);

break;

case TelephonyManager.CALL_STATE_IDLE:// 不管是电话打出去还是电话打进来都会监听到的状态。

Log.i("onCallStateChanged", "CALL_STATE_IDLE");

break;

}

}

}

在上面类适当的地方加上挂断或接通电话的代码即可。

最后别忘记在AndroidManifest.xml里声明和注册权限。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

android挂断电话广播,android实现接通和挂断电话相关推荐

  1. Android通过AIDL实现接听电话、挂断电话操作 | 拨打电话

    Android中拨打电话功能是很轻松的只需要几行代码即可,如下: 首先还是得加上权限这个东西(或者动态申请) <!--拨打电话--> <uses-permission android ...

  2. Android 5.1 Phone 挂断电话流程分析

    写在前面的话 本文主要分析Android挂断电话的流程,研究的代码是Android 5.1的,以CDMA为例,GSM同理. 挂断电话主要分两种情况: 本地主动挂断电话 \color{red}{本地主动 ...

  3. Android自动接听和挂断电话实现原理

    转自:http://bbs.51cto.com/thread-1078059-1.html 一  前言 这两天要研究类似白名单黑名单以及手势自动接听的一些功能,所以呢,自然而然的涉及到怎么自动接听/挂 ...

  4. android 实现挂断电话【四种方式】,亲测可用

    网上查到两种挂断电话方式 第一种:通过反射得到隐藏的API,需要aidl文件,但是这种方式测试后,无效.方法写出来吧,也许对你有你有帮助. 首先在main文件下创建报名com.android.inte ...

  5. Android拨打、接听、挂断电话操作

    Android2.3之前的系统可以通过反射机制调用ITelephone的方法来挂断电话,因为Android2.3以后增加了对permission  android.permission.MODIFY_ ...

  6. Android挂断电话流程

    近期在友盟上看到许多关于挂断电话导致崩溃的问题,如下异常 java.lang.NoSuchMethodError: No interface method endCall()Z in class Lc ...

  7. Android挂断电话以及Java Class Loader

    Android中,要自己实现一个挂断电话方法时,很久之前可以endCall().不过现在已经不行了,要应用反射机制,获取到 "android.os.ServiceManager" ...

  8. android 自动拨打电话和挂断电话(反射和intent方式)

    欢迎来到博主的技术分享博客 今天分享的技术主要内容是自动拨打电话和挂断电话.自动拨打电话分为两种,一种是通过反射ITelephony这个类直接调用call方法,一种是通过intent方法.而挂断电话则 ...

  9. Android挂断电话代码

    监听来电这里就不贴代码了,这是监听到来电后挂断电话 /*** 通过反射的方式挂断电话*/public void endcall() {try {//获取到ServiceManagerClass< ...

最新文章

  1. ajax从mysql提取数据在html中_EXCEL混合内容中提取数据,其实很简单
  2. np.random.permutation
  3. jmi,mof,cwm
  4. 浅析校园安防视频监控设备发展趋势
  5. hive 添加分区
  6. 【版本工具】SVN-E155036 Working copy ... is too old Please see the ‘svn upgrade‘ command
  7. 连续设置多张背景图片并且平铺
  8. Cesium应用篇:3控件(3)SelectionIndicator InfoBox
  9. 披荆斩棘的北森,乘风破浪的HR SaaS
  10. 解决办法:look up error: undefined symbol
  11. 关于集异璧实验室(Geblab)
  12. 让PPT设计更出彩的技巧都有哪些?
  13. sql 查询in的集合元素过多优化方式
  14. 常见模拟电路设计 四 :比较器详讲
  15. Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
  16. vue $confirm 自定义图标及修改图标颜色
  17. NatApp免费内网穿透
  18. ubuntu微信中输入乱码解决
  19. 【opencv】车牌定位及倾斜较正
  20. 网络编程(Tcp/Udp实现聊天、文件上传)

热门文章

  1. 【求助:PowerShell激活win10失败】程序“slmgr.vbs”无法运行:找不到应用程序所在位置设置报错0xC004F074
  2. 基于ROS的使用USB相机识别QR码
  3. 无需root找回安卓手机误删除照片
  4. 梦幻西游维护公告里面的可转服务器,梦幻西游:大量服务器即将开放平转,跨服倒卖人人都能做...
  5. Axure RP:页面原型制作工具
  6. 未能从程序集.......
  7. Word、Excel中实现表格打印中,标题行在每页都显示
  8. 「Python」数据结构——字典和集合
  9. 发现属于你的大数据“矿脉”
  10. 点评阿里云、盛大云等国内IaaS产业