Android M(API 23) - Phonegap权限问题

时间:2016-06-22 22:24:21

标签: cordova phonegap-plugins android-6.0-marshmallow android-permissions

我升级了我的应用程序以支持Android M(6),但即使我知道Android 6(API 23)中有关权限的要求,我也遇到了无法解决的问题。

我正在使用PhoneGap 6.2.6和地理定位插件,它应该提供(as far as I see) ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION 权限。

但是当我运行我的应用程序并打电话扫描BLE支持的设备时,我收到以下错误。

Caught a RuntimeException from the binder stub implementation.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:772)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onClientRegistered(BluetoothLeScanner.java:324)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:56)
at android.os.Binder.execTransact(Binder.java:453)

我无法得到的一件事是我应该如何要求用户授予权限?这不是插件的任务,地理定位吗?

PS:当我手动给出" geolocation"来自应用程序设置的许可,一切正常。

我的config.xml中有Geolocation功能;

config.xml中

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.apps.poc1" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <preference name="loglevel" value="DEBUG" />
    <feature name="Battery">
        <param name="android-package" value="org.apache.cordova.batterystatus.BatteryListener" />
    </feature>
    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature>
    <feature name="File">
        <param name="android-package" value="org.apache.cordova.file.FileUtils" />
        <param name="onload" value="true" />
    </feature>
    <feature name="Capture">
        <param name="android-package" value="org.apache.cordova.mediacapture.Capture" />
    </feature>
    <feature name="Contacts">
        <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
    </feature>
    <feature name="Device">
        <param name="android-package" value="org.apache.cordova.device.Device" />
    </feature>
    <feature name="Accelerometer">
        <param name="android-package" value="org.apache.cordova.devicemotion.AccelListener" />
    </feature>
    <feature name="Compass">
        <param name="android-package" value="org.apache.cordova.deviceorientation.CompassListener" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
    </feature>
    <feature name="FileTransfer">
        <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
    </feature>
    <feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.geolocation.Geolocation" />
    </feature>
    <feature name="Globalization">
        <param name="android-package" value="org.apache.cordova.globalization.Globalization" />
    </feature>
    <feature name="InAppBrowser">
        <param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser" />
    </feature>
    <feature name="Media">
        <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
    </feature>
    <feature name="NetworkStatus">
        <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
    </feature>
    <feature name="SplashScreen">
        <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
        <param name="onload" value="true" />
    </feature>
    <feature name="StatusBar">
        <param name="android-package" value="org.apache.cordova.statusbar.StatusBar" />
        <param name="onload" value="true" />
    </feature>
    <feature name="Vibration">
        <param name="android-package" value="org.apache.cordova.vibration.Vibration" />
    </feature>
    <feature name="Whitelist">
        <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
        <param name="onload" value="true" />
    </feature>
    <feature name="AndroiDiamond">
        <param name="android-package" value="com.androidiamond.AndroiDiamond" />
    </feature>
    <feature name="FileOpener2">
        <param name="android-package" value="io.github.pwlin.cordova.plugins.fileopener2.FileOpener2" />
    </feature>
    <icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />
    <icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />
    <icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />
    <icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />
    <icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />
    <icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />
    <splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />
    <splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />
    <splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />
    <splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />
    <splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />
    <splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />
    <splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />
    <splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />
    <splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />
    <splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />
    <splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />
    <splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />
    <allow-intent href="market:*" />
    <name>poc1</name>
    <description>
        Hello World sample application that responds to the deviceready event.
    </description>
    <author email="support@phonegap.com" href="http://phonegap.com">
        PhoneGap Team
    </author>
    <content src="index.html" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="android-minSdkVersion" value="14" />
    <icon src="icon.png" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
</widget>

2 个答案:

答案 0 :(得分:2)

您为Phonegap Build文档提供的链接适用于v2.9.0,它实际上是陈旧且过时的。如果您查看latest documentation,则会看到the feature tag has been deprecated<feature>仅向AndroidManifest.xml添加权限,这不足以使用Android M运行时权限。如上所述,您需要将<feature>标记替换为等效的plugins,其中还包含处理请求Android M运行时权限的代码。

因此,例如,替换:

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.Geolocation" />
</feature>

使用:

<plugin name="cordova-plugin-geolocation" spec="*"/>

您还需要添加<preference name="phonegap-version" value="cli-6.1.0" />,这将指示Phonegap Build使用Phonegap CLI v6.1.0。这对Android M运行时权限很重要,因为CLI版本会影响构建应用程序的平台版本。 Android M支持仅在Android平台v5中添加,但current default for Phonegap Build是CLI v5.2.0,默认为Android平台v4.1.1

答案 1 :(得分:0)

使用android 6时,权限系统发生了一些变化:

检查documentation about themthis related article on android security and permissions