安装插件
cordova plugin add cordova-plugin-android-permissions
导入
var permissions = (window as any).cordova.plugins.permissions;
使用
permissions.checkPermission(
permissions.ACCESS_COARSE_LOCATION,
(status: any) => {
if (!status.hasPermission) { // status.hasPermission 为false表示未打开定位 true表达已打开定位
// 申请权限
permissions.requestPermission(
permissions.ACCESS_COARSE_LOCATION,
(status: any) => {
if (!status.hasPermission) {
// 申请失败
}
},
() => {
// 申请失败
}
);
return;
}
// 打开权限之后的操作
}
);
config.xml
<config-file parent="/manifest" target="AndroidManifest.xml"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
</config-file>
platforms/android/app/src/main/AndroidManifest.xml
<manifest android:hardwareAccelerated="true" android:versionCode="10908" android:versionName="1.9.8" package="com.sucsoft.jnsw" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
<uses-feature android:name="android.hardware.location.gps" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
参考: https://blog.csdn.net/qq_20663175/article/details/80368176