前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >电话拦截

电话拦截

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

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

项目目录图:

详细代码如下:

代码语言:html
复制
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
复制
<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
复制
/* //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
复制
/*  
 * 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
复制
<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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档