java.lang.SecurityException:我的位置需要权限 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION
java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
我对我的代码有疑问。
所以,我正在使用 nativescript-geolocation 获取我的位置。
在 component.ts 我有这个代码:
import * as geoLocation from "nativescript-geolocation";
currentGeoLocation: any;
ngOnInit(): void {
geoLocation.isEnabled().then(enabled => {
if (!enabled) {
geoLocation.enableLocationRequest().then(() => geoLocation.watchLocation(location => {
this.currentGeoLocation = location;
this.mapView.longitude = this.currentGeoLocation.longitude;
this.mapView.latitude = this.currentGeoLocation.latitude;
this.mapView.zoom = 15;
console.log(this.currentGeoLocation)
}, error => {
alert(error);
}, {
desiredAccuracy: 3,
updateDistance: 10,
minimumUpdateTime: 1000 * 1
}));
} else {
geoLocation.watchLocation(location => {
this.currentGeoLocation = location;
this.mapView.longitude = this.currentGeoLocation.longitude;
this.mapView.latitude = this.currentGeoLocation.latitude;
this.mapView.zoom = 15;
console.log(this.currentGeoLocation)
}, error => {
alert(error);
}, {
desiredAccuracy: 3,
updateDistance: 10,
minimumUpdateTime: 1000 * 1
});
}
});
}
在 AndroidMainfest.xml 我有:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
安装应用程序时显示 error
在AndroidM,需要申请运行次权限
Android架构
根据 Android 文档:
Android 6.0 Marshmallow introduced a new permissions model that lets
apps request permissions from the user at runtime, rather than prior
to installation. Apps that support the new model request permissions
when the app actually requires the services or data protected by the
services. While this doesn't (necessarily) change overall app
behavior, it does create a few changes relevant to the way sensitive
user data is handled:
如果用户 运行ning Android 6.0(API 级别 23)或更高版本,用户必须在 运行 时向您的应用授予权限宁的应用程序。
所以需要添加运行次权限
import { Component, OnInit } from "@angular/core";
import * as Permissions from "nativescript-permissions";
declare var android: any;
@Component({
selector: "ns-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public getCameraPermission() {
Permissions.requestPermission(android.Manifest.permission.CAMERA, "Needed for connectivity status").then(() => {
console.log("Permission granted!");
}).catch(() => {
console.log("Permission is not granted (sadface)");
});
}
}
- 这里有一些从用户那里获得运行时权限的简单步骤。
1) 在 build.gradle 文件中添加以下实现。
implementation 'com.karumi:dexter:5.0.0'
2) 添加以下代码以从用户那里获得您想要的位置的权限。
Dexter.withActivity(activity)
.withPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
Log.i(TAG, "onPermissionsChecked: ");
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
// All permissions are granted!
// Do what you want to do.
} else {
// Some permissions are not granted!
Toast.makeText(activity, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
}
})
.onSameThread()
.check();
Manifest.permission.ACCESS_FINE_LOCATION 和 Manifest.permission.ACCESS_COARSE_LOCATION 都用于获取位置。
函数:-
if (report.areAllPermissionsGranted()) {
// Write code to get Location.
}
这个功能是检查用户是否授予权限。如果是,那么它会进入。然后编写您的代码以从用户那里获取位置。
我对我的代码有疑问。
所以,我正在使用 nativescript-geolocation 获取我的位置。
在 component.ts 我有这个代码:
import * as geoLocation from "nativescript-geolocation";
currentGeoLocation: any;
ngOnInit(): void {
geoLocation.isEnabled().then(enabled => {
if (!enabled) {
geoLocation.enableLocationRequest().then(() => geoLocation.watchLocation(location => {
this.currentGeoLocation = location;
this.mapView.longitude = this.currentGeoLocation.longitude;
this.mapView.latitude = this.currentGeoLocation.latitude;
this.mapView.zoom = 15;
console.log(this.currentGeoLocation)
}, error => {
alert(error);
}, {
desiredAccuracy: 3,
updateDistance: 10,
minimumUpdateTime: 1000 * 1
}));
} else {
geoLocation.watchLocation(location => {
this.currentGeoLocation = location;
this.mapView.longitude = this.currentGeoLocation.longitude;
this.mapView.latitude = this.currentGeoLocation.latitude;
this.mapView.zoom = 15;
console.log(this.currentGeoLocation)
}, error => {
alert(error);
}, {
desiredAccuracy: 3,
updateDistance: 10,
minimumUpdateTime: 1000 * 1
});
}
});
}
在 AndroidMainfest.xml 我有:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
安装应用程序时显示 error
在AndroidM,需要申请运行次权限
Android架构
根据 Android 文档:
Android 6.0 Marshmallow introduced a new permissions model that lets apps request permissions from the user at runtime, rather than prior to installation. Apps that support the new model request permissions when the app actually requires the services or data protected by the services. While this doesn't (necessarily) change overall app behavior, it does create a few changes relevant to the way sensitive user data is handled:
如果用户 运行ning Android 6.0(API 级别 23)或更高版本,用户必须在 运行 时向您的应用授予权限宁的应用程序。 所以需要添加运行次权限
import { Component, OnInit } from "@angular/core";
import * as Permissions from "nativescript-permissions";
declare var android: any;
@Component({
selector: "ns-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public getCameraPermission() {
Permissions.requestPermission(android.Manifest.permission.CAMERA, "Needed for connectivity status").then(() => {
console.log("Permission granted!");
}).catch(() => {
console.log("Permission is not granted (sadface)");
});
}
}
- 这里有一些从用户那里获得运行时权限的简单步骤。
1) 在 build.gradle 文件中添加以下实现。
implementation 'com.karumi:dexter:5.0.0'
2) 添加以下代码以从用户那里获得您想要的位置的权限。
Dexter.withActivity(activity)
.withPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
Log.i(TAG, "onPermissionsChecked: ");
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
// All permissions are granted!
// Do what you want to do.
} else {
// Some permissions are not granted!
Toast.makeText(activity, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
}
})
.onSameThread()
.check();
Manifest.permission.ACCESS_FINE_LOCATION 和 Manifest.permission.ACCESS_COARSE_LOCATION 都用于获取位置。
函数:-
if (report.areAllPermissionsGranted()) { // Write code to get Location. }
这个功能是检查用户是否授予权限。如果是,那么它会进入。然后编写您的代码以从用户那里获取位置。