【问题标题】:Application crashes before requestLocationPermissions应用程序在 requestLocationPermissions 之前崩溃
【发布时间】:2017-03-02 13:54:47
【问题描述】:

我正在 Titanium 上使用 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)

【问题讨论】:

  • 是否已输入 Google API 密钥 来注册您的应用?这可能是崩溃的原因。
  • 是的,我已经输入了 Google API 密钥。并且地图在第一次崩溃后可以正常工作。
  • 这段代码对我来说似乎很好,我尝试了相同的代码并且我的应用程序运行正常。您能否使用您为加载地图而编写的代码更新问题。

标签: android geolocation titanium


【解决方案1】:

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

Ti.Geolocation.hasLocationPermissions();

Documentation

【讨论】:

  • 我已经添加了这个,Ti.map 在用户推送 OK 之前被调用。我的理想方式是出现权限提示 -> ti.map open。但是,即使我设置了Ti.Geolocation.hasLocationPermissions();,它也会在触发一些 ti.map 操作时显示弹出窗口。
  • 对不起,你是对的,我误解了这个问题。 Ti.Geolocation.hasLocationPermissions(); 在开始时出现弹出窗口;但是另一个窗口覆盖了对话框。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多