[android] 手机卫士接收打电话广播显示号码归属地

2024-03-17 21:32

本文主要是介绍[android] 手机卫士接收打电话广播显示号码归属地,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用广播接收者接收打电话的意图,显示号码归属地

 

新建一个类OutCallReceiver继承系统的BroadcastReceiver

重写onReceive()方法

调用getResultData()方法,获取到String电话号码,查询数据库获取到归属地

package com.qingguow.mobilesafe.receiver;import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/*** 接收打电话广播* @author taoshihan**/
public class OutcallReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context arg0, Intent arg1) {String phone=getResultData();String address=NumberQueryAddressUtil.queryAddress(phone);System.out.println("111"+address);Toast.makeText(arg0, address, 1).show();}}

 

 

清单文件中定义<receiver>节点,和<intent-filter>节点,设置<action>节点名称,android:name=”android.intent.action.NEW_OUTGOING_CALL”

需要权限,android.permission.PROCESS_OUTGOING_CALLS

 代码注册广播

此时的广播接收者,只要一打电话就会被调用,用户体验不好,我们使用代码注册广播接收者,用设置中心的开启监听电话来电的部分来控制这个广播

 

在服务里面调用registerReceiver()方法,注册广播,参数:BroadcastReceiver对象,IntentFilter对象

获取IntentFilter对象,调用IntentFilter对象的addAction()方法,参数:android.intent.action.NEW_OUTGOING_CALL

 

在服务里面解除注册广播,调用unregisterReceiver()方法,参数:BroadcastReceiver对象

 

package com.qingguow.mobilesafe.service;import com.qingguow.mobilesafe.receiver.OutcallReceiver;
import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;/*** 来电显示* * @author taoshihan* */
public class AddressService extends Service {private TelephonyManager tm;private MyPhoneStateListener phoneStateListener;private OutcallReceiver outcallReceiver;@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}/*** 服务创建*/@Overridepublic void onCreate() {super.onCreate();tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);phoneStateListener = new MyPhoneStateListener();tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);//注册广播outcallReceiver=new OutcallReceiver();IntentFilter filter=new IntentFilter();filter.addAction("android.intent.action.NEW_OUTGOING_CALL");registerReceiver(outcallReceiver, filter);}private class MyPhoneStateListener extends PhoneStateListener {@Overridepublic void onCallStateChanged(int state, String incomingNumber) {super.onCallStateChanged(state, incomingNumber);switch (state) {case TelephonyManager.CALL_STATE_RINGING:String info = NumberQueryAddressUtil.queryAddress(incomingNumber);Toast.makeText(getApplicationContext(), info, 1).show();break;default:break;}}}/*** 服务销毁*/@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();//取消监听
        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);phoneStateListener=null;//解除注册广播
        unregisterReceiver(outcallReceiver);outcallReceiver=null;}
}

 

这篇关于[android] 手机卫士接收打电话广播显示号码归属地的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/820228

相关文章

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局