应用程序在 requestLocationPermissions 之前崩溃

Application crashes before requestLocationPermissions

我在 Titanium 上使用 ti.map 进行申请。

来自Android6 权限系统正在改变。 我必须在安装时而不是在应用程序中授予权限 运行.

我编写了这些代码,但它是这样工作的。

如果您尝试显示地图,它会崩溃。但是警报(允许访问此设备的位置?)仍然存在。

我认为问题是,在 requestLocationPermissions 对话框结果 return 之前尝试打开地图时会发生崩溃。

但是,只有当用户尝试通过 Ti.Geolocation.requestLocationPermissions 打开地图时,才会触发 requestLocationPermissions 对话框。 所以不可能在打开地图之前先征得用户的同意。

综上所述

我想提前触发 requestLocationPermissions 回调。

所以这是我的代码。

if (Ti.Platform.osname === 'android'){
    var hasLocationPermissions = Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS);
    Ti.API.info('Ti.Geolocation.hasLocationPermissions : ' +  hasLocationPermissions);
    if (hasLocationPermissions) {
        Ti.API.info('You already have permission.');
    }
    else {
        Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
            Ti.API.info('Ti.Geolocation.requestLocationPermissions' +  e);
            if (e.success) {
                // Instead, probably call the same method you call if hasLocationPermissions() is true
                alert('You granted permission.');

            } else if (OS_ANDROID) {
                alert('You denied permission for now, forever or the dialog did not show at all because it you denied forever before.');

            } else {

        // We already check AUTHORIZATION_DENIED earlier so we can be sure it was denied now and not before
                Ti.UI.createAlertDialog({
                    title: 'You denied permission.',

                    // We also end up here if the NSLocationAlwaysUsageDescription is missing from tiapp.xml in which case e.error will say so
                    message: e.error
                }).show();
            }
        });
    }
}

下面是错误信息。

[ERROR] TiApplication: (main) [13751,15075] Sending event: exception on thread: main msg:java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION; Titanium 6.0.1,2016/12/19 16:51,undefined
[ERROR] TiApplication: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
[ERROR] TiApplication:  at com.google.maps.api.android.lib6.impl.az.c(:com.google.android.gms.DynamiteModulesB:50297)

将 targetsdk 版本 21 设置为 build.gradle。您的问题将得到解决。

在 Android 上调用不带参数的函数

Ti.Geolocation.hasLocationPermissions();

Documentation