1.获得通话记录权限
//获得通话权限
plus.android.requestPermissions(['android.permission.READ_CALL_LOG', 'android.permission.WRITE_CALL_LOG', 'android.permission.CALL_PHONE'], (res) => {
if (res.granted.length > 0) {
console.log('已授权');
this.permisionObj[4].state = true;
} else {
console.log('请先授权!');
this.permisionObj[4].state = false;
}
});
获取通话记录文章来源:https://www.toymoban.com/news/detail-848989.html
import { parseTime } from '@/utils/format.js';
import permision from '@/js_sdk/wa-permission/permission.js';
import { gotoAppPermissionSetting } from '@/js_sdk/wa-permission/permission.js';
async getCalllog() {
//检查授权
var result = await permision.requestAndroidPermission(['android.permission.READ_CALL_LOG', 'android.permission.WRITE_CALL_LOG', 'android.permission.CALL_PHONE']);
var strStatus;
if (result == 1) {
strStatus = '已获得授权';
} else {
uni.showToast({
title: '读通话记录权限未开启',
icon: 'none'
});
setTimeout(() => {
uni.hideToast();
}, 5000);
}
var CallLog = plus.android.importClass('android.provider.CallLog');
var Activity = plus.android.runtimeMainActivity();
var ContentResolver = plus.android.importClass('android.content.ContentResolver');
var resolver = Activity.getContentResolver();
plus.android.importClass(resolver);
var String = plus.android.importClass('java.lang.String');
var cs = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
uni.showLoading({
title: '匹配通话记录中..'
});
var count = 0; // 记录多少条 用于处理循环跳出
while (plus.android.invoke(cs, 'moveToNext')) {
count++;
console.log(count);
let talist = {
// 联系人姓名
xm: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.CACHED_NAME)),
telphone: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.NUMBER)),
// 通话时长
duration: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.DURATION)),
date: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.DATE)),
type: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.TYPE))
};
talist.date = this.parseTime(talist.date);
switch (talist.type) {
// 判断通话类型
case '1':
talist.type = ' 呼入 ';
break;
case '2':
talist.type = ' 呼出 ';
break;
case '3':
talist.type = ' 未接 ';
break;
default:
talist.type = ' 挂断 ';
break;
}
if (count > 100) {
break;
}
this.telphoneLog.push(talist);
}
uni.hideLoading();
},
permission.js中部分代码(参考插件App权限判断和提示 - DCloud 插件市场)文章来源地址https://www.toymoban.com/news/detail-848989.html
// Android权限查询
function requestAndroidPermission(permissionID) {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(
[permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
function(resultObj) {
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
console.log('已获取的权限:' + grantedPermission);
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
result = -1
}
resolve(result);
// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
// if (result != 1) {
// gotoAppPermissionSetting()
// }
},
function(error) {
console.log('申请权限错误:' + error.code + " = " + error.message);
resolve({
code: error.code,
message: error.message
});
}
);
});
}
// 跳转到**应用**的权限页面
function gotoAppPermissionSetting() {
if (isIos) {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
} else {
// console.log(plus.device.vendor);
var Intent = plus.android.importClass("android.content.Intent");
var Settings = plus.android.importClass("android.provider.Settings");
var Uri = plus.android.importClass("android.net.Uri");
var mainActivity = plus.android.runtimeMainActivity();
var intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
mainActivity.startActivity(intent);
}
}
2.读取短信权限
async getMsgs() {
//检查、获取授权
var result = await permision.requestAndroidPermission('android.permission.READ_SMS');
var strStatus;
if (result == 1) {
strStatus = '已获得授权';
} else {
uni.showToast({
title: '读短信权限未开启',
icon: 'none'
});
setTimeout(() => {
uni.hideToast();
}, 2000);
}
//获得短信内容
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();
uni.showLoading({
title: '匹配短信记录中..'
});
// 这一块设置了拿到当前时间的前五分钟,默认是获取全部的短信信息
// 小米系统默认拿到的不是全部短信信息,需要在权限中开启"通知类短信"这个权限才能拿到全部
let newdata = new Date().getTime();
// 拿到当前时间的前五分钟
let fiveMinsAgo = new Date(newdata - 3 * 30 * 24 * 60 * 60 * 1000).getTime();
var selection = 'date > ' + fiveMinsAgo;
var cur = cr.query(uri, null, selection, null, null);
while (cur.moveToNext()) {
let newObj = {};
// 发送人号码
var index_Address = cur.getColumnIndex('address');
var address = cur.getString(index_Address);
newObj.telphone = address;
// console.log('发送人号码:' + address);
//短信内容
var index_Body = cur.getColumnIndex('body');
var body = cur.getString(index_Body);
newObj.content = body;
// console.log('短信内容:' + body);
//短信类型1接收 2发送
var index_Type = cur.getColumnIndex('type');
var type = cur.getString(index_Type);
newObj.type = type;
// console.log('短信类型:' + type);
// 发送日期
var smsDate = cur.getString(cur.getColumnIndex('date'));
smsDate = this.parseTime(smsDate);
newObj.sendDate = smsDate;
// console.log('发送日期:' + smsDate);
this.msgList.push(newObj);
}
cur.close();
uni.hideLoading();
}
到了这里,关于uniapp获得手机通话记录、短信等权限的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!