Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >电话拦截

电话拦截

作者头像
xiangzhihong
发布于 2018-01-29 08:11:36
发布于 2018-01-29 08:11:36
1.6K0
举报
文章被收录于专栏:向治洪向治洪

首先需要 android 源码文件NeighboringCellInfo.aidl和ITelephony.aidl,新建文件夹android.telephony(文件名必须为这个名称),将文件NeighboringCellInfo.aidl拷贝到该文件夹下,在新建另一个文件夹com.android.internal.telephony(不必须名称),将文件ITelephony.aidl放入刷新项目目录,会看到在gen目录下生成相应类代码。

项目目录图:

详细代码如下:

代码语言:html
AI代码解释
复制
package com.internal.telephony;  
 
import java.lang.reflect.Method;  
 
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.media.AudioManager;  
import android.os.IBinder;  
import android.telephony.TelephonyManager;  
import android.util.Log;  
 
import com.android.internal.telephony.ITelephony;  
import com.internal.main.BlockList;  
 
public class TelInternal extends BroadcastReceiver {  
 
    @Override  
    public void onReceive(Context context, Intent intent) {  
 
        AudioManager mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);  
        BlockList b=new BlockList(context);   
 
        if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){//  Log.e("msg", "calling");     
 
        //如果是去电(拨出)    
            String num=getResultData();  
 
            if(num.equals("12345")){  
            setResultData(null); //清除电话  
            break;  
            }  
 
 
        }else{ //由于android没有来点广播所以,去掉拨打电话就是来电状态了  
            // Log.e("msg", "coming");     
 
             String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);     
           //  Log.e("msg", "State: "+ state);     
 
             String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);     
           //  Log.e("msg", "Incomng Number: " + number);     
 
             if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){Log.e("msg", "ring");  
 
                 if(number.equals("12345")){//拦截指定的电话号码     
                     //先静音处理     
                     mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);     
              //       Log.e("msg", "Turn Ringtone Silent");     
 
                     try {     
                        /* //挂断电话   方法一  
                         Method method = Class.forName(  
                                "android.os.ServiceManager").getMethod(  
                                "getService", String.class);  
                            // 获取远程TELEPHONY_SERVICE的IBinder对象的代理  
                            IBinder binder = (IBinder) method.invoke(null,  
                                new Object[] { Context.TELEPHONY_SERVICE });  
                            // 将IBinder对象的代理转换为ITelephony对象  
                            ITelephony telephony = ITelephony.Stub  
                                .asInterface(binder);  
                            // 挂断电话  
                            telephony.endCall();  Log.e("msg", "end"); */   
                        //挂断电话   方法二  
                         ITelephony  iTelephony = getITelephony(context); //获取电话接口  
                          iTelephony.endCall(); // 挂断电话  
                          Log.e("msg", "end");  
                     } catch (Exception e) {     
                         e.printStackTrace();     
                     }     
                     //再恢复正常铃声     
                     mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);     
                     break;  
                 }     
 
             }  
         }  
    }  
    /**  
     * 根据反射获取end()方法2  
     * @param context  
     * @return  
     */  
     private static ITelephony getITelephony(Context context) {  
         ITelephony iTelephony=null;  
            TelephonyManager mTelephonyManager = (TelephonyManager) context  
                    .getSystemService(Context.TELEPHONY_SERVICE);  
            Class<TelephonyManager> c = TelephonyManager.class;  
            Method getITelephonyMethod = null;  
            try {  
 getITelephonyMethod = c.getDeclaredMethod("getITelephony",  
                        (Class[]) null); // 获取声明的方法  
                getITelephonyMethod.setAccessible(true);  
            } catch (SecurityException e) {  
                e.printStackTrace();  
            } catch (NoSuchMethodException e) {  
                e.printStackTrace();  
            }  
 
            try {  
 iTelephony = (ITelephony) getITelephonyMethod.invoke(  
                        mTelephonyManager, (Object[]) null); // 获取实例  
                return iTelephony;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return iTelephony;  
        }  
 
}  

注册广播:

代码语言:html
AI代码解释
复制
<receiver android:name="com.internal.telephony.TelInternal" android:enabled="true"> 
 <intent-filter> 
 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> 
 <action android:name="android.intent.action.PHONE_STATE"/> 
 </intent-filter> 
 </receiver> 

相关权限:

 <uses-permission android:name = "android.permission.READ_PHONE_STATE"/>

为了方便大家我把文件NeighboringCellInfo.aidl和ITelephony.aidl源码复制到这里供大家使用:

文件NeighboringCellInfo.aidl源码:

代码语言:html
AI代码解释
复制
/* //device/java/android/android/content/Intent.aidl  
**  
** Copyright 2007, The Android Open Source Project  
**  
** Licensed under the Apache License, Version 2.0 (the "License");  
** you may not use this file except in compliance with the License.  
** You may obtain a copy of the License at  
**  
**     http://www.apache.org/licenses/LICENSE-2.0  
**  
** Unless required by applicable law or agreed to in writing, software  
** distributed under the License is distributed on an "AS IS" BASIS,  
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
** See the License for the specific language governing permissions and  
** limitations under the License.  
*/  
 
package android.telephony;  
 
parcelable NeighboringCellInfo;  

文件ITelephony.aidl源码:

代码语言:html
AI代码解释
复制
/*  
 * Copyright (C) 2007 The Android Open Source Project  
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0  
 *  
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.  
 */  
 
package com.android.internal.telephony;  
 
import android.os.Bundle;  
import java.util.List;  
import android.telephony.NeighboringCellInfo;  
 
/**  
 * Interface used to interact with the phone.  Mostly this is used by the   
 * TelephonyManager class.  A few places are still using this directly.  
 * Please clean them up if possible and use TelephonyManager insteadl.  
 *  
 * {@hide}  
 */  
interface ITelephony {  
 
    /**  
     * Dial a number. This doesn't place the call. It displays  
     * the Dialer screen.  
     * @param number the number to be dialed. If null, this  
     * would display the Dialer screen with no number pre-filled.  
     */  
    void dial(String number);  
 
    /**  
     * Place a call to the specified number.  
     * @param number the number to be called.  
     */  
    void call(String number);  
 
    /**  
     * If there is currently a call in progress, show the call screen.  
     * The DTMF dialpad may or may not be visible initially, depending on  
     * whether it was up when the user last exited the InCallScreen.  
     *  
     * @return true if the call screen was shown.  
     */  
    boolean showCallScreen();  
 
    /**  
     * Variation of showCallScreen() that also specifies whether the  
     * DTMF dialpad should be initially visible when the InCallScreen  
     * comes up.  
     *  
     * @param showDialpad if true, make the dialpad visible initially,  
     *                    otherwise hide the dialpad initially.  
     * @return true if the call screen was shown.  
     *  
     * @see showCallScreen  
     */  
    boolean showCallScreenWithDialpad(boolean showDialpad);  
 
    /**  
     * End call or go to the Home screen  
     *  
     * @return whether it hung up  
     */  
    boolean endCall();  
 
    /**  
     * Answer the currently-ringing call.  
     *  
     * If there's already a current active call, that call will be  
     * automatically put on hold.  If both lines are currently in use, the  
     * current active call will be ended.  
     *  
     * TODO: provide a flag to let the caller specify what policy to use  
     * if both lines are in use.  (The current behavior is hardwired to  
     * "answer incoming, end ongoing", which is how the CALL button  
     * is specced to behave.)  
     *  
     * TODO: this should be a oneway call (especially since it's called  
     * directly from the key queue thread).  
     */  
    void answerRingingCall();  
 
    /**  
     * Silence the ringer if an incoming call is currently ringing.  
     * (If vibrating, stop the vibrator also.)  
     *  
     * It's safe to call this if the ringer has already been silenced, or  
     * even if there's no incoming call.  (If so, this method will do nothing.)  
     *  
     * TODO: this should be a oneway call too (see above).  
     *       (Actually *all* the methods here that return void can  
     *       probably be oneway.)  
     */  
    void silenceRinger();  
 
    /**  
     * Check if we are in either an active or holding call  
     * @return true if the phone state is OFFHOOK.  
     */  
    boolean isOffhook();  
 
    /**  
     * Check if an incoming phone call is ringing or call waiting.  
     * @return true if the phone state is RINGING.  
     */  
    boolean isRinging();  
 
    /**  
     * Check if the phone is idle.  
     * @return true if the phone state is IDLE.  
     */  
    boolean isIdle();  
 
    /**  
     * Check to see if the radio is on or not.  
     * @return returns true if the radio is on.  
     */  
    boolean isRadioOn();  
 
    /**  
     * Check if the SIM pin lock is enabled.  
     * @return true if the SIM pin lock is enabled.  
     */  
    boolean isSimPinEnabled();  
 
    /**  
     * Cancels the missed calls notification.  
     */  
    void cancelMissedCallsNotification();   
 
    /**  
     * Supply a pin to unlock the SIM.  Blocks until a result is determined.  
     * @param pin The pin to check.  
     * @return whether the operation was a success.  
     */  
    boolean supplyPin(String pin);  
 
    /**  
     * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which are initiated  
     * without SEND (so <code>dial</code> is not appropriate).  
     *   
     * @param dialString the MMI command to be executed.  
     * @return true if MMI command is executed.  
     */  
    boolean handlePinMmi(String dialString);  
 
    /**  
     * Toggles the radio on or off.  
     */  
    void toggleRadioOnOff();  
 
    /**  
     * Set the radio to on or off  
     */  
    boolean setRadio(boolean turnOn);  
 
    /**  
     * Request to update location information in service state  
     */  
    void updateServiceLocation();  
 
    /**  
     * Enable location update notifications.  
     */  
    void enableLocationUpdates();  
 
    /**  
     * Disable location update notifications.  
     */  
    void disableLocationUpdates();  
 
    /**  
     * Enable a specific APN type.  
     */  
    int enableApnType(String type);  
 
    /**  
     * Disable a specific APN type.  
     */  
    int disableApnType(String type);  
 
    /**  
     * Allow mobile data connections.  
     */  
    boolean enableDataConnectivity();  
 
    /**  
     * Disallow mobile data connections.  
     */  
    boolean disableDataConnectivity();  
 
    /**  
     * Report whether data connectivity is possible.  
     */  
    boolean isDataConnectivityPossible();  
 
    Bundle getCellLocation();  
 
    /**  
     * Returns the neighboring cell information of the device.  
     */  
    List<NeighboringCellInfo> getNeighboringCellInfo();  
 
     int getCallState();  
     int getDataActivity();  
     int getDataState();  
}  

最后千万别忘了添加权限呀!

代码语言:java
AI代码解释
复制
<uses-permission android:name="android.permission.CALL_PHONE"/>    
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>  

ok,希望对大家有帮助!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-04-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
服务发现之consul的介绍、部署和使用
微服务的框架体系中,服务发现是不能不提的一个模块。我相信了解或者熟悉微服务的童鞋应该都知道它的重要性。这里我只是简单的提一下,毕竟这不是我们的重点。我们看下面的一幅图片:
sunsky
2020/08/20
1.8K0
grafana+prometheus+Consul自动服务发现监控平台
一、安装prometheus 安装包下载地址:https://prometheus.io/download/
@凌晨
2020/12/07
2.2K0
通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布
前段时间顺利地把整个服务集群和中间件全部从UCloud迁移到阿里云,笔者担任了架构和半个运维的角色。这里详细记录一下通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布的核心知识点和操作步骤,整个体系已经在生产环境中平稳运行。编写本文使用的虚拟机系统为CentOS7.x,虚拟机的内网IP为192.168.56.200。
Throwable
2020/06/20
1.7K0
通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布
微服务 - 搭建Consul集群服务,Consul配置中心
传统配置文件的弊端 静态化配置,例如env文件 配置文件无法区分环境 配置文件过于分散 历史版本无法查看 配置中心如何解决的呢?配置中心的思路是把项目中的配置参数全部放在一个集中的地方来管理,并提供一
stark张宇
2023/04/22
8620
学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片
那么,我们对 Consul 的理解,就是服务网格、服务发现,官网文档说的这两个特征,到底是啥意思?跨什么云?
痴者工良
2021/04/26
1.1K0
Consul 集群2
此时已经分别在104和103上启动了两个代理a1和a2,a1准备用来作server ,a2用来作client,但它们彼此还互不认识,都是自己的单节点集群中的唯一节点,可以通过 consul members 来进行查看
franket
2021/12/01
4140
Spring Cloud(二)Consul 服务治理实现
Spring Cloud Consul 项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具有分布式、高可用、高扩展性。 Consul 简介 Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式” ,内置了服务注册与发现框 架、具有以下性质: 分布一致性协议实现、 健康检查、 Key/Value存储、 多数据中心方案, 不再需要依赖其他工具(比如ZooKeeper等)。 使用起来
程序员鹏磊
2018/02/09
1.6K0
Spring Cloud(二)Consul 服务治理实现
Consul 基础7
Consul 会加载配置目录中的所有配置文件,配置文件是以 .json 结尾的,并且以字典顺序加载
franket
2021/12/01
2230
Nginx + UpSync + Consul 实现 Dynamic Upstream
Nginx 是一款开源、高性能、高可靠的 Web 和反向代理服务器,性能是 Nginx 最重要的考量,其占用内存少、并发能力强。Nginx 最常见的使用场景就是反向代理,Nginx 接收客户端的请求并通过相应的负载均衡算法将流量转发给后端的多台应用服务器。
Se7en258
2021/07/23
1.8K0
Nginx + UpSync + Consul 实现 Dynamic Upstream
Prometheus+Consul 自助服务发现(六)
Consul 是基于 GO 语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能。Consul 提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能。通过 Prometheus 实现监控,当新增一个 Target 时,需要变更服务器上的配置文件,即使使用 file_sd_configs 配置,也需要登录服务器修改对应 Json 文件,会非常麻烦。不过 Prometheus 官方支持多种自动服务发现的类型,其中就支持 Consul。
Kevin song
2023/02/22
1.8K0
Prometheus+Consul 自助服务发现(六)
Kubernetes中Consul重启自动加入集群实践
近期频繁的容器母机调整导致我们的业务需要多次重启,不得不寻找一种自动重建Consul集群的方式。在网上搜索和学习一番后发现,基本没有针对Kubernetes容器环境的自动重建方案。
sherlock99
2018/08/02
1.8K0
Consul 集群3
加入成功后server节点上就会产生如下日志 ... ... 2016/03/18 22:00:36 [INFO] agent.rpc: Accepted client: 127.0.0.1:44743 2016/03/18 22:00:36 [INFO] agent: (LAN) joining: [192.168.100.103] 2016/03/18 22:00:36 [INFO] agent: (LAN) joined: 0 Err: dial tcp 192.168.100
franket
2021/12/01
3240
安装Consul集群
TIPS •本文基于Consul 1.5.3,理论适用于Consul 1.6及更低版本。•安装单机版Consul详见:《安装单机版Consul》
用户1516716
2019/12/23
1.7K0
Consul 入门教程
微服务的框架体系中,服务发现是不能不提的一个模块。我相信了解或者熟悉微服务的童鞋应该都知道它的重要性。这里我只是简单的提一下,毕竟这不是我们的重点。我们看下面的一幅图片:
菲宇
2019/06/12
48.6K1
Consul 入门教程
Consul 基础3
每一个数据中心必须有至少一个服务节点,3到5个服务节点最好,非常不建议只运行一个服务节点,因为在节点失效的情况下数据有极大的丢失风险
franket
2021/12/01
2900
Consul1.7 多数据中心 新Hashicorp学习指南
在微服务化的趋势下,为了最大限度增加扩容缩容的灵活性,名字服务和服务发现等方式就越来越受到青睐了。目前,主流的服务发现组件有:consul、etcd、zookeeper,其中的区别这里就不展开说明了,可以在官网查看这些服务之间的区别。
公众号: 云原生生态圈
2021/11/15
1.4K0
Consul1.7 多数据中心 新Hashicorp学习指南
Prometheus 通过 consul 实现自动服务发现
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
哎_小羊
2019/11/12
13K0
Prometheus监控神器-服务发现篇(三)
上图是官网提供的一个事例系统图,图中的Server是consul服务端高可用集群,Client是consul客户端。consul客户端不保存数据,客户端将接收到的请求转发给响应的Server端。Server之间通过局域网或广域网通信实现数据一致性。每个Server或Client都是一个consul agent。
Kubernetes技术栈
2020/09/07
1.5K0
微服务系统consul初探
简单来说,consul就是一个用于微服务治理的系统,什么是微服务,这个大家自行了解。consul的主要功能有如下几个:
tunsuy
2022/10/27
9090
Nginx、Consul、Upsync实现动态负载均衡
Nginx实现动态负载均衡,首先需要一个服务发现集群,通过集群中注册的信息动态更新nginx的配置,实现动态负载均衡。因此首先准备一个Consul集群
公众号: 云原生生态圈
2021/03/16
1.1K0
Nginx、Consul、Upsync实现动态负载均衡
相关推荐
服务发现之consul的介绍、部署和使用
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档