电话拦截

简介: 首先需要 android 源码文件NeighboringCellInfo.aidl和ITelephony.aidl,新建文件夹android.telephony(文件名必须为这个名称),将文件NeighboringCellInfo.aidl拷贝到该文件夹下,在新建另一个文件夹com.android.internal.telephony(不必须名称),将文件ITelephony.aidl放入

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

项目目录图:



详细代码如下:

  1. package com.internal.telephony;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.media.AudioManager;  
  9. import android.os.IBinder;  
  10. import android.telephony.TelephonyManager;  
  11. import android.util.Log;  
  12.   
  13. import com.android.internal.telephony.ITelephony;  
  14. import com.internal.main.BlockList;  
  15.   
  16. public class TelInternal extends BroadcastReceiver {  
  17.   
  18.     @Override  
  19.     public void onReceive(Context context, Intent intent) {  
  20.   
  21.         AudioManager mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);  
  22.         BlockList b=new BlockList(context);   
  23.           
  24.         if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){//  Log.e("msg", "calling");     
  25.             
  26.         //如果是去电(拨出)    
  27.             String num=getResultData();  
  28.               
  29.             if(num.equals("12345")){  
  30.             setResultData(null); //清除电话  
  31.             break;  
  32.             }  
  33.               
  34.               
  35.         }else{ //由于android没有来点广播所以,去掉拨打电话就是来电状态了  
  36.             // Log.e("msg", "coming");     
  37.               
  38.              String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);     
  39.            //  Log.e("msg", "State: "+ state);     
  40.                   
  41.              String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);     
  42.            //  Log.e("msg", "Incomng Number: " + number);     
  43.                   
  44.              if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){Log.e("msg", "ring");  
  45.               
  46.                  if(number.equals("12345")){//拦截指定的电话号码     
  47.                      //先静音处理     
  48.                      mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);     
  49.               //       Log.e("msg", "Turn Ringtone Silent");     
  50.                           
  51.                      try {     
  52.                         /* //挂断电话   方法一  
  53.                          Method method = Class.forName(  
  54.                                 "android.os.ServiceManager").getMethod(  
  55.                                 "getService", String.class);  
  56.                             // 获取远程TELEPHONY_SERVICE的IBinder对象的代理  
  57.                             IBinder binder = (IBinder) method.invoke(null,  
  58.                                 new Object[] { Context.TELEPHONY_SERVICE });  
  59.                             // 将IBinder对象的代理转换为ITelephony对象  
  60.                             ITelephony telephony = ITelephony.Stub  
  61.                                 .asInterface(binder);  
  62.                             // 挂断电话  
  63.                             telephony.endCall();  Log.e("msg", "end"); */   
  64.                         //挂断电话   方法二  
  65.                          ITelephony  iTelephony = getITelephony(context); //获取电话接口  
  66.                           iTelephony.endCall(); // 挂断电话  
  67.                           Log.e("msg", "end");  
  68.                      } catch (Exception e) {     
  69.                          e.printStackTrace();     
  70.                      }     
  71.                      //再恢复正常铃声     
  72.                      mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);     
  73.                      break;  
  74.                  }     
  75.                
  76.              }  
  77.          }  
  78.     }  
  79.     /**  
  80.      * 根据反射获取end()方法2  
  81.      * @param context  
  82.      * @return  
  83.      */  
  84.      private static ITelephony getITelephony(Context context) {  
  85.          ITelephony iTelephony=null;  
  86.             TelephonyManager mTelephonyManager = (TelephonyManager) context  
  87.                     .getSystemService(Context.TELEPHONY_SERVICE);  
  88.             Class<TelephonyManager> c = TelephonyManager.class;  
  89.             Method getITelephonyMethod = null;  
  90.             try {  
  91.                 getITelephonyMethod = c.getDeclaredMethod("getITelephony",  
  92.                         (Class[]) null); // 获取声明的方法  
  93.                 getITelephonyMethod.setAccessible(true);  
  94.             } catch (SecurityException e) {  
  95.                 e.printStackTrace();  
  96.             } catch (NoSuchMethodException e) {  
  97.                 e.printStackTrace();  
  98.             }  
  99.   
  100.             try {  
  101.                 iTelephony = (ITelephony) getITelephonyMethod.invoke(  
  102.                         mTelephonyManager, (Object[]) null); // 获取实例  
  103.                 return iTelephony;  
  104.             } catch (Exception e) {  
  105.                 e.printStackTrace();  
  106.             }  
  107.             return iTelephony;  
  108.         }  
  109.        
  110. }  


注册广播:
  1. <receiver android:name="com.internal.telephony.TelInternal" android:enabled="true">  
  2.          <intent-filter>  
  3.           <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>  
  4.           <action android:name="android.intent.action.PHONE_STATE"/>  
  5.          </intent-filter>  
  6.        </receiver>  


相关权限:

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


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

文件NeighboringCellInfo.aidl源码:

  1. /* //device/java/android/android/content/Intent.aidl  
  2. **  
  3. ** Copyright 2007, The Android Open Source Project  
  4. **  
  5. ** Licensed under the Apache License, Version 2.0 (the "License");  
  6. ** you may not use this file except in compliance with the License.  
  7. ** You may obtain a copy of the License at  
  8. **  
  9. **     http://www.apache.org/licenses/LICENSE-2.0  
  10. **  
  11. ** Unless required by applicable law or agreed to in writing, software  
  12. ** distributed under the License is distributed on an "AS IS" BASIS,  
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14. ** See the License for the specific language governing permissions and  
  15. ** limitations under the License.  
  16. */  
  17.   
  18. package android.telephony;  
  19.   
  20. parcelable NeighboringCellInfo;  

文件ITelephony.aidl源码:
  1. /*  
  2.  * Copyright (C) 2007 The Android Open Source Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */  
  16.   
  17. package com.android.internal.telephony;  
  18.   
  19. import android.os.Bundle;  
  20. import java.util.List;  
  21. import android.telephony.NeighboringCellInfo;  
  22.   
  23. /**  
  24.  * Interface used to interact with the phone.  Mostly this is used by the   
  25.  * TelephonyManager class.  A few places are still using this directly.  
  26.  * Please clean them up if possible and use TelephonyManager insteadl.  
  27.  *  
  28.  * {@hide}  
  29.  */  
  30. interface ITelephony {  
  31.   
  32.     /**  
  33.      * Dial a number. This doesn't place the call. It displays  
  34.      * the Dialer screen.  
  35.      * @param number the number to be dialed. If null, this  
  36.      * would display the Dialer screen with no number pre-filled.  
  37.      */  
  38.     void dial(String number);  
  39.   
  40.     /**  
  41.      * Place a call to the specified number.  
  42.      * @param number the number to be called.  
  43.      */  
  44.     void call(String number);  
  45.   
  46.     /**  
  47.      * If there is currently a call in progress, show the call screen.  
  48.      * The DTMF dialpad may or may not be visible initially, depending on  
  49.      * whether it was up when the user last exited the InCallScreen.  
  50.      *  
  51.      * @return true if the call screen was shown.  
  52.      */  
  53.     boolean showCallScreen();  
  54.   
  55.     /**  
  56.      * Variation of showCallScreen() that also specifies whether the  
  57.      * DTMF dialpad should be initially visible when the InCallScreen  
  58.      * comes up.  
  59.      *  
  60.      * @param showDialpad if true, make the dialpad visible initially,  
  61.      *                    otherwise hide the dialpad initially.  
  62.      * @return true if the call screen was shown.  
  63.      *  
  64.      * @see showCallScreen  
  65.      */  
  66.     boolean showCallScreenWithDialpad(boolean showDialpad);  
  67.   
  68.     /**  
  69.      * End call or go to the Home screen  
  70.      *  
  71.      * @return whether it hung up  
  72.      */  
  73.     boolean endCall();  
  74.   
  75.     /**  
  76.      * Answer the currently-ringing call.  
  77.      *  
  78.      * If there's already a current active call, that call will be  
  79.      * automatically put on hold.  If both lines are currently in use, the  
  80.      * current active call will be ended.  
  81.      *  
  82.      * TODO: provide a flag to let the caller specify what policy to use  
  83.      * if both lines are in use.  (The current behavior is hardwired to  
  84.      * "answer incoming, end ongoing", which is how the CALL button  
  85.      * is specced to behave.)  
  86.      *  
  87.      * TODO: this should be a oneway call (especially since it's called  
  88.      * directly from the key queue thread).  
  89.      */  
  90.     void answerRingingCall();  
  91.   
  92.     /**  
  93.      * Silence the ringer if an incoming call is currently ringing.  
  94.      * (If vibrating, stop the vibrator also.)  
  95.      *  
  96.      * It's safe to call this if the ringer has already been silenced, or  
  97.      * even if there's no incoming call.  (If so, this method will do nothing.)  
  98.      *  
  99.      * TODO: this should be a oneway call too (see above).  
  100.      *       (Actually *all* the methods here that return void can  
  101.      *       probably be oneway.)  
  102.      */  
  103.     void silenceRinger();  
  104.   
  105.     /**  
  106.      * Check if we are in either an active or holding call  
  107.      * @return true if the phone state is OFFHOOK.  
  108.      */  
  109.     boolean isOffhook();  
  110.   
  111.     /**  
  112.      * Check if an incoming phone call is ringing or call waiting.  
  113.      * @return true if the phone state is RINGING.  
  114.      */  
  115.     boolean isRinging();  
  116.   
  117.     /**  
  118.      * Check if the phone is idle.  
  119.      * @return true if the phone state is IDLE.  
  120.      */  
  121.     boolean isIdle();  
  122.   
  123.     /**  
  124.      * Check to see if the radio is on or not.  
  125.      * @return returns true if the radio is on.  
  126.      */  
  127.     boolean isRadioOn();  
  128.   
  129.     /**  
  130.      * Check if the SIM pin lock is enabled.  
  131.      * @return true if the SIM pin lock is enabled.  
  132.      */  
  133.     boolean isSimPinEnabled();  
  134.   
  135.     /**  
  136.      * Cancels the missed calls notification.  
  137.      */  
  138.     void cancelMissedCallsNotification();   
  139.   
  140.     /**  
  141.      * Supply a pin to unlock the SIM.  Blocks until a result is determined.  
  142.      * @param pin The pin to check.  
  143.      * @return whether the operation was a success.  
  144.      */  
  145.     boolean supplyPin(String pin);  
  146.   
  147.     /**  
  148.      * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which are initiated  
  149.      * without SEND (so <code>dial</code> is not appropriate).  
  150.      *   
  151.      * @param dialString the MMI command to be executed.  
  152.      * @return true if MMI command is executed.  
  153.      */  
  154.     boolean handlePinMmi(String dialString);  
  155.   
  156.     /**  
  157.      * Toggles the radio on or off.  
  158.      */  
  159.     void toggleRadioOnOff();  
  160.   
  161.     /**  
  162.      * Set the radio to on or off  
  163.      */  
  164.     boolean setRadio(boolean turnOn);  
  165.   
  166.     /**  
  167.      * Request to update location information in service state  
  168.      */  
  169.     void updateServiceLocation();  
  170.   
  171.     /**  
  172.      * Enable location update notifications.  
  173.      */  
  174.     void enableLocationUpdates();  
  175.   
  176.     /**  
  177.      * Disable location update notifications.  
  178.      */  
  179.     void disableLocationUpdates();  
  180.   
  181.     /**  
  182.      * Enable a specific APN type.  
  183.      */  
  184.     int enableApnType(String type);  
  185.   
  186.     /**  
  187.      * Disable a specific APN type.  
  188.      */  
  189.     int disableApnType(String type);  
  190.   
  191.     /**  
  192.      * Allow mobile data connections.  
  193.      */  
  194.     boolean enableDataConnectivity();  
  195.   
  196.     /**  
  197.      * Disallow mobile data connections.  
  198.      */  
  199.     boolean disableDataConnectivity();  
  200.   
  201.     /**  
  202.      * Report whether data connectivity is possible.  
  203.      */  
  204.     boolean isDataConnectivityPossible();  
  205.   
  206.     Bundle getCellLocation();  
  207.   
  208.     /**  
  209.      * Returns the neighboring cell information of the device.  
  210.      */  
  211.     List<NeighboringCellInfo> getNeighboringCellInfo();  
  212.   
  213.      int getCallState();  
  214.      int getDataActivity();  
  215.      int getDataState();  
  216. }  


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

  1. <uses-permission android:name="android.permission.CALL_PHONE"/>    
  2.     <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>  


ok,希望对大家有帮助!
目录
相关文章
|
1月前
|
API 数据安全/隐私保护 开发者
怎么发电子邮件?aoksendAPI接口发信方法
怎么发电子邮件?aoksendAPI接口发信方法
|
9月前
|
JavaScript API 容器
手机短信验证码登录功能的开发实录(机器识别码、短信限流、错误提示、发送验证码倒计时60秒)
手机短信验证码登录功能的开发实录(机器识别码、短信限流、错误提示、发送验证码倒计时60秒)
198 1
|
11月前
|
安全 前端开发 机器人
短信验证码接口说明
短信验证码接口的功能是,自定义您的短信签名与短信内容,给用户手机发送短信。简易对接,快速添加短信签名,无需等待,可添加多个短信签名。请联系客服添加短信模板,进行测试对接。
短信验证码接口说明
|
Java 云计算
JAVA实现《阿里云发送短信验证码以及短信通知》
JAVA实现《阿里云发送短信验证码以及短信通知》
826 0
|
监控 安全 搜索推荐
网址被QQ拦截后应该怎么做才可能尽快解除拦截
作为一个多元化的应用平台,QQ拥有着庞大的基础用户群,腾讯旗下任何一个新兴的网络应用都会受到QQ用户的广泛认可,因此,QQ应用平台一直是网站推广的理想选择。
661 0
网址被QQ拦截后应该怎么做才可能尽快解除拦截
|
XML API 数据安全/隐私保护
SIP网关怎样拨打外部电话
SIP网关怎样拨打外部电话
|
存储 JavaScript 前端开发
纳税服务系统五(登陆与系统拦截)【配置系统、子系统首页、登陆与拦截】(下)
到目前位置,我们的用户模块和角色模块基本已经做好了,我们的纳税服务系统是放在一个大系统里边的。我们应该把我们已经写好的模块加载进去。 本文主要的知识点: 配置系统首页 登陆模块 权限拦截模块
332 0
纳税服务系统五(登陆与系统拦截)【配置系统、子系统首页、登陆与拦截】(下)
|
Java 数据安全/隐私保护
纳税服务系统五(登陆与系统拦截)【配置系统、子系统首页、登陆与拦截】(上)
到目前位置,我们的用户模块和角色模块基本已经做好了,我们的纳税服务系统是放在一个大系统里边的。我们应该把我们已经写好的模块加载进去。 本文主要的知识点: 配置系统首页 登陆模块 权限拦截模块
256 0
纳税服务系统五(登陆与系统拦截)【配置系统、子系统首页、登陆与拦截】(上)
|
iOS开发
iOS用CallKit实现来电识别、来电拦截
前言 最近需要实现一个新需求,用iOS 10出的CallKit实现将APP的通讯录的信息同步到系统中,可以不把人员信息加到通讯录中,实现来电号码识别。
2880 0