使用android中的cordova重定向到位置设置

Ble*_*ien 5 gps android cordova ionic-framework cordova-plugins

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

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

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

第一部分是使用GPS探测器插件轻松制作的,第二部分是使用网络意图插件实现的.但它没有像我预期的那样工作.

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)
        }
    );              
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误Failed to open URL via Android Intent.

Dav*_*den 15

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

cordova.plugins.diagnostic.switchToLocationSettings();
Run Code Online (Sandbox Code Playgroud)

UPDATE

您可以使用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);
Run Code Online (Sandbox Code Playgroud)