欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2021-12-08 11:54:58
自动接听与挂断调用关系如下1. 首先在启动apk初始界面的activity中,启动service服务,开始监听通话状态(incallservice) //启动通话状态监听服务 private...

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

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拦截与接听内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: android 拦截