android9.0源码来电屏蔽

首先,通过获取TelephonyManage,第二继承PhoneStateListener 监听电话状态的变化,执行判断
哪个地方需要此功能加registerphone(this||***.this)

 public void registerphone(Context context) {
    
     mPhoneStateListener = new PhoneCallListener(); //监听
    
    mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); //获取TelephonyManager
     
     mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); //listen执行

 }	
public class PhoneCallListener extends PhoneStateListener { //继承PhoneStateListener 
    public void onCallStateChanged(int state, String incomingNumber) {
        Log.d(TAG, "onCallStateChanged-state: " + state); //打印LOG
        Log.d(TAG, "onCallStateChanged-incomingNumber: " + incomingNumber);
        switch (state)  { //执行判断
            case TelephonyManager.CALL_STATE_RINGING: //电话状态
                 endCall(); //挂断
                break;
            default:
                break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
}

private void endCall() {
    try {
       mTelephonyManager.endCall();    //调用mTelephonyManager中的endCall()进行挂断
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "endCallError", e);
    }
}

取消来电屏蔽

@Override

protected void onDestroy() {
    
    super.onDestroy();
	
	mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);

	
}

在需要的地方的onDestroy中加入

附贴listen()

 public void listen(PhoneStateListener listener, int events) {
   
    if (mContext == null || listener == null) return;
   
    try {
        
        boolean notifyNow = (getITelephony() != null);
        
        // If the listener has not explicitly set the subId (for example, created with the
       
        // default constructor), replace the subId so it will listen to the account the
       
        // telephony manager is created with.
       
        if (listener.mSubId == null) {
       
            listener.mSubId = mSubId;
     
        }

        ITelephonyRegistry registry = getTelephonyRegistry();
        if (registry != null) {
            registry.listenForSubscriber(listener.mSubId, getOpPackageName(),
                    listener.callback, events, notifyNow);
        } else {
            Rlog.w(TAG, "telephony registry not ready.");
        }
    } catch (RemoteException ex) {
        // system process dead
    }
}

PhoneStateListener.LISTEN_NONE值

           public static final int LISTEN_NONE = 0;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值