Android 来去电监听,电话挂断

Android 来去电监听,电话挂断,Android应用性能优化 考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新! 阅读详情

android:enabled=“true”

android:process=“:PhoneListenService”>

public class PhoneStateReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

// 去电,可以用定时挂断

} else {

//来电

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

Log.d(PhoneListenService.TAG, "PhoneStateReceiver onReceive state: " + state);

if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {

Log.d(PhoneListenService.TAG, “PhoneStateReceiver onReceive endCall”);

HangUpTelephonyUtil.endCall(context);

}

}

}

}

三. 实战,有什么需要特别注意地方


3.1 双卡双待的手机怎么获取

对于双卡手机,每张卡都对应一个 Service 和一个 PhoneStateListener,需要给每个服务注册自己的 Ph 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 oneStateListener,服务的名称还会有点变化,厂商可能会修改

public ArrayList getMultSimCardInfo() {

// 获取双卡的信息,这个也是经验尝试出来的,不知道其他厂商有什么坑

ArrayList phoneServerList = new ArrayList();

for(int i = 1; i < 3; i++) {

try {

String phoneServiceName;

if (MiuiUtils.isMiuiV6()) {

phoneServiceName = “phone.” + String.valueOf(i-1);

} else {

phoneServiceName = “phone” + String.valueOf(i);

}

// 尝试获取服务看是否能获取到

IBinder iBinder = ServiceManager.getService(phoneServiceName);

if(iBinder == null) continue;

ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);

if(iTelephony == null) continue;

phoneServerList.add(phoneServiceName);

} catch(Exception e) {

e.printStackTrace();

}

}

// 这个是默认的

phoneServerList.add(Context.TELEPHONY_SERVICE);

return phoneServerList;

}

3.2 挂断电话

挂断电话使用系统服务提供的接口去挂断,但是挂断电话是个并不能保证成功的方法,所以会有多种方式挂断同时使用,下面提供

package com.phone.listen;

import android.content.Context;

import android.os.RemoteException;

import android.telephony.TelephonyManager;

import com.android.internal.telephony.ITelephony;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.concurrent.Executor;

import java.util.concurrent.Executors;

/**

  • 封装挂断电话接口

*/

public class HangUpTelephonyUtil {

public static boolean endCall(Context context) {

boolean callSuccess = false;

ITelephony telephonyService = getTelephonyService(context);

try {

if (telephonyService != null) {

callSuccess = telephonyService.endCall();

}

} catch (RemoteException e) {

e.printStackTrace();

} catch (Exception e){

e.printStackTrace();

}

if (callSuccess == false) {

Executor eS = Executors.newSingleThreadExecutor();

eS.execute(new Runnable() {

@Override

public void run() {

disconnectCall();

}

});

callSuccess = true;

}

return callSuccess;

}

private static ITelephony getTelephonyService(Context context) {

TelephonyManager telephonyManager = (TelephonyManager)

context.getSystemService(Context.TELEPHONY_SERVICE);

Class clazz;

try {

clazz = Class.forName(telephonyManager.getClass().getName());

Method method = clazz.getDeclaredMethod(“getITelephony”);

method.setAccessible(true);

return (ITelephony) method.invoke(telephonyManager);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

return null;

}

private static boolean disconnectCall() {

Runtime runtime = Runtime.getRuntime();

try {

runtime.exec(“service call phone 5 \n”);

} catch (Exception exc) {

exc.printStackTrace();

return false;

}

return true;

}

// 使用 endCall 挂断不了,再使用 killCall 反射调用再挂一次

public static boolean killCall(Context context) {

try {

// Get the boring old TelephonyManager

TelephonyManager telephonyManager = (TelephonyManager)

context.getSystemService(Context.TELEPHONY_SERVICE);

// Get the getITelephony() method

Class classTelephony = Class.forName(telephonyManager.getClass().getName());

Method methodGetITelephony = classTelephony.getDeclaredMethod(“getITelephony”);

// Ignore that the method is supposed to be private

methodGetITelephony.setAccessible(true);

// Invoke getITelephony() to get the ITelephony interface

Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

// Get the endCall method from ITelephony

Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());

Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod(“endCall”);

// Invoke endCall()

methodEndCall.invoke(telephonyInterface);

} catch (Exception ex) { // Many things can go wrong with reflection calls

return false;

}

return true;

}

}

ITelephony 接口在 layoutlib.jar 包中,需要导入 android sdk 目录\platforms\android-8\data\layoutlib.jar

挂断电话需要权限

3.3 监听来去电状态放到后台服务(独立进程)

<service android:name=“.PhoneListenService”

android:label=“Android 来电监听”

android:process=“:PhoneListenService”/>

来去电监听 Service

package com.phone.listen;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.os.IBinder;

import android.telephony.PhoneStateListener;

import android.telephony.TelephonyManager;

import android.util.Log;

/**

  • 来去电监听服务

*/

public class PhoneListenService extends Service {

public static final String TAG = PhoneListenService.class.getSimpleName();

public static final String ACTION_REGISTER_LISTENER = “action_register_listener”;

@Override

public void onCreate() {

super.onCreate();

Log.d(TAG, “onCreate”);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(TAG, "onStartCommand action: " + intent.getAction() +

" flags: " + flags + " startId: " + startId);

String action = intent.getAction();

if (action.equals(ACTION_REGISTER_LISTENER)) {

registerPhoneStateListener();

}

return super.onStartCommand(intent, flags, startId);

}

private void registerPhoneStateListener() {

CustomPhoneStateListener customPhoneStateListener = new CustomPhoneStateListener(this);

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

if (telephonyManager != null) {

telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

}

}

}

通过PhoneStateListener实现Android电话监听 电话监听是比较简单的安卓案例。但却非常经典,因为它涵盖了动态监听、服务绑定、文件保存三大技术操作。作为Android学习的不错案例,今天我就和大家一起来看看安卓通过PhoneStateListener实现的电话监听。 由于监听方法比较多,方法也可以不断修正改良,作为原理讲解,我们就先来看最简单的例子,直接通过程序启动服务绑定。(接下来就比较好接受广播+服务的后台监听) OK,现在开始! 阅读详情

相关推荐

android之通过phoneStateListener监听电话状态改变

<br />效果图<br />当我们外部打电话过来的时候,当前的信息会自动的进行改变。<br />-------------------------------------------------------------------------------<br />activity代码:<br />package cn.com.chenzheng_java; import android.app.Activity; import android.os.Bundle; import android

梦中一夜下江南 3万+

PhoneStateListener

概述 PhoneStateListener是给三方app监听通信状态变化的方法,基本使用如下: TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); PhoneStateListener mPhoneStateListener = new Ph

firedancer0089的专栏 1万+

Android基于AudioManager、PhoneStateListener实现设置黑名单功能

本文实例讲述了Android基于AudioManager、PhoneStateListener实现设置黑名单功能。分享给大家供大家参考,具体如下: 手机中一般有设置黑名单的功能。此例通过设置电话黑名单,当黑名单中的电话打来时,手机铃声为变成静音。 程序代码如下: import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.telephony.PhoneStateListener;

电话状态说明

打电话和来电话时处理 媒体音量的问题。现将逻辑说明如下: 1.来电话 监听类 PhoneStateListener 第一种状态:响铃:TelephonyManager.CALL_STATE_RINGING 第二种状态:接起电话: TelephonyManager.CALL_STATE_OFFHOOK 第三种状态:挂电话:TelephonyManager.CALL_STATE_IDLE

善感的人 1万+

从内外参推导IPM变换方程及代码实现(生成AVM环视拼接图)

最近想实现AVM拼接,看了不少博客和论文,不过比较愚钝,一直没能很好理解原理,尤其是怎么在实现时把下文中Z1和Z2消除的,所以严谨的推导了一下对应的公式,如有不对,水平有限,烦请指出~IPM变换(逆透视变换),顾名思义,即将正常透视效应消除的变换,变换结果为鸟瞰图BEV(俯视图);AVM环视拼接一般是将车身前后左右的四个鱼眼相机拼接成环视图(也是俯视),其使用的单应矩阵将四个相机转到同一个俯视坐标系。

mxdsdo09的博客 4169

从DBC到ARXML:用RTA-CAR7实现车载通信协议栈自动化生成全流程

本文详细介绍了如何利用ETAS RTA-CAR7工具链,特别是ISOLAR-A和RTA-BSW ConfGen,实现从车载通信DBC文件到AUTOSAR ARXML系统描述文件,再到基础软件(BSW)协议栈代码的自动化生成全流程。该流程通过深度配置、信号路由、PDU复用及规则定制,大幅提升开发效率与一致性,是应对现代汽车电子复杂网络开发的关键实践。

brandy的博客 220

Android 来去电监听,电话挂断(1)

由于题目很多整理答案的工作量太大,所以仅限于提供知识点,详细的很多问题和参考答案我都整理成了 PDF文件《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!由于题目很多整理答案的工作量太大,所以仅限于提供知识点,详细的很多问题和参考答案我都整理成了 PDF文件[外链图片转存中…(img-daN9PC9G-1714687974365)][外链图片转存中…(img-EQPS3Oox-1714687974365)]

WJud11的博客 1019

告别环境配置噩梦:VSCode + MinGW 下 OpenGL (glad/glm/glfw) 一站式配置指南(含常见错误排查)

本文提供了一份详细的VSCode与MinGW下OpenGL开发环境配置指南,涵盖glad、glm和glfw三大核心库的安装与调试。通过逐步指导解决常见错误,帮助开发者快速搭建高效的OpenGL开发环境,特别适合初学者避免环境配置中的常见陷阱。

weixin_42557803的博客 488

Android 来去电监听,电话挂断,卑微打工人

面试题集可以帮助你查漏补缺,有方向有针对性的学习,为之后进大厂做准备。但是如果你仅仅是看一遍,而不去学习和深究。那么这份面试题对你的帮助会很有限。最终还是要靠资深技术水平说话。网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。建议先制定学习计划,根据学习计划把知识点关联起来,形成一个系统化的知识体系。学习方向很容易规划,但是如果只通过碎片化的学习,对自己的提升是很慢的。

m0_60721823的博客 923

数字逻辑EduCoder课设项目 厨房计时器系统设计 Logisim实现

数字逻辑EduCoder课设项目 厨房计时器系统设计 Logisim实现

weixin_51693702的博客 1万+

新能源汽车电机交流电流测量系统精度全链路分析

通过全链路误差建模与瓶颈分析,结合硬件选型优化与软件算法补偿,可将系统精度从4.2%提升至≤1% FS。本文所述方法论已应用于某800V电驱平台开发,实测电流控制精度达±0.8%,为高性能电机控制奠定基础。为例,深入剖析从互感器采集到CAN报文解析的全链路信号处理流程,揭示各环节误差来源及对最终精度的影响,为研发测试提供理论支撑。:±0.1%(电阻温漂与运放开环增益限制):运放增益带宽积(GBW)不足导致高频衰减。:±0.02%/℃(铁氧体材料温敏特性):±0.5%读数(磁芯饱和效应)

优快云-新能源汽车研发测试专业博主 1492

Android 来去电监听,电话挂断,【微信小程序

极易碰到天花板技术停滞不前!**

2201_75604341的博客 1278

【Bluetooth蓝牙开发】十一、一文秒懂 | 超详细的Bluez交叉编译

1、开篇词 2、蓝牙开发入门 3、蓝牙协议栈总览 4、蓝牙协议栈——物理层 5、蓝牙协议栈——链路层 6、蓝牙协议栈——传输层 7、蓝牙协议栈——L2CAP协议 8、蓝牙协议栈——ATT协议 9、蓝牙协议栈——GATT协议 10、蓝牙通信流程分析 11、蓝牙协议栈——Bluez交叉编译 12、蓝牙调试工具汇总

Donge's Blog 3975
上一篇: Android 广播机制 详解
下一篇: Android 网络性能优化(2)DNS优化
Java364102
博客等级 码龄5年 12粉丝 58原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值