Android电话拦截实现以及TelephonyManager监听的取消

由于毕业设计题目涉及到电话拦截这一块。所以鼓捣了一下。遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们。

本篇博客目的:实现电话的拦截

会遇到的问题:android studio下AIDL的使用,TelephonyManager.Listen()的监听取消。

首先,电话状态监听需要涉及到系统服务TelephonyManager,我们需要获取到他的实例

 mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
     

之后我们就可以向他添加状态改变的监听,需要重写监听器PhoneStateListener

他的state值有如下几种

TelephonyManager.CALL_STATE_IDLE:  空闲状态
TelephonyManager.CALL_STATE_RINGING:  响铃状态
TelephonyManager.CALL_STATE_OFFHOOK:  挂掉电话

    class MyPhoneListener extends PhoneStateListener{

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if(state==TelephonyManager.CALL_STATE_RINGING){
                    endCall();
                    super.onCallStateChanged(state,incomingNumber);
                }
            }
        }
这里我给他加了个模式来设置监听与取消。

我们需要用一个FLAG : PhoneStateListener.LISTEN_NONE来取消监听。如果新加监听器,会同时监听

   private void setTelephonyListener(int mode){
        if(mode==INTERRUPTED){
            mTelephonyManager.listen(mMyPhoneListener,PhoneStateListener.LISTEN_CALL_STATE);
        }else{
            mTelephonyManager.listen(mMyPhoneListener,PhoneStateListener.LISTEN_NONE);
        }
    }
好了,以上就是状态监听的步骤。

那么如何实现电话的拦截呢?由于安卓隐藏了PHONE类的API,所以我们需要用AIDL将它的方法反射出来。

1,新建AIDL文件,ITelephony.aidl。注意包名为com.android.internal.telephony,不可更改,此时需要rebuild project才能正常使用。

内容如下:

package com.android.internal.telephony;  

 /**  

  * 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 instead.  

  * {@hide}  

  */ 



interface ITelephony {      

 /**      

  * 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(); 

    

    /**

     * Allow mobile data connections.

     */

    boolean enableDataConnectivity();



    /**

     * Disallow mobile data connections.

     */

    boolean disableDataConnectivity();



    /**

     * Report whether data connectivity is possible.

     */

    boolean isDataConnectivityPossible();

} 



之后来反射出来endcall方法,当来电的时候 即可对电话进行拦截

 private void endCall()
    {
        Class<TelephonyManager> c = TelephonyManager.class;
        try
        {
            Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
            getITelephonyMethod.setAccessible(true);
            ITelephony iTelephony = null;
            iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null);
            iTelephony.endCall();

            Log.i("wing", "iTelePhony endcall");
        }
        catch (Exception e)
        {

            Log.i("wing", "iTelePhony endcall failed"+e.getMessage()    );
        }
    }
以上就是我的经验和总结,欢迎大家讨论。



转载于:https://www.cnblogs.com/muyuge/p/6333566.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中实现电话拦截功能可以通过以下步骤进行: 1. 添加权限:在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> ``` 2. 创建BroadcastReceiver:创建一个继承自BroadcastReceiver的类,用于接收来电广播和拦截电话。在该类中,重写onReceive()方法,并在其中实现电话拦截逻辑。 ```java public class CallInterceptor extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 获取来电号码 String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); // 判断是否需要拦截电话 if (shouldInterceptCall(phoneNumber)) { // 拦截电话 endCall(context); } } private boolean shouldInterceptCall(String phoneNumber) { // 在这里实现判断逻辑,根据需要拦截的条件返回true或false // 例如,可以根据黑名单列表判断是否需要拦截电话 return phoneNumber.equals("1234567890"); } private void endCall(Context context) { // 挂断电话 TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try { Class<?> telephonyClass = Class.forName(telephonyManager.getClass().getName()); Method method = telephonyClass.getDeclaredMethod("getITelephony"); method.setAccessible(true); Object telephonyInterface = method.invoke(telephonyManager); Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName()); Method endCallMethod = telephonyInterfaceClass.getDeclaredMethod("endCall"); endCallMethod.invoke(telephonyInterface); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 注册BroadcastReceiver:在AndroidManifest.xml文件中注册BroadcastReceiver,并添加相应的intent-filter,以接收来电广播。 ```xml <receiver android:name=".CallInterceptor"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> ``` 4. 动态申请权限:在需要拦截电话的地方,动态申请电话权限。 ```java if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE); } ``` 以上是在Android Studio中实现电话拦截功能的基本步骤。你可以根据具体需求进行适当的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值