赞
踩
问题:获取手机定位权限,在用户点击拒绝后,再次点击定位按钮,手机无反应。这里,安卓系统2次拒绝之后,默认为禁止询问弹窗弹出,所以再点击定位肯定没有反应。
一、解决思路:第一想到,检查是否打开GPS功能(Android),打开了直接跳转到地图定位页面,关闭状态则跳转系统设置;
以下均为Android示例:
原代码:
- import { checkOpenGPSServiceByAndroid } from "@/utils/device";
- /*.
- .
- .
- */
- /*
- 定位按钮触发事件
- */
- onGPSAddress() {
- checkOpenGPSServiceByAndroid()
- // 获取当前位置
- uni.getLocation({
- type: "gcj02", //返回可以用于uni.openLocation的经纬度
- success: res => {
- let latitude = res.latitude;
- let longitude = res.longitude;
- uni.chooseLocation({
- latitude,
- longitude,
- success: data => {
- //此处仅返回详细地址和经纬度,所以要进行经纬度解析
- this.getCity(data.latitude, data.longitude);
- },
- fail: fail => {
- checkOpenGPSServiceByAndroid();
- }
- });
- }
- });
- },
- // 解析经纬度
- getCity(latitude, longitude) {
- let location = `${longitude},${latitude}`;
- var myAmapFun = new amapFile.AMapWX({
- key: amapKey,
- batch: true
- });
- myAmapFun.getRegeo({
- //如果经纬度有问题会导致不进入回调方法,从而报错
- location: location,
- success: e => {
- //成功回调
- console.log(e);
- let addressComponent = e[0].regeocodeData.addressComponent;
- let formatted_address = e[0].regeocodeData.formatted_address;
- this.detail = formatted_address;
- this.city = addressComponent.city;
- this.province = addressComponent.province;
- this.provinceCity = addressComponent.province + addressComponent.city;
- this.district = addressComponent.district;
- this.districtId = addressComponent.adcode;
- this.location = e[0].location;
- this.longitude = e[0].longitude;
- },
- fail: function (info) {
- //失败回调
- console.log(info);
- }
- });
- },
device.js文件:
- let system = uni.getSystemInfoSync(); // 获取系统信息
- /**检查是否打开GPS功能(android)**/
- export const checkOpenGPSServiceByAndroid = () => {
- if (system.platform === 'android') { // 判断平台
- var context = plus.android.importClass("android.content.Context");
- var locationManager = plus.android.importClass("android.location.LocationManager");
- var main = plus.android.runtimeMainActivity();
- var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
- if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
- uni.showModal({
- title: '提示',
- content: '请打开定位服务功能',
- success(res) {
- if (res.confirm) {
- if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
- var Intent = plus.android.importClass('android.content.Intent');
- var Settings = plus.android.importClass('android.provider.Settings');
- var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- main.startActivity(intent); // 打开系统设置GPS服务页面
- } else {
- console.log('GPS功能已开启');
- }
- }
- }
- });
- return
- }
- }
- }
-
- /* 该问题用不到下面的方法,请忽略 */
-
- /**
- * 检查通知是否开启
- */
- export const noticMsgTool = () => {
- //如果没有打开 则可以 去 请求打开
- if (system.platform == "ios") {
- //苹果打开对应的通知栏
- uni.showModal({
- title: "通知权限开启提醒",
- content: "您还没有开启通知权限,无法接受到消息通知,请前往设置!",
- confirmText: "去设置",
- success: function(res) {
- if (res.confirm) {
- var app = plus.ios.invoke("UIApplication", "sharedApplication");
- var setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
- plus.ios.invoke(app, "openURL:", setting);
- plus.ios.deleteObject(setting);
- plus.ios.deleteObject(app);
- }
- }
- });
- } else {
- //android打开对应的通知栏
- var main = plus.android.runtimeMainActivity();
- var pkName = main.getPackageName();
- var uid = main.getApplicationInfo().plusGetAttribute("uid");
- var Intent = plus.android.importClass('android.content.Intent');
- var Build = plus.android.importClass("android.os.Build");
- uni.showModal({
- title: "通知权限开启提醒",
- content: "您还没有开启通知权限,无法接受到消息通知,请前往设置!",
- confirmText: "去设置",
- success: function(res) {
- if (res.confirm) {
- if (Build.VERSION.SDK_INT >= 26) {
- var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
- intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
- } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
- var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
- intent.putExtra("app_package", pkName);
- intent.putExtra("app_uid", uid);
- } else { //(<21)其他--跳转到该应用管理的详情页
- var Settings = plus.android.importClass("android.provider.Settings");
- var Uri = plus.android.importClass("android.net.Uri");
- var intent = new Intent();
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- var uri = Uri.fromParts("package", main.getPackageName(), null);
- intent.setData(uri);
- }
- // 跳转到该应用的系统通知设置页
- main.startActivity(intent);
-
- }
- }
- });
- }
- }
度娘‘检测安卓是否开启GPS功能’后,代码均为上面5+方法,测试了PDA(安卓9.0版本)以上代码检测永远是已开启。传音手机(安卓10版本)以上代码检测永远是未开启,如果你用了上面的代码打开设置页面,会导致信息位置授权弹窗不会弹出。
二、解决思路:那检测用户操作弹窗拒绝、禁止、允许的事件,若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限;
device.js文件代码调整:
- let system = uni.getSystemInfoSync(); // 获取系统信息
- /**检查是否打开GPS功能(android)**/
- export const checkOpenGPSServiceByAndroid = () => {
- if (system.platform === 'android') { // 判断平台
-
- openGps();
- }
- }
- /**
- * 定位权限及弹出权限弹框 监听用户点击按钮
- * **/
- export const openGps = () => {
- plus.android.requestPermissions(
- ['android.permission.ACCESS_FINE_LOCATION'],
- function(resultObj) {
- console.log('resultObj---', resultObj);
- var result = 0;
- for (var i = 0; i < resultObj.granted.length; i++) {
- var grantedPermission = resultObj.granted[i];
- // console.log(6, '已获取的权限:' + grantedPermission);
- result = 1
- }
- for (var i = 0; i < resultObj.deniedPresent.length; i++) {
- var deniedPresentPermission = resultObj.deniedPresent[i];
- // console.log(5, '拒绝本次申请的权限:' + deniedPresentPermission);
-
- result = 0
- }
- for (var i = 0; i < resultObj.deniedAlways.length; i++) {
- var deniedAlwaysPermission = resultObj.deniedAlways[i];
- // console.log(4, '永久拒绝申请的权限:' + deniedAlwaysPermission);
-
- result = -1
- }
- // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
- if (result != 1) {
- //如果用户第一次拒绝后,跳转到**应用**的权限页面
- 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);
- }
- },
- function(error) {
- console.log('申请权限错误:' + error.code + " = " + error.message);
- resolve({
- code: error.code,
- message: error.message
- });
- }
- );
- }
-
每次用户拒绝后,均会跳转到设置权限,不太合理。
三、解决思路:用户拒绝2次之后,用户再次点击定位,提示去设置权限。且跳转到系统设置;
device.js文件代码调整:
- let system = uni.getSystemInfoSync(); // 获取系统信息
- /**检查是否打开GPS功能(android)**/
- export const checkOpenGPSServiceByAndroid = () => {
- if (system.platform === 'android') { // 判断平台
-
- openGps();
- }
- }
- /**
- * 定位权限及弹出权限弹框 监听用户点击按钮
- * **/
- let num = 0;
- export const openGps = () => {
- plus.android.requestPermissions(
- ['android.permission.ACCESS_FINE_LOCATION'],
- function(resultObj) {
- console.log('resultObj---', resultObj);
- var result = 0;
- for (var i = 0; i < resultObj.granted.length; i++) {
- var grantedPermission = resultObj.granted[i];
- // console.log(6, '已获取的权限:' + grantedPermission);
- result = 1
- }
- for (var i = 0; i < resultObj.deniedPresent.length; i++) {
- var deniedPresentPermission = resultObj.deniedPresent[i];
- // console.log(5, '拒绝本次申请的权限:' + deniedPresentPermission);
- num += 1
- result = 0
- }
- for (var i = 0; i < resultObj.deniedAlways.length; i++) {
- var deniedAlwaysPermission = resultObj.deniedAlways[i];
- // console.log(4, '永久拒绝申请的权限:' + deniedAlwaysPermission);
- num += 1
- result = -1
- }
- // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
- if (result != 1) {
- //如果用户第2次拒绝后,跳转到**应用**的权限页面
- if (num > 2) {
- uni.showToast({
- title: "请到系统设置打开定位服务功能!",
- icon: "none",
- duration: 1500
- })
- setTimeout(() => {
- 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);
- }, 2000)
- }
-
-
- }
- },
- function(error) {
- console.log('申请权限错误:' + error.code + " = " + error.message);
- resolve({
- code: error.code,
- message: error.message
- });
- }
- );
- }
安卓测试有效;
有误或不清楚的地方,欢迎大佬们讨论指正~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。