Loading [MathJax]/jax/output/CommonHTML/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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
android电话拦截
其实大家可以下载 xxx卫士看下,它设置来电拒接模式后,都是会启动设置MMI指令的界面。然后再去“设置->通话设置->来电转接”,看看 “占线时转接” 设置好的电话号码,就可以知道空号/已关机/已停机对应的电话号码是什么了。  1、修改一下BLOCKED_NUMBER这个变量值,把它设置为你要测试拦截的电话号码。  2、全部功能是在一个Activity里实现的,所以大家要先运行这个Activity,然后点击“设置呼叫转移”,设置好呼叫转移后,不要关闭这个Activity,关了就拦截不了电话了。有心的朋
xiangzhihong
2018/01/29
1.8K0
Android来电监听和去电监听
用户1155943
2018/01/04
5.1K0
Android 电话自己主动接听和挂断具体解释「建议收藏」
详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件(构建后在gen下有ITelephony.java文件,这是aidl生成的接口),文件内容例如以下: package com.android.internal.telephony; interface ITelephony{ boolean endCall(); void answerRingingCall(); } (2)在须要的类中加入例如以下方法,代码例如以下(通过反射获取电话接口的实例)
全栈程序员站长
2022/07/08
1.2K0
【详解】Android自动挂断电话的实现
在开发Android应用时,有时会遇到需要实现自动挂断电话的需求。例如,在某些特定情况下(如用户正在使用某个重要功能时),不希望电话打断用户的操作。本文将介绍如何在Android中实现自动挂断电话的功能。
大盘鸡拌面
2025/04/21
2060
跨进程访问(AIDL服务)
我们都知道Service的主要的作用是后台运行和跨进程访问。 关于Service后台运行请查看鄙人的另外一篇文章Service基础
小小工匠
2021/08/16
9050
Android监听来电和去电
要监听android打电话和接电话,只需下面2步骤 1.第一步,写一个Receiver继承自BroadcastReceiver 1 import android.app.Service; 2 import android.content.BroadcastReceiver; 3 import android.content.Context; 4 import android.content.Intent; 5 import android.telephony.TelephonyManager;
欢醉
2018/01/22
1.2K0
android的电话监听
android的电话监听 新建一个项目,结构图如下: image PhoneService: package com.demo.tingdianhua; import android.app.Ser
Dream城堡
2019/01/28
1.2K1
[android] 手机卫士接收打电话广播显示号码归属地
新建一个类OutCallReceiver继承系统的BroadcastReceiver
唯一Chat
2019/09/10
9580
全局事件-广播(Broadcast)
广播是Android SDK的四大组件中唯一需要别动接收数据的组件。也就是说对于Activity、ContentProvider和Service都可以主动调用,并获取返回数据。而负责接收Broadcast数据的接收器却永远不知道什么时候可以接收到广播。从这种表现形式上看,很像面向对象中的事件(Event),对于事件(onClick、onKeydown)来说,从来不会预知用户什么时候触发他们,只能默默的等待不可预知的事件发生。因此,广播也可以被成为全局事件。
小小工匠
2021/08/16
1.3K0
安卓基础干货(八):安卓进程的学习
接口(interface): 对外开放暴露的功能,但是不会暴露功能实现的细节; 让中间人实现服务接口的目的:只对外暴露接口里面业务逻辑方法,隐藏中间人里面的其他方法;
緣來
2018/09/18
5.2K0
[android] 采用服务录制电话&服务的生命周期
调用MediaRecorder对象的setAudioSource()方法,设置音频源,
唯一Chat
2019/09/10
6400
Android系统服务
本篇博文主要介绍的是Android中的Java服务。 这部分服务大部分都有一个Manager类,其实就是一个RPC调用,用户通过调用xxxManager的方法,实际上被Binder给迁移到system_server进程中对应的xxxManagerService中对应的方法,并将结果再通过binder带回。
小小工匠
2021/08/16
2K0
android学习笔记----服务的启动方式、生命周期和aidl的介绍
1.定义一个类继承Service 2.在Manifest.xml文件中配置该Service 3.使用Context的startService(Intent)方法启动该Service 4.不再使用时,调用stopService(Intent)方法或者stopSelf()方法停止该服务,如果不调用,服务会一直处于运行状态,可以在设置手动停止
砖业洋__
2023/05/06
7650
android学习笔记----服务的启动方式、生命周期和aidl的介绍
Android实现电话录音
在清单文件AndroidManifest.xml中添加权限: <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-
用户7999227
2021/09/19
1.2K0
Android手机监控小程序
9.AndroidManifest.xml中修改程序图标、增加intent-filter内容和权限。
知识与交流
2023/03/25
2.7K0
Android手机监控小程序
Android IBinder的linkToDeath介绍及情景模拟
最近查看Framework源码的时候,读到了AudioService处理音量的流程,在这里碰到了IBinder的linkToDeath()这个知识点,比较感兴趣,所以记录下来,并自己写demo尝试了一下。
Frank909
2019/01/14
2.7K0
Android监听来电状态
转载请以链接形式标明出处: 本文出自:103style的博客 记录一下. public class PhoneCallReceiver extends BroadcastReceiver { private static final String TAG = "PhoneCallReceiver "; private OnPhoneCallListener onPhoneCallListener; @Override public void onReceive(C
103style
2022/12/19
5500
[android] 手机卫士自定义吐司
继续在之前监听来电的服务AddressService里,添加成员方法MyToast()
唯一Chat
2019/09/10
5050
android四大组件
Android开发的四大组件,本文主要分为一、Activity详解 二、Service详解 三、Broadcast Receiver详解 四、Content Provider详解 外加一个重要组件 intent的详解。 一、Activity详解 Activty的生命周期的也就是它所在进程的生命周期。
黄啊码
2020/05/29
1.1K0
Android 开发常用代码片段
在AndroidManifest.xml中配置Activity节点是配置theme如下:
阳光岛主
2019/02/19
7570
相关推荐
android电话拦截
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档