Android监听系统来电并弹出提示窗口
作者:苏打水解渴 发布时间:2021-10-26 05:32:46
标签:Android,监听,来电
1.问题
项目中有自己企业的通讯录,但是在应用中拨打公司通讯录的联系人,由于手机通讯录中没有相应的信息,只显示一串电话号
2 .目的
监听系统来电,获取到电话号码,通过调用接口,查询出来相应电话号码的详细信息,并弹出系统悬浮框,给用户提示。
3.实现
首先 注册广播监听系统来电。监听系统来电需要、注册相应的权限
代码地址:https://github.com/sdsjk/phone_alert.git
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
自定义广播去监听系统来电
public class PhoneReceiver extends BroadcastReceiver {
private Context mcontext;
@Override
public void onReceive(Context context, Intent intent){
mcontext=context;
System.out.println("action"+intent.getAction());
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
//如果是去电(拨出)
Log.e("TAG","拨出");
}else{
Log.e("TAG","来电");
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
//设置一个 *
}
}
private PhoneStateListener listener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, final String incomingNumber) {
// TODO Auto-generated method stub
//state 当前状态 incomingNumber,貌似没有去电的API
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
Log.e("TAG","挂断");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("TAG","接听");
break;
case TelephonyManager.CALL_STATE_RINGING:
//输出来电号码
Log.e("TAG","响铃:来电号码"+incomingNumber);
Log.e("TAG","响铃:======"+Thread.currentThread().getName());
break;
}
}
};
};
需要静态注册广播
<receiver android:name="com.cloud.adapter.myview.PhoneReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
其次在注册完,广播之后我们需要在监听到系统的来电之后,后获取到电话号之后去请求接口,获取数据。并弹出系统悬浮框。
注意:在弹出系统悬浮框的时候需要注册权限,并且检查应用的允许弹出悬浮框权限是否开启。
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
在监听中的 TelephonyManager.CALL_STATE_RINGING中操作
inflate= LayoutInflater.from(mcontext);
wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity= Gravity.CENTER;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 600;
params.format = PixelFormat.RGBA_8888;
phoneView=inflate.inflate(R.layout.phone_alert,null);
wm.addView(phoneView, params);
自定义一个布局文件,作为要添加的View,布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<com.cloud.adapter.myview.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="200dp"
android:orientation="vertical"
android:layout_gravity="center"
android:id="@+id/rootview"
>
<LinearLayout
android:background="@drawable/top_background"
android:layout_width="300dp"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="陆XX"
android:textSize="26sp"
/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="系统运行科科长"
/>
</LinearLayout>
<LinearLayout
android:background="@drawable/bottom_background"
android:layout_width="300dp"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<TextView
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公司本部-信息中心-系统运营科"
android:textSize="20sp"
/>
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#fff"
android:text="XXX有限公司"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</com.cloud.adapter.myview.MyLinearLayout>
使用到两个背景shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
/>
<solid android:color="@color/colorPrimary"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
/>
<solid android:color="#f44"/>
</shape>
广播中完整代码
package com.cloud.adapter.myview;
import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Looper;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
/**
* Created by zhang on 2017/10/10.
*/
public class PhoneReceiver extends BroadcastReceiver {
private Context mcontext;
private WindowManager wm;
@Override
public void onReceive(Context context, Intent intent){
mcontext=context;
System.out.println("action"+intent.getAction());
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
//如果是去电(拨出)
Log.e("TAG","拨出");
}else{
//查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电
Log.e("TAG","来电");
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
//设置一个 *
}
}
private TextView tv;
private LayoutInflater inflate;
private View phoneView;
private PhoneStateListener listener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, final String incomingNumber) {
// TODO Auto-generated method stub
//state 当前状态 incomingNumber,貌似没有去电的API
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
Log.e("TAG","挂断");
wm.removeView(tv);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("TAG","接听");
wm.removeView(tv);
break;
case TelephonyManager.CALL_STATE_RINGING:
inflate= LayoutInflater.from(mcontext);
wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity= Gravity.CENTER;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 600;
params.format = PixelFormat.RGBA_8888;
phoneView=inflate.inflate(R.layout.phone_alert,null);
wm.addView(phoneView, params);
Log.e("TAG","响铃:来电号码"+incomingNumber);
Log.e("TAG","响铃:======"+Thread.currentThread().getName());
//输出来电号码
break;
}
}
};
};
效果图
来源:http://blog.csdn.net/baidu_31956557/article/details/78206478


猜你喜欢
- 前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图
- 在编程中经常需要使用到表格(报表)的处理主要以Excel表格为主。下面给出用java写入数据到excel表格方法:1.添加jar文件java
- 拆分字符串:这个可以使用两次分割,第一次使用 | 分割,放到arr数组里,然后使用循环对arr[i]进行使用:分割public static
- 什么是ServletContext?根据字面意思即Servlet上下文服务器会为每一个工程创建一个对象,这个对象就是ServletConte
- 首先给出一段代码:public class AslistMethod { public static void main(String[]
- 本文实例为大家分享了java根据网络地址保存图片的具体代码,供大家参考,具体内容如下import java.io.BufferedInput
- 目录一、新建简单窗口二、编写窗口中的按键三、简单的按键运行1.流布局管理器:2.静态文本框:四、窗口画图五、窗口鼠标响应六、总结好了,sto
- 本文实例为大家分享了Android自定义View画圆的具体代码,供大家参考,具体内容如下引入布局<?xml version="
- 多环境开发我们平常都是在自己的开发环境进行开发,当开发完成后,需要把开发的功能部署到测试环境供测试人员进行测试使用,等测试人员测试通过后,我
- 1、添加依赖<dependency> <groupId>org.springframewo
- 前言说实话,我在 * 工作的时候,就见过Gradle。但是当时我一直不知道这是什么东西。而且 * 工具组的工程师还将其和Android Stud
- 一、自定义菜单的说明和按钮类型1、菜单说明1)自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。2)一级菜单最多4个汉字,二
- namespace ConsoleTest{ class Program  
- 一、前言我们在日常学习中,对一个java代码有问题,不知道jvm内部怎么进行解析的时候;有个伟大壮举就是反编译,这样就可以看到jvm内部怎么
- 本文实例为大家分享了Javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下基本规则棋子马设计“日”的移动方式兵设计只能向前直走,每
- Android中oncreate中获得控件高度或宽度的实现方法onCreate函数只是提供了数据初始化的机会,此时还没有正式绘制图形。在图形
- Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态。Memento模式相
- Android 7.0行为变更 FileUriExposedException解决方法当我们开发关于【在应用间共享文件】相关功能的时候,在A
- 背景:本人不是Java开发人员,经过四年多的历练,可以说是一枚BI攻城师了吧,最近粗糙的写了一个Portal来集成cognos报表,下面就入
- 1.启动项目的时候报错1.Error starting ApplicationContext. To display the auto-co