当前位置:   article > 正文

uniapp获得手机通话记录、短信等权限_plus.android.requestpermissions

plus.android.requestpermissions

1.获得通话记录权限

  1. //获得通话权限
  2. plus.android.requestPermissions(['android.permission.READ_CALL_LOG', 'android.permission.WRITE_CALL_LOG', 'android.permission.CALL_PHONE'], (res) => {
  3. if (res.granted.length > 0) {
  4. console.log('已授权');
  5. this.permisionObj[4].state = true;
  6. } else {
  7. console.log('请先授权!');
  8. this.permisionObj[4].state = false;
  9. }
  10. });

获取通话记录

  1. import { parseTime } from '@/utils/format.js';
  2. import permision from '@/js_sdk/wa-permission/permission.js';
  3. import { gotoAppPermissionSetting } from '@/js_sdk/wa-permission/permission.js';
  4. async getCalllog() {
  5. //检查授权
  6. var result = await permision.requestAndroidPermission(['android.permission.READ_CALL_LOG', 'android.permission.WRITE_CALL_LOG', 'android.permission.CALL_PHONE']);
  7. var strStatus;
  8. if (result == 1) {
  9. strStatus = '已获得授权';
  10. } else {
  11. uni.showToast({
  12. title: '读通话记录权限未开启',
  13. icon: 'none'
  14. });
  15. setTimeout(() => {
  16. uni.hideToast();
  17. }, 5000);
  18. }
  19. var CallLog = plus.android.importClass('android.provider.CallLog');
  20. var Activity = plus.android.runtimeMainActivity();
  21. var ContentResolver = plus.android.importClass('android.content.ContentResolver');
  22. var resolver = Activity.getContentResolver();
  23. plus.android.importClass(resolver);
  24. var String = plus.android.importClass('java.lang.String');
  25. var cs = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
  26. uni.showLoading({
  27. title: '匹配通话记录中..'
  28. });
  29. var count = 0; // 记录多少条 用于处理循环跳出
  30. while (plus.android.invoke(cs, 'moveToNext')) {
  31. count++;
  32. console.log(count);
  33. let talist = {
  34. // 联系人姓名
  35. xm: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.CACHED_NAME)),
  36. telphone: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.NUMBER)),
  37. // 通话时长
  38. duration: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.DURATION)),
  39. date: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.DATE)),
  40. type: plus.android.invoke(cs, 'getString', plus.android.invoke(cs, 'getColumnIndex', CallLog.Calls.TYPE))
  41. };
  42. talist.date = this.parseTime(talist.date);
  43. switch (talist.type) {
  44. // 判断通话类型
  45. case '1':
  46. talist.type = ' 呼入 ';
  47. break;
  48. case '2':
  49. talist.type = ' 呼出 ';
  50. break;
  51. case '3':
  52. talist.type = ' 未接 ';
  53. break;
  54. default:
  55. talist.type = ' 挂断 ';
  56. break;
  57. }
  58. if (count > 100) {
  59. break;
  60. }
  61. this.telphoneLog.push(talist);
  62. }
  63. uni.hideLoading();
  64. },

permission.js中部分代码(参考插件App权限判断和提示 - DCloud 插件市场

  1. // Android权限查询
  2. function requestAndroidPermission(permissionID) {
  3. return new Promise((resolve, reject) => {
  4. plus.android.requestPermissions(
  5. [permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  6. function(resultObj) {
  7. var result = 0;
  8. for (var i = 0; i < resultObj.granted.length; i++) {
  9. var grantedPermission = resultObj.granted[i];
  10. console.log('已获取的权限:' + grantedPermission);
  11. result = 1
  12. }
  13. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  14. var deniedPresentPermission = resultObj.deniedPresent[i];
  15. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  16. result = 0
  17. }
  18. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  19. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  20. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  21. result = -1
  22. }
  23. resolve(result);
  24. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  25. // if (result != 1) {
  26. // gotoAppPermissionSetting()
  27. // }
  28. },
  29. function(error) {
  30. console.log('申请权限错误:' + error.code + " = " + error.message);
  31. resolve({
  32. code: error.code,
  33. message: error.message
  34. });
  35. }
  36. );
  37. });
  38. }
  1. // 跳转到**应用**的权限页面
  2. function gotoAppPermissionSetting() {
  3. if (isIos) {
  4. var UIApplication = plus.ios.import("UIApplication");
  5. var application2 = UIApplication.sharedApplication();
  6. var NSURL2 = plus.ios.import("NSURL");
  7. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  8. var setting2 = NSURL2.URLWithString("app-settings:");
  9. application2.openURL(setting2);
  10. plus.ios.deleteObject(setting2);
  11. plus.ios.deleteObject(NSURL2);
  12. plus.ios.deleteObject(application2);
  13. } else {
  14. // console.log(plus.device.vendor);
  15. var Intent = plus.android.importClass("android.content.Intent");
  16. var Settings = plus.android.importClass("android.provider.Settings");
  17. var Uri = plus.android.importClass("android.net.Uri");
  18. var mainActivity = plus.android.runtimeMainActivity();
  19. var intent = new Intent();
  20. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  21. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  22. intent.setData(uri);
  23. mainActivity.startActivity(intent);
  24. }
  25. }

2.读取短信权限

  1. async getMsgs() {
  2. //检查、获取授权
  3. var result = await permision.requestAndroidPermission('android.permission.READ_SMS');
  4. var strStatus;
  5. if (result == 1) {
  6. strStatus = '已获得授权';
  7. } else {
  8. uni.showToast({
  9. title: '读短信权限未开启',
  10. icon: 'none'
  11. });
  12. setTimeout(() => {
  13. uni.hideToast();
  14. }, 2000);
  15. }
  16. //获得短信内容
  17. var main = plus.android.runtimeMainActivity();
  18. var Uri = plus.android.importClass('android.net.Uri');
  19. var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
  20. var uri = Uri.parse('content://sms/');
  21. var cr = main.getContentResolver();
  22. plus.android.importClass(cr);
  23. var cur = cr.query(uri, null, null, null, null);
  24. plus.android.importClass(cur);
  25. cur.moveToFirst();
  26. uni.showLoading({
  27. title: '匹配短信记录中..'
  28. });
  29. // 这一块设置了拿到当前时间的前五分钟,默认是获取全部的短信信息
  30. // 小米系统默认拿到的不是全部短信信息,需要在权限中开启"通知类短信"这个权限才能拿到全部
  31. let newdata = new Date().getTime();
  32. // 拿到当前时间的前五分钟
  33. let fiveMinsAgo = new Date(newdata - 3 * 30 * 24 * 60 * 60 * 1000).getTime();
  34. var selection = 'date > ' + fiveMinsAgo;
  35. var cur = cr.query(uri, null, selection, null, null);
  36. while (cur.moveToNext()) {
  37. let newObj = {};
  38. // 发送人号码
  39. var index_Address = cur.getColumnIndex('address');
  40. var address = cur.getString(index_Address);
  41. newObj.telphone = address;
  42. // console.log('发送人号码:' + address);
  43. //短信内容
  44. var index_Body = cur.getColumnIndex('body');
  45. var body = cur.getString(index_Body);
  46. newObj.content = body;
  47. // console.log('短信内容:' + body);
  48. //短信类型1接收 2发送
  49. var index_Type = cur.getColumnIndex('type');
  50. var type = cur.getString(index_Type);
  51. newObj.type = type;
  52. // console.log('短信类型:' + type);
  53. // 发送日期
  54. var smsDate = cur.getString(cur.getColumnIndex('date'));
  55. smsDate = this.parseTime(smsDate);
  56. newObj.sendDate = smsDate;
  57. // console.log('发送日期:' + smsDate);
  58. this.msgList.push(newObj);
  59. }
  60. cur.close();
  61. uni.hideLoading();
  62. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/910405
推荐阅读
相关标签
  

闽ICP备14008679号