TelephoneManager分析,监听手机通话及网络通信

xxxxManager通过getsystemservice获得,管理xxxxService
ServiceManager管理xxxxService的Binder通讯
xxxxManager设置了hide api、system api
telephoneManager hide call dial endcall
listen(PhoneStateListener listener, int events):注册监听事件,监听指定的状态变化

例如:TelephonyManager#CALL_STATE_IDLE 没有通话
     TelephonyManager#CALL_STATE_RINGING 一个新的通话进来,正在响铃或等待
     TelephonyManager#CALL_STATE_OFFHOOK 至少一个通话存在,并且通话正在进行,没有新的通话进来

     TelephonyManager#DATA_UNKNOWN 初始值,在状态获取前使用
     TelephonyManager#DATA_DISCONNECED 网络连接失败
     TelephonyManager#DATA_CONNECTIN   正在启动数据连接
     TelephonyManager#DATA_CONNECTED   数据连接成功
     TelephonyManager#DATA_SUSPENDED   数据连接暂停,如,一个通话到达,网络切换到2G

     TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
            PhoneStateListener listener=new PhoneStateListener(){  
                @Override  
                //监测电话状态的变化。  
                public void onCallStateChanged(int state, String incomingNumber) {  // state 电话的状态 incomingNumber 打进来的号码  
                   switch(state){  
                       //无状态  
                       case  TelephonyManager.CALL_STATE_IDLE:  

                           break;  
                       //通话状态  
                       case  TelephonyManager.CALL_STATE_OFFHOOK:  
                           break;  
                       //响铃状态  
                       case  TelephonyManager.CALL_STATE_RINGING:  
                          int mode= Blackmdb.getInstance(getApplicationContext()).queuenumber(incomingNumber);  
                           if(mode==2||mode==3){  
                               Log.d("挂断电话","挂断");  
                               endcall();  
                           }  
                           break;  
                       default:  
                           break;  
                     }  
                    super.onCallStateChanged(state,incomingNumber);  

                }  

            };  
            tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE); 

getSystemService(String name)

public int getCallState() {
        try {
            ITelecomService telecom = getTelecomService();
            if (telecom != null) {
                return telecom.getCallState();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Error calling ITelecomService#getCallState", e);
        }
        return CALL_STATE_IDLE;
    }
private ITelephony getITelephony() {
        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
    }

    private ITelecomService getTelecomService() {
        return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
    }

ServiceMangager是隐藏类,只能通过反射调用。

private void endcall() {  
    //利用反射机制挂断电话  
        try {  
            //获取类对象  
            Class clazz= Class.forName("android.os.ServiceManager");  
            //获取类中方法,这个方法是非静态的  
            Method mothod = clazz.getDeclaredMethod("getService",String.class);  
            //执行这个方法  
           IBinder binder= (IBinder) mothod.invoke(null, Context.TELEPHONY_SERVICE);  
            ITelephony iTelephony = ITelephony.Stub.asInterface(binder);  
            iTelephony.endCall();//挂断电话  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (NoSuchMethodException e) {  
            e.printStackTrace();  
        } catch (InvocationTargetException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }  
    }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值