当前位置:   article > 正文

uniapp打包安卓app,获取app的权限数据,并弹窗提示设置app权限_uniapp 获取安卓有麦克风权限

uniapp 获取安卓有麦克风权限
  1. var androidPermissions = {
  2. camera: "android.permission.CAMERA", // 摄像头权限
  3. location: "android.permission.ACCESS_FINE_LOCATION", // 位置权限
  4. calendar: {
  5. read: "android.permission.READ_CALENDAR", // // 日历读取权限
  6. write: "android.permission.WRITE_CALENDAR", // // 日历写入权限
  7. },
  8. storage: {
  9. read: "android.permission.READ_EXTERNAL_STORAGE", // 存储读取权限
  10. write: "android.permission.WRITE_EXTERNAL_STORAGE", // 存储写入权限
  11. },
  12. record: "android.permission.RECORD_AUDIO", // 麦克风权限
  13. contact: {
  14. read: "android.permission.READ_CONTACTS", // 联系人读取权限
  15. write: "android.permission.WRITE_CONTACTS", // 联系人写入权限
  16. },
  17. sms: {
  18. read: "android.permission.READ_SMS", // 短信读取权限
  19. send: "android.permission.SEND_SMS", //短信发送权限
  20. receive: "android.permission.RECEIVE_SMS", // 短信接收权限
  21. },
  22. state: "android.permission.READ_PHONE_STATE", // 手机识别码权限
  23. phone: "android.permission.CALL_PHONE", // 拨打电话权限
  24. log: "android.permission.READ_CALL_LOG", // 通话记录权限
  25. };
  26. export default {
  27. onLaunch: function() {
  28. console.log('App Launch')
  29. let _this = this;
  30. // 获取app的权限状态数据
  31. let MainActivity = plus.android.runtimeMainActivity();
  32. let PermissionCheck = MainActivity.checkSelfPermission('android.permission.READ_EXTERNAL_STORAGE')
  33. console.log('MainActivity:', JSON.stringify(PermissionCheck))
  34. // PermissionCheck为0:有权限,为-1:没有权限
  35. if (PermissionCheck != 0) {
  36. uni.showModal({
  37. title: '提示',
  38. content: '应用保存运行状态等信息,需要获取读写手机存储(系统提示为访问设备上的照片、媒体内容和文件)权限,请允许。',
  39. // showCancel: false, // 不显示取消按钮
  40. success(res) {
  41. if (res.confirm) {
  42. _this.androidPermissionCheck('storage')
  43. }
  44. }
  45. });
  46. }
  47. },
  48. onShow: function() {
  49. console.log('App Show')
  50. },
  51. onHide: function() {
  52. console.log('App Hide')
  53. },
  54. methods: {
  55. getPermissionList(permissionID) {
  56. const id = androidPermissions[permissionID];
  57. const list = [];
  58. if (typeof id == "string") {
  59. list[0] = id;
  60. } else {
  61. for (let key in id) {
  62. list.push(id[key]);
  63. }
  64. }
  65. return list;
  66. },
  67. androidPermissionCheck(permissionID) {
  68. return new Promise((resolve, reject) => {
  69. if (plus) {
  70. let ids = this.getPermissionList(permissionID);
  71. // 调用plus.android.requestPermissions后就默认获取到了手机权限
  72. plus.android.requestPermissions(
  73. ids,
  74. function(result) {
  75. console.log('')
  76. let res = 0;
  77. for (let i = 0; i < result.granted.length; i++) {
  78. let permission = result.granted[i];
  79. console.log("已获取的权限:", permission);
  80. res = 1;
  81. }
  82. for (let i = 0; i < result.deniedPresent.length; i++) {
  83. let permission = result.deniedPresent[i];
  84. console.log("本次已拒绝的权限:", permission);
  85. res = 0;
  86. }
  87. for (let i = 0; i < result.deniedAlways.length; i++) {
  88. let permission = result.deniedAlways[i];
  89. console.log("永久拒绝的权限:", permission);
  90. res = -1;
  91. }
  92. resolve({
  93. code: 1,
  94. data: res,
  95. });
  96. },
  97. (error) => {
  98. reject({
  99. code: 2,
  100. data: `code:${error.code},msg:${error.message}!`,
  101. });
  102. }
  103. );
  104. }
  105. });
  106. }
  107. }
  108. }

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

闽ICP备14008679号