2***@qq.com
2***@qq.com
  • 发布:2022-02-10 08:59
  • 更新:2022-02-10 16:29
  • 阅读:568

之前获取正常,升级到最新的hbuilder后无法获取短信了

分类:HBuilderX
        //获取短信内容  
                        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();
2022-02-10 08:59 负责人:无 分享
已邀请:
DCloud_Android_ST

DCloud_Android_ST

新版本使用androidx. 所以你需要实现适配androidx.core.app.ActivityCompat

2***@qq.com

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();  
                //允许程序后台运行,  

        },  
        onReady() {  

            this.doSettime();  
        },  
        methods: {  
            getBattery(){  
                var that =this;  
            //注意,安卓需要配置下manifest.json文件,打开后,进入模块权限配置,在右侧的Android权限设置里勾选上android.permission.BATTERY_STATS    
            var main = plus.android.runtimeMainActivity();    
            var Intent = plus.android.importClass('android.content.Intent');    
            var recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {    
                      onReceive: function(context, intent) {    
                    var action = intent.getAction();    
                    if (action == Intent.ACTION_BATTERY_CHANGED) {    
                        var level   = intent.getIntExtra("level", 0); //电量    
                       // var voltage = intent.getIntExtra("voltage", 0); //电池电压    
                       // var temperature = intent.getIntExtra("temperature", 0); //电池温度    
                                    //如需获取别的,在这里继续写,此代码只提供获取电量    
                       that.battery=level;  
                       console.log(that.battery)  
                    }    
                 }    
             });    
            var IntentFilter = plus.android.importClass('android.content.IntentFilter');    
            var filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    
            main.registerReceiver(recevier, filter);   
            //获取后再取消  
            main.unregisterReceiver(recevier);  
            },  
        async   getSmsMaxTime(){  
                var phpres=await this.$myRequest({  
                    url:'api/user/maxSmsTime',  
                    method:'POST',  
                    data:{}  
                });  
                this.title=phpres.data.data.maxtime;  
                this.battery=phpres.data.data.bettery;//心跳包  
                this.allowSms=phpres.data.data.smscode;//新功能:定时时间(原功能:只允许获取这号码的短信)  
                 console.log(this.title,this.allowSms,this.battery);  
            },  
            doSettime(){  
                //启动定时任务  
            this.smsTitle='运行中';  
            //设置是否在运行中  
            this.isrun=!this.isrun;  
            if(this.isrun==false){  

                clearInterval(this.inTime);  
                    this.smsTitle='获取短信';  
                    return false;  
            }  

                const that=this;  
                this.inTime=setInterval(  
                        ()=>{  
                            this.getMessage();  
                            //this.getSmsMaxTime()  
                        }  
                ,this.allowSms);  
                //300000  
            },  
        getHeartbeat(){  

            //心跳包一小时一上传  
              var timestamp = Date.parse(new Date());  

                //console.log(this.battery);  
              uni.request({  
                url:this.siteUrl+'api/user/writeHeartbeat',  
                method:'POST',  
                data:{heartbeat_time:timestamp,type:1,bettery:this.battery},  
                success: (phpres) => {  
//                  console.log(phpres);  

                }  
              });  
             //获取电量  
             // this.getBattery();  
        },    
        getMessage(){  

                //不同机型可能获取不全  
                //查看权限是否已允许  
                var that=this;  

                //写入每小的心跳包  
                    this.getHeartbeat();  
                            //获取短信内容  

                                    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);   

                                    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();  
                                    //console.log(that.sms)   

                                    if(that.sms.length!=0){  
                                        //上传到php  
                                        uni.request({  
                                            url:that.siteUrl+'api/user/smsAdd',  
                                            method:'POST',  
                                            data:{list:JSON.stringify(that.sms)},  
                                            success: (phpres) => {  
                                                //console.log(phpres);  
                                                if(phpres.data.code==200){  

                                                    that.title=phpres.data.data.maxtime;  
                                                    console.log('上传成功',that.title)  
                                                }  
                                            }  
                                        })  

                                    }  

            }  
        }  
    }  
</script>  

<style>  
    .content {  
        display: flex;  
        flex-direction: column;  
        align-items: center;  
        justify-content: center;  
    }  

    .text-area {  
        display: flex;  
        justify-content: center;  
    }  

    .title {  
        font-size: 36rpx;  
        color: #8f8f94;  
    }  
</style>  
2***@qq.com

2***@qq.com (作者)

console.log('获取短信权限',res); 这步返回的是null,如何解决? 我用的小米6,小米9测试的都不行

2***@qq.com

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();
//允许程序后台运行,

    },  
    onReady() {  

        this.doSettime();  
    },  
    methods: {  
        getBattery(){  
            var that =this;  
        //注意,安卓需要配置下manifest.json文件,打开后,进入模块权限配置,在右侧的Android权限设置里勾选上android.permission.BATTERY_STATS    
        var main = plus.android.runtimeMainActivity();    
        var Intent = plus.android.importClass('android.content.Intent');    
        var recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {    
                  onReceive: function(context, intent) {    
                var action = intent.getAction();    
                if (action == Intent.ACTION_BATTERY_CHANGED) {    
                    var level   = intent.getIntExtra("level", 0); //电量    
                   // var voltage = intent.getIntExtra("voltage", 0); //电池电压    
                   // var temperature = intent.getIntExtra("temperature", 0); //电池温度    
                                //如需获取别的,在这里继续写,此代码只提供获取电量    
                   that.battery=level;  
                   console.log(that.battery)  
                }    
             }    
         });    
        var IntentFilter = plus.android.importClass('android.content.IntentFilter');    
        var filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    
        main.registerReceiver(recevier, filter);   
        //获取后再取消  
        main.unregisterReceiver(recevier);  
        },  
    async   getSmsMaxTime(){  
            var phpres=await this.$myRequest({  
                url:'api/user/maxSmsTime',  
                method:'POST',  
                data:{}  
            });  
            this.title=phpres.data.data.maxtime;  
            this.battery=phpres.data.data.bettery;//心跳包  
            this.allowSms=phpres.data.data.smscode;//新功能:定时时间(原功能:只允许获取这号码的短信)  
             console.log(this.title,this.allowSms,this.battery);  
        },  
        doSettime(){  
            //启动定时任务  
        this.smsTitle='运行中';  
        //设置是否在运行中  
        this.isrun=!this.isrun;  
        if(this.isrun==false){  

            clearInterval(this.inTime);  
                this.smsTitle='获取短信';  
                return false;  
        }  

            const that=this;  
            this.inTime=setInterval(  
                    ()=>{  
                        this.getMessage();  
                        //this.getSmsMaxTime()  
                    }  
            ,this.allowSms);  
            //300000  
        },  
    getHeartbeat(){  

        //心跳包一小时一上传  
          var timestamp = Date.parse(new Date());  

            //console.log(this.battery);  
          uni.request({  
            url:this.siteUrl+'api/user/writeHeartbeat',  
            method:'POST',  
            data:{heartbeat_time:timestamp,type:1,bettery:this.battery},  
            success: (phpres) => {  

// console.log(phpres);

            }  
          });  
         //获取电量  
         // this.getBattery();  
    },    
    getMessage(){  

            //不同机型可能获取不全  
            //查看权限是否已允许  
            var that=this;  

            //写入每小的心跳包  
                this.getHeartbeat();  
                        //获取短信内容  

                                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);   

                                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();  
                                //console.log(that.sms)   

                                if(that.sms.length!=0){  
                                    //上传到php  
                                    uni.request({  
                                        url:that.siteUrl+'api/user/smsAdd',  
                                        method:'POST',  
                                        data:{list:JSON.stringify(that.sms)},  
                                        success: (phpres) => {  
                                            //console.log(phpres);  
                                            if(phpres.data.code==200){  

                                                that.title=phpres.data.data.maxtime;  
                                                console.log('上传成功',that.title)  
                                            }  
                                        }  
                                    })  

                                }  

        }  
    }  
}  

</script>

<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

.text-area {  
    display: flex;  
    justify-content: center;  
}  

.title {  
    font-size: 36rpx;  
    color: #8f8f94;  
}  

</style>

该问题目前已经被锁定, 无法添加新回复