最近刚接到一个需求,为BOSS做一个来电显示功能,查找号码库显示姓名角色。

一、查找来电监听方法

PhoneStateListener监听器类,用于监视设备上特定电话状态的变化,包括服务状态、信号强度、消息等待指示器(语音邮件)等。

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;public class MyPhoneStateListener extends PhoneStateListener {private static final String TAG = "MyPhoneStateListener";protected CallListener listener;/*** 返回电话状态** CALL_STATE_IDLE 无任何状态时* CALL_STATE_OFFHOOK 接起电话时* CALL_STATE_RINGING 电话响铃时*/@Overridepublic void onCallStateChanged(int state, String incomingNumber) {switch (state) {case TelephonyManager.CALL_STATE_IDLE:Log.d(TAG ,"电话挂断...");listener.onCallIdle();break;case TelephonyManager.CALL_STATE_OFFHOOK:Log.d(TAG ,"正在通话...");listener.onCallOffHook();break;case TelephonyManager.CALL_STATE_RINGING:Log.d(TAG ,"电话响铃...");listener.onCallRinging();break;}super.onCallStateChanged(state, incomingNumber);}//回调public void setCallListener(CallListener callListener) {this.listener = callListener;}//回调接口public interface CallListener {void onCallIdle();void onCallOffHook();void onCallRinging();}
}

TelephonyManager 提供对设备上电话服务的信息的访问。应用程序可以使用该类中的方法来确定电话服务和状态,以及访问某些类型的订阅者信息。应用程序还可以注册侦听器来接收电话状态更改的通知。

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import com.flymbp.callmonitor.MyPhoneStateListener;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);telephony();}private void telephony() {//获得相应的系统服务TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);if(tm != null) {try {MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();myPhoneStateListener.setCallListener(new MyPhoneStateListener.CallListener() {@Overridepublic void onCallIdle() {}@Overridepublic void onCallOffHook() {}@Overridepublic void onCallRinging() {//走接口查询号码信息}});// 注册来电监听tm.listen(myPhoneStateListener, MyPhoneStateListener.LISTEN_CALL_STATE);} catch(Exception e) {// 异常捕捉}}}
}

此时此刻我们就可以监听到来电状态,但是incomingNumber没值,测试设备是华为mate20 pro Android 9.0
需要READ_CALL_LOG权限

     <!--读取电话的状态信息的权限--><uses-permission android:name="android.permission.READ_PHONE_STATE" /><!--读取通话记录的权限--><uses-permission android:name="android.permission.READ_CALL_LOG" />

Android 9 来电监听incomingNumber为空

拿到incomingNumber 我们就可以请求后台接口来获取号码信息,或者有本地号码数据库进行查找。

二、来电弹窗提示信息

来电号码信息有了,我们要在来电界面进行提示,既然不能对来电界面进行篡改,那我们就加个弹窗提示吧。
想到两种方式:
1、Toast提示,实现简单,但是显示时间短,不是主动触发,会错过看到提示,不采用。
2、悬浮窗提示,既然要在自身应用以外的界面上显示弹窗,那必然要使用悬浮窗。

我们将使用悬浮窗进行来电提示。为了让悬浮窗与Activity脱离,使其在应用处于后台时悬浮窗仍然可以正常运行,这里使用Service来启动悬浮窗。

来电时显示悬浮窗,点击悬浮窗可移除,拖拽悬浮窗可移动,接通或挂断移除悬浮窗,注意悬浮窗不要来一个电话显示一个弹窗。

public class FloatingButtonService extends Service {public static boolean isStarted = false;private WindowManager windowManager;private WindowManager.LayoutParams layoutParams;private Button button;private String content;@Overridepublic void onCreate() {super.onCreate();isStarted = true;windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);layoutParams = new WindowManager.LayoutParams();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;} else {layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;}layoutParams.format = PixelFormat.RGBA_8888;layoutParams.gravity = Gravity.LEFT | Gravity.TOP;layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;layoutParams.width = 500;layoutParams.height = 100;layoutParams.x = 300;layoutParams.y = 300;}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {content = intent.getStringExtra("content");int state = intent.getIntExtra("state", 0);switch (state) {case TelephonyManager.CALL_STATE_IDLE:removeFloating();break;case TelephonyManager.CALL_STATE_OFFHOOK:removeFloating();break;case TelephonyManager.CALL_STATE_RINGING:showFloatingWindow();break;}return super.onStartCommand(intent, flags, startId);}private void removeFloating() {if(button != null){windowManager.removeView(button);}}private void showFloatingWindow() {if(button != null){windowManager.removeView(button);}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (Settings.canDrawOverlays(this)) {button = new Button(getApplicationContext());button.setText(content);button.setTextColor(Color.BLACK);button.setBackgroundColor(Color.WHITE);button.setOnTouchListener(new FloatingOnTouchListener());windowManager.addView(button, layoutParams);}} else {button = new Button(getApplicationContext());button.setText(content);button.setTextColor(Color.BLACK);button.setBackgroundColor(Color.WHITE);button.setOnTouchListener(new FloatingOnTouchListener());windowManager.addView(button, layoutParams);}}private class FloatingOnTouchListener implements View.OnTouchListener {private int x;private int y;private int clickx;private int clicky;@Overridepublic boolean onTouch(View view, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:x = (int) event.getRawX();y = (int) event.getRawY();clickx = x;clicky = y;break;case MotionEvent.ACTION_MOVE:int nowX = (int) event.getRawX();int nowY = (int) event.getRawY();int movedX = nowX - x;int movedY = nowY - y;x = nowX;y = nowY;layoutParams.x = layoutParams.x + movedX;layoutParams.y = layoutParams.y + movedY;windowManager.updateViewLayout(view, layoutParams);break;case MotionEvent.ACTION_UP:if (clickx == x && clicky == y)windowManager.removeView(button);break;default:break;}return false;}}
}

如何触发悬浮窗呢?

BroadcastReceiver使用广播来接收来电状态

在MainActivity.onCreate中注册广播

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String data = intent.getStringExtra("data");showFloating(data);}};IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");registerReceiver(mReceiver, intentFilter);
}public void showFloating(String mobile, int state) {Intent regIntent = new Intent(MainActivity.this, FloatingButtonService.class);regIntent.putExtra("content", mobile);regIntent.putExtra("state",state);startService(regIntent);
}

三、后台监听

来电监听我们不能总让应用在前台运行吧,这时需要后台运行进行监听。
需要把在MainActivity.telephony的方法写到服务里。

public class MyPhoneStateListenService extends Service {private static final String tag = "MyPhoneStateListenService";public static final String ACTION_REGISTER_LISTENER = "action_register_listener";// 电话管理者对象private TelephonyManager mTelephonyManager;// 电话状态监听者private MyPhoneStateListener myPhoneStateListener;@Overridepublic void onCreate() {mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);myPhoneStateListener = new MyPhoneStateListener(this);mTelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {// 取消来电的电话状态监听服务if (mTelephonyManager != null && myPhoneStateListener != null) {mTelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);}super.onDestroy();}
}

在MainActivity.onCreate中开启服务

private void registerPhoneStateListener() {Intent intent = new Intent(this,  MyPhoneStateListenService.class);intent.setAction(MyPhoneStateListenService.ACTION_REGISTER_LISTENER);startService(intent);
}

四、进程保活

那么问题又来了,在后台服务很容易被杀,那我们就得考虑加入保活方案。
保活方案有很多,采用合适的方案,这里就不细说了。
常见的一些保活方案:
1、一像素保活
2、双进程守护
3、后台播放无声音乐
。。。

Android 来电监听相关推荐

  1. android o来电广播启动服务,Android来电监听

    本文实现来电自动接听,自动录音,自动回拨功能. public class MainActivity extends Activity { @Override protected void onCrea ...

  2. 【Android】来电监听

    Android 来电监听 1 依据来电号码查询本地联系人 1.1 权限 1.2 实现 2 来电监听方法(有两种) 2.1 系统广播监听 2.1.1 权限 2.1.2 注册电话状态系统广播接收器 2.2 ...

  3. [已验证]双卡手机下两种来电监听方法的一些问题。

    为什么80%的码农都做不了架构师?>>>    首先,网上学习到的来电监听有两种方法,但在双卡手机上都不太正常工作. 经过用户的反馈,基本上都能监听到大部分主流双卡手机的主副卡来电了 ...

  4. Android监听左右滑删除通知,Android 滑动监听RecyclerView线性流+左右划删除+上下移动...

    废话不多说了,直接给大家贴代码了.具体代码如下所示: xmlns:tools="http://schemas.android.com/tools" android:layout_w ...

  5. Android 监听 Android中监听系统网络连接打开或者关闭的实现代码

    本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.c ...

  6. Android RecyclerView 监听滑动

    今天,简单讲讲Android 如何监听滑动. 不废话了,主要是需要做一个功能,实现RecyclerView滑动时,让一个标题栏固定显示在顶部. 基本知识: 列表的滚动一般分为两种: 手指按下 -> ...

  7. android 如何监听应用前后台切换

    今天,简单讲讲android如何判断应用切换到后台和应用切换到前台. 这个其实很简单,之前需要做一个功能,当app由后台进入前台时需要完成一些逻辑操作,所以在网上查找如何判断app由后台进入前台,最终 ...

  8. java edittext 输入监听_Android应用开发之Android EditText 监听用户输入完成的实例

    本文将带你了解Android应用开发Android EditText 监听用户输入完成的实例,希望本文对大家学Android有所帮助. 我们都知道, Android   EditText输入框,并没有 ...

  9. Android查看web日志,详解Android WebView监听console错误信息

    根据需求,我们要拿到h5的错误信息,并将error信息进行上报.查询了下Android WebView的API发现了WebChromeClient这个方法可以满足要求: @Override publi ...

最新文章

  1. Python 复数属性及操作介绍
  2. 3DSSD:基于点云的single-stage物体检测模型 | CVPR2020
  3. 对于容器环境来说 全栈监控究竟意味着什么?
  4. 分布式一致性协议paxos
  5. TOMCAT安全配置
  6. scala tail recursive优化,复用函数栈
  7. 用计算机模拟物理学,计算机模拟技术在医学物理学教学中的应用研究.pdf
  8. Nginx反向代理负载均衡时,验证码不正确
  9. wso2 esb_使用WSO2 ESB构建制造服务总线(MSB)
  10. 进程控制块PCB(进程描述符)
  11. 电子商务专业需要考计算机证吗,电子商务必考的证有哪些
  12. 近期计算机视觉算法竞赛汇总—总奖池超300万人民币
  13. 通过异常处理错误-2
  14. Java7/8集合框架——基本知识点
  15. 深析在线教育下半场机遇逻辑 掌门教育论道蓝鲸教育大会
  16. java购物系统需求分析_java网上购物系统需求分析报告.doc
  17. Android对话框控件读写,Android 对话框控件
  18. 主析取范式和主合取范式
  19. 新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
  20. android 一种键盘不能调起的解决方法

热门文章

  1. 在IIS7、IIS7.5中应用程序池最优配置方案
  2. 牛客网——歪脖子树下的灯
  3. Praat脚本-001 | 音频文件重新采样
  4. 伯克利的电气工程和计算机科学专业,加州大学伯克利分校
  5. 【Linux】常用命令汇总
  6. 计算机应用领域的实践,计算机技术在通信技术领域的应用实践探微
  7. 字符串乘以数字python_将字符串乘以数字!
  8. 几个链接搞定你想要的HEX颜色和RBG
  9. 叙事的变迁:从《街》到《428》
  10. 在网页中插入一个透明背景的PNG图片