Android监听系统来电并弹出提示窗口
程序员文章站
2023-12-11 15:13:52
1.问题
项目中有自己企业的通讯录,但是在应用中拨打公司通讯录的联系人,由于手机通讯录中没有相应的信息,只显示一串电话号
2 .目的
监听系统来电,获取到电话号码,通...
1.问题
项目中有自己企业的通讯录,但是在应用中拨打公司通讯录的联系人,由于手机通讯录中没有相应的信息,只显示一串电话号
2 .目的
监听系统来电,获取到电话号码,通过调用接口,查询出来相应电话号码的详细信息,并弹出系统悬浮框,给用户提示。
3.实现
首先 注册广播监听系统来电。监听系统来电需要、注册相应的权限
代码地址:
<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; } } }; };
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。