Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android拦截与接听

Android自动拦截与接听功能APK黑白名单

作者:perfectxiannv

大家好,本篇文章主要讲的是Android自动拦截与接听功能APK黑白名单,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

自动接听与挂断调用关系如下

1. 首先在启动apk初始界面的activity中,启动service服务,开始监听通话状态(InCallService)

   //启动通话状态监听服务
    private void startCallListen() {
        Intent intent = new Intent(CallAndMessageEntranceActivity.this, InCallService.class);
        startService(intent);
}
 

2. 在通话监听服务中,监听通话状态,当通话状态为CALL_STATE_RINGING时处理通话。

public class InCallService extends Service {
    private TelecomManager mTelecomManager;
    private TelephonyManager mTelephonymanager;
    MyPhoneStateListener mMyPhoneStateListener = new MyPhoneStateListener();
 
    @Override
    public void onCreate() {
        Log.e("InCallService", "onCreate()");
        super.onCreate();
 
        //获取电话服务
        mTelecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
        mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
   }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mTelephonymanager == null) {
            mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        }
        mTelephonymanager.listen(mMyPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        return super.onStartCommand(intent, flags, startId);
    }
 
    //监听通过话
    public class MyPhoneStateListener extends PhoneStateListener {
 
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        public void onCallStateChanged(int state, final String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    //通话空闲状态
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    //通话来电状态
                    try {
                        Log.e("C_M_BlackCallService", "Monitor incoming calls");
                        //拦截来电判断来电时黑名单or白名单
                        splitWhiteAndBlack(incomingNumber);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //电话挂起状态
                    break;
            }
 
        }
    }
    //incomingNumber:来电号码 依据来电号码判断黑白名单
    private void splitWhiteAndBlack(String incomingNumber) {
        //1.黑名单自动挂断
        mTelecomManager.endCall();
        //2.白名单自动接听
        mTelecomManager.acceptRingingCall();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
 
}

到此这篇关于Android自动拦截与接听功能APK黑白名单的文章就介绍到这了,更多相关Android拦截与接听内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文