Android中监听未接来电的2种方法
作者:junjie 时间:2023-11-02 10:44:48
这里主要是总结一下如何监听有未接来电的问题
1.1 使用广播 * BrocastReceiver
实现思路 :
静态注册监听android.intent.action.PHONE_STATE 的广播 * 当手机的状态改变后将会触发 onReceive.
手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音).
也就是说当你没有任何电话是,状态是 IDLE ,当接到电话时是 OFFHOOK ,电话结束后返回 IDLE 状态。
记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver android:name="com.example.phonestatedemo.receiver.PhoneStateReceiver">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneStateReceiver extends BroadcastReceiver {
private static int lastCallState = TelephonyManager.CALL_STATE_IDLE;
@Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
Log.d("PhoneStateReceiver", action );
TelephonyManager telephonyManager = (TelephonyManager) arg0
.getSystemService(Context.TELEPHONY_SERVICE);
int currentCallState = telephonyManager.getCallState();
Log.d("PhoneStateReceiver", "currentCallState=" + currentCallState );
if (currentCallState == TelephonyManager.CALL_STATE_IDLE) {// 空闲
//TODO
} else if (currentCallState == TelephonyManager.CALL_STATE_RINGING) {// 响铃
//TODO
} else if (currentCallState == TelephonyManager.CALL_STATE_OFFHOOK) {// 接听
//TODO
}
if(lastCallState == TelephonyManager.CALL_STATE_RINGING &&
currentCallState == TelephonyManager.CALL_STATE_IDLE){
Toast.makeText(arg0, "有未接来电", 1).show();
}
lastCallState = currentCallState;
}
}
1.2 使用 PhoneStateListener
实现思路 :
继承PhoneStateListener后,当手机的状态改变后将会触发onCallStateChanged.手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音).
也就是说当你没有任何电话是,状态是 IDLE ,当接到电话时是 OFFHOOK ,电话结束后返回 IDLE 状态。
记录上一次的手机状态,如果的手机现在的空闲,上次的状态响铃中的话,就可以判断是未接来电.
不足:现在的处理不能判断出是用户是否主动不接电话.
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CallStateListener(this),
PhoneStateListener.LISTEN_CALL_STATE);
package com.example.phonestatedemo.listener;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class CallStateListener extends PhoneStateListener {
private static int lastetState = TelephonyManager.CALL_STATE_IDLE; // 最后的状态
private Context context;
public CallStateListener(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
Log.d("CallStateListener", "onCallStateChanged state=" + state );
// 如果当前状态为空闲,上次状态为响铃中的话,则破觚为认为是未接来电
if (lastetState == TelephonyManager.CALL_STATE_RINGING
&& state == TelephonyManager.CALL_STATE_IDLE) {
//TODO
Toast.makeText(this.context, "CallStateListener 有未接来电", 1).show();
}
lastetState = state;
}
}
标签:Android,监听,未接来电
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot注册bean的三种方法
2023-11-22 21:57:12
Java实现Dijkstra输出最短路径的实例
2023-09-01 17:44:02
使用OkHttp包在Android中进行HTTP头处理的教程
2023-06-16 23:55:37
java 过滤器filter防sql注入的实现代码
2023-08-30 09:56:40
基于springboot 长轮询的实现操作
2022-02-06 09:46:22
Android仿一点资讯收藏Toast动画效果
2022-01-15 18:42:33
![](https://img.aspxhome.com/file/2023/2/106832_0s.png)
深入浅出MappedByteBuffer(推荐)
2023-11-14 19:59:43
Android 虚拟按键适配动态调整布局的方法
2022-03-03 07:20:24
![](https://img.aspxhome.com/file/2023/5/90135_0s.jpg)
C#使用读写锁解决多线程并发问题
2022-02-28 20:36:09
![](https://img.aspxhome.com/file/2023/0/119460_0s.png)
Android ViewPager2 使用及自定义指示器视图实现
2022-06-16 09:35:26
使用C#发送Http请求实现模拟登陆实例
2023-06-22 22:25:07
![](https://img.aspxhome.com/file/2023/7/67047_0s.png)
打造酷炫的AndroidStudio插件
2021-07-27 03:06:42
![](https://img.aspxhome.com/file/2023/1/138471_0s.gif)
Java C++实现相同MD5加密算法的方式
2023-10-12 12:26:02
![](https://img.aspxhome.com/file/2023/5/58685_0s.jpg)
Android自定义view实现圆形与半圆形菜单
2023-10-18 19:08:53
![](https://img.aspxhome.com/file/2023/8/115988_0s.gif)
自己动手写的mybatis分页插件(极其简单好用)
2023-11-01 18:12:09
详解Java中字符流与字节流的区别
2023-01-14 00:19:58
浅谈Android中视图动画的属性与使用
2023-04-15 22:41:12
详解Asp.Net MVC的Bundle捆绑
2021-09-10 05:21:38
![](https://img.aspxhome.com/file/2023/2/96322_0s.png)
SpringBoot使用AOP与注解实现请求参数自动填充流程详解
2022-08-18 17:30:36
Java使用组合模式实现表示公司组织结构功能示例
2022-07-31 08:03:37
![](https://img.aspxhome.com/file/2023/4/75404_0s.png)