//获取短信内容
var Context = plus.android.runtimeMainActivity();
var res = plus.android.invoke('android.support.v4.app.ActivityCompat', 'checkSelfPermission', Context, 'android.permission.READ_SMS');
console.log('获取短信权限',res);
var PERMISSIONS_STORAGE = new Array();
PERMISSIONS_STORAGE.push('android.permission.READ_SMS');
// res == -1 时为询问状态,询问时会走Show 和 Hidden
if (res != '0') {
plus.android.invoke('android.support.v4.app.ActivityCompat', 'requestPermissions', Context, PERMISSIONS_STORAGE, 1);
} else {
that.sms=[];
var main = plus.android.runtimeMainActivity();
var Uri = plus.android.importClass('android.net.Uri');
var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
var uri = Uri.parse('content://sms/');
var cr = main.getContentResolver();
plus.android.importClass(cr);
var cur = cr.query(uri, null, null, null, null);
plus.android.importClass(cur);
//cur.moveToFirst();
that.count++;
that.smsTitle='运行'+that.count+'次';
while (cur.moveToNext()) {
// 时间
var index_date = cur.getColumnIndex("date");
var date = cur.getString(index_date);
//如果是原内容,就退出
if(date<=that.title){
break;
}
//类型1接收 2发送
var index_Type = cur.getColumnIndex('type');
var type = cur.getString(index_Type);
if(type!=1){
continue;
}
var index_Address = cur.getColumnIndex('address');
var address = cur.getString(index_Address);
//短信内容
var index_Body = cur.getColumnIndex('body');
var body = cur.getString(index_Body);
//console.log(address, body, type);
that.sms.push({
address:address,
body:body,
type:type,
date:date
})
// 收件人
// 短信内容
// 短信的类型
}
cur.close();
2***@qq.com
- 发布:2022-02-10 08:59
- 更新:2022-02-10 16:29
- 阅读:568
4 个回复
DCloud_Android_ST
新版本使用androidx. 所以你需要实现适配androidx.core.app.ActivityCompat
2***@qq.com (作者)
2***@qq.com (作者)
console.log('获取短信权限',res); 这步返回的是null,如何解决? 我用的小米6,小米9测试的都不行
2***@qq.com (作者)
目前用这个可以正常,代码如下:
<template>
<view class="content">
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view >
<button type="default" @click="doSettime()">{{smsTitle}}</button>
<!-- <button type="default" @click="getHeartbeat()">获取</button> -->
</view>
<scroll-view scroll-x="true">
<v-table :columns="tableTitle" :list="sms"></v-table>
</scroll-view>
</view>
</template>
<script>
import vTable from "@/components/nobadtable/table.vue"
export default {
components:{
vTable
},
data() {
return {
title: 0,
inTime:null,
isrun:false,
allowSms:30000,
count:0,
battery:0,
smsTitle:'获取短信',
sms:[],
"tableTitle": [
{
"title": "短信号码",
"key": "address"
},
{
"title": "短信内容",
"key": "body",
"$width":500
},
{
"title": "1接受2发送",
"key": "type"
}
]
}
},
onLoad() {
this.getSmsMaxTime();
//允许程序后台运行,
// console.log(phpres);
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</style>