719387365@qq.com
719387365@qq.com
  • 发布:2020-03-18 15:38
  • 更新:2020-07-01 03:01
  • 阅读:469

uni-app 短信监听

分类:uni-app

已经按照 https://ask.dcloud.net.cn/article/id-676 的方式来做但是短信发送过来
register(handleSMS); handleSMS 没有执行

var callbacks = [];
var receiver;
var filter;
var main;
var isInit = false;
var isRegistered = false;
var isOlderVersion = false;

//plusReady封装,若使用mui,可直接使用mui.plusReady()方法;    
var plusReady = function(callback){    
    //  if (window.plus) {    
    //     callback();    
    // } else {    
        try{  
            // #ifdef APP-PLUS  
            plus.globalEvent.addEventListener("newintent", function() {  
                 callback();    
             });    
             // #endif  
        }catch(e){  
            console.log(e)  
            //TODO handle the exception  
        }  

    // }    
}    
/**    
* 初始化    
*/    
var init = function(callback) {    
//仅支持Android版本    
if (plus.os.name !== 'Android') {    
    return;    
}    
try {    
    var version = plus.runtime.innerVersion.split('.');    
    isOlderVersion = parseInt(version[version.length - 1]) < 22298;    
    main = plus.android.runtimeMainActivity();    
    var Intent = plus.android.importClass('android.content.Intent');    
    var IntentFilter = plus.android.importClass('android.content.IntentFilter');    
    var SmsMessage = plus.android.importClass('android.telephony.SmsMessage');    
    var receiverClass = 'io.dcloud.feature.internal.reflect.BroadcastReceiver';    
    if (isOlderVersion) {    
        receiverClass = 'io.dcloud.feature.internal.a.a';    
    }    
    filter = new IntentFilter();    
    console.log('走到这里1')  
    var onReceiveCallback = function(context, intent) {  
            var action = intent.getAction();    
            if (action == "android.provider.Telephony.SMS_RECEIVED") {    
                var pdus = intent.getSerializableExtra("pdus");    
                var msgs = [];    
                for (var i = 0, len = pdus.length; i < len; i++) {    
                    msgs.push(SmsMessage.createFromPdu(pdus[i]));    
                }    
                for (var i = 0, len = callbacks.length; i < len; i++) {    
                    callbacks[i](msgs);    
                }    
            }    
    }    
    receiver = plus.android.implements(receiverClass, {    
        a: onReceiveCallback,    
        onReceive: onReceiveCallback    
    });    
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");    
    callback && callback();    
} catch (e) {}    

}

//注册短信监听    
var register = function(callback) {    
    callbacks.push(callback);   
    if (!isInit) {    
        isInit = isRegistered = true;    
        // plusReady(function() {    
            init(function() {    
                setTimeout(function() {    
                    try {    
                        if (isOlderVersion) {    
                            main.a(receiver, filter);    
                        } else {  
                            main.registerReceiver(receiver, filter); //注册监听  
                                //TODO handle the exception  
                        }    
                    } catch (e) {}    
                }, 300);    
            });    
        // });    
    } else if (!isRegistered) {    
        //      console.log('registerReceiver');    
        try {    
            if (isOlderVersion) {    
                main.a(receiver, filter);    
            } else {    
                main.registerReceiver(receiver, filter); //注册监听    
            }    
        } catch (e) {}    
    }  
};    

//注销监听,在登录成功或从登录页跳转到其它页面后调用    
var unregister = function(callback, remove) {    
    for (var i = 0, len = callbacks.length; i < len; i++) {    
        if (callbacks[i] === callback) {    
            callbacks.splice(i, 1);    
        }    
    }    
    if (remove && !callbacks.length) {    
        if (main && isRegistered) {    
            try {    
                if (isOlderVersion) {    
                    main.a(receiver);    
                } else {    
                    main.unregisterReceiver(receiver);    
                }    
            } catch (e) {}    
            isRegistered = false;    
            //          console.log('unregisterReceiver');    
        }    
    }    
};  

var handleSMS = function(msgs) {   
    console.log(1231)  
    var len = msgs.length  
    for (var i = 0; i < len; i++) {    
        var content = msgs[i].getDisplayMessageBody();    
        var phone = msgs[i].getDisplayOriginatingAddress()  
        //匹配短信内容,若短信内容包含“XX网”,则认为初步匹配成功    
            //匹配验证码规则,比如包含6位数字  
            console.log(phone)  
             console.log(content)  
             uni.showToast({  
                title:phone,  
                icon:'none'  
             })  
    }    
};    
register(handleSMS);
2020-03-18 15:38 负责人:无 分享
已邀请:
719387365@qq.com

719387365@qq.com (作者)

有好多人问我,我解决了 我代码放出来,新建一个页面直接用

719387365@qq.com

719387365@qq.com (作者)

手动置顶

admin@libaoka.com

admin@libaoka.com

来个稳得有没有 我改造成uniapp的vue方法去也还是不行

要回复问题请先登录注册