Android 应用程序在requestLocationPermissions之前崩溃

Android 应用程序在requestLocationPermissions之前崩溃,android,geolocation,titanium,Android,Geolocation,Titanium,我正在使用钛上的ti.map制作应用程序 来自Android6 权限系统正在改变。 我必须在应用程序运行时授予权限,而不是在安装时授予权限 我编写了这些代码,但它是这样工作的 第一次发射时 如果您试图显示地图,它会崩溃。但是警报(允许访问此设备的位置?)仍然存在 第二次发射 如果在第一次启动时触碰“是”。它可以正常工作并正确显示地图 我认为问题是,在返回requestLocationPermissions对话框的结果之前尝试打开映射时会发生崩溃 但是,只有当用户尝试通过Ti.Geoloc

我正在使用钛上的ti.map制作应用程序

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

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

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

  • 第二次发射 如果在第一次启动时触碰“是”。它可以正常工作并正确显示地图
我认为问题是,在返回requestLocationPermissions对话框的结果之前尝试打开映射时会发生崩溃

但是,只有当用户尝试通过
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();

是否已输入Google API密钥以注册您的应用程序?这可能是崩溃的原因。是的,我已经输入了Google API密钥。在第一次崩溃后,map可以正常工作。这个代码对我来说似乎很好,我尝试了相同的代码,我的应用程序工作正常。你能用你为加载地图而编写的代码更新这个问题吗?我已经添加了这个,Ti.Map在用户按下OK之前被调用。我的理想方式是出现权限提示->ti.map打开。但是,即使我设置了
Ti.Geolocation.hasLocationPermissions()当一些ti.map动作触发时显示弹出窗口。对不起,你是对的,我误解了这个问题。开始时,弹出窗口由Ti.Geolocation.hasLocationPermissions()显示;然而,另一个窗口覆盖了对话框。