在android中使用cordova重定向到位置设置 [英] Redirect to location settings using cordova in android
问题描述
这是我试图在我的cordova android应用程序中实现的要求
Here is the requirements what i am trying to implement in my cordova android application
当用户进入主页时检查是否启用了 GPS.
When user enters the home page check to see if the gps is enabled or not.
如果未启用,我想指示用户打开位置设置.
If not enabled i want to point the user to turn on the location settings.
第一部分使用 GPS 检测器插件轻松制作,第二部分使用网络意图插件实现.但它没有按我预期的那样工作.
first part is made easily with GPS detector plugin and second part is implemented using the web intent plugin.But its not working as i expected.
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)
}
);
}
我收到此错误无法通过 Android Intent 打开 URL
.
推荐答案
您可以使用 实现此目的科尔多瓦诊断插件.安装后,您可以通过 JS 调用它,例如:
You can achieve this using the cordova-diagnostic-plugin. Once installed, you call it via JS something like:
cordova.plugins.diagnostic.switchToLocationSettings();
更新
您可以使用 cordova-plugin-request-location-accuracy 直接从应用程序内请求高精度定位模式(即 GPS).这将显示一个本地确认对话框,如果用户同意,GPS 将自动启用并要求用户手动更改设置:
You can use cordova-plugin-request-location-accuracy to request high accuracy location mode (i.e. GPS) directly from within the app. This will show a native confirm dialog and if user agrees, GPS will be enabled automatically with requiring user to manually change settings:
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);
这篇关于在android中使用cordova重定向到位置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!