【问题标题】:Redirect to location settings using cordova in android在android中使用cordova重定向到位置设置
【发布时间】:2015-02-11 01:36:12
【问题描述】:

这是我试图在我的 cordova android 应用程序中实现的要求

  1. 当用户进入主页时,检查gps是否启用。

  2. 如果未启用,我想指示用户打开位置设置。

第一部分使用 GPS 检测器插件轻松完成,第二部分使用 Web Intent 插件实现。但它没有按我预期的那样工作。

if(!gps){
     //gps is disabled try to show the location setting using webintent plugin
    window.plugins.webintent.startActivity(
        {
            action: window.plugins.webintent.ACTION_LOCATION_SOURCE_SETTINGS,
        },
        function() {},
        function() {
            alert('Failed to open URL via Android Intent.');
            console.log("Failed to open URL via Android Intent. URL: " + theFile.fullPath)
        }
    );              
}

我收到此错误Failed to open URL via Android Intent

【问题讨论】:

  • 请帮帮我
  • 我遇到了同样的问题,但没有解决
  • 你用什么插件来检查gps?请问?
  • 我已经创建了自己的 gps 检测器插件,将在我的 github 中更新

标签: android cordova gps ionic-framework cordova-plugins


【解决方案1】:

您可以使用cordova-diagnostic-plugin 实现此目的。安装后,您可以通过 JS 调用它,例如:

cordova.plugins.diagnostic.switchToLocationSettings();

更新

您可以使用cordova-plugin-request-location-accuracy 直接在应用内请求高精度定位模式(即 GPS)。这将显示一个本机确认对话框,如果用户同意,GPS 将自动启用,要求用户手动更改设置:

function onRequestSuccess(success){
    console.log("Successfully requested accuracy: "+success.message);
}

function onRequestFailure(error){
    console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
    if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
        if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
            cordova.plugins.diagnostic.switchToLocationSettings();
        }
    }
}

cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);

【讨论】:

  • @Root,你是怎么做到的?
  • 没关系!虽然它是一个 phonegap 插件,最后一次提交是在 10 月 13 日,但它在 Ionic 1.0.0 中开箱即用
  • @FernandoFabreti 仅供参考,我最近分叉了那个 repo,以便添加文档并将插件发布到 npm 和 cordova 注册表 - 分叉的 repo 是 here
  • 太棒了!我注意到 Diagnostic.prototype.isGpsEnabled 在我的 Galaxy S4 设备上总是返回 true。您的文档说您的“相似”版本仅在启用高精度时才返回 true,您是否更改了它?将查看您的代码,谢谢。
  • 我没有更改我的 fork 中的代码 - 只是根据代码 cmets 添加了文档,所以可能是我的文档有误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
相关资源
最近更新 更多