【问题标题】:Cordova diagnostic plugin not workingCordova 诊断插件不起作用
【发布时间】:2017-07-13 05:38:45
【问题描述】:

我正在使用诊断插件来获取有关位置是否打开的信息。由于代码中的一些问题,该插件工作正常,直到我将其删除。

现在,当我在同一个应用程序中再次添加插件时,它无法正常工作。

相同的插件在我为演示目的创建的另一个应用程序中工作。

我也尝试过旧版诊断插件。

function checkLocationEnabled(){
    cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
        alert("Location is " + (enabled ? "enabled" : "disabled"));
        alert(enabled);
        if(enabled == false){
            cordova.plugins.locationAccuracy.canRequest(function(canRequest){
                if(canRequest){
                    cordova.plugins.locationAccuracy.request(function(){
                        alert("GPS turned on");
                        setLatitudeLongitude();
                    }, function (error){
                        alert("Request failed");
                        if(error){
                            // Android only
                            alert("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_PRIORITY_HIGH_ACCURACY // iOS will ignore this
                    );
                }
            });
        } else if (enabled == true){
            setLatitudeLongitude();
        }
    }, function(error){
        alert("The following error occurred: "+error);
    });
}

我的 config.xml 文件中的条目

<plugin name="cordova.plugins.diagnostic" spec="~3.6.5" />

Cordova 版本:6.5.0

已安装平台: 安卓 6.2.3 浏览器 4.1.0 ios 4.3.1

<preference name="android-minSdkVersion" value="14" />

尝试在 chrome 上调试,它显示错误“无法读取未定义的属性诊断”。

此外,我使用的是 Windows 7,并且在不同版本的 android 5、5.1、6.0 上测试了相同的应用程序。 (三星+索尼)

它以前可以工作并显示“位置已启用/已禁用”的警报。

【问题讨论】:

  • 您需要比“它不起作用”更具体。描述预期与实际行为。提供您的测试环境的详细信息,例如运行 Android 7.1.1 的设备。如果有 JS 或原生的错误信息产生,请贴出来:尝试连接 Chrome Dev Tools 到 Webview 来查看 JS 错误并在代码中放置断点来逐步调试。
  • @DaveAlden 做到了

标签: android cordova phonegap-plugins phonegap


【解决方案1】:

显示未定义的错误“无法读取属性 isLocationEnabled”。

这表示cordova.plugins.diagnostic 对象不可用。

这最常见的原因是在 deviceready 事件触发之前尝试调用插件,因为 Cordova 在运行时动态加载插件的 JS 组件。

另一个可能的原因是插件没有正确安装到项目中 - 如果 diagnostic.js 在 Sources 中列出,请检查 Chrome 开发工具。如果不存在,请执行 cordova platform rm android &amp;&amp; cordova platform add android 以重建原生 Android 项目。

【讨论】:

  • 我检查了浏览器,诊断.js 没有在源代码中列出。我也确实删除了android平台并再次添加了它。还是同样的问题。应用程序中的插件文件夹包含 cordova.plugins.diagnostic 并且在 config.xml 中有一个关于它的条目,但它仍然无法正常工作。
  • I checked the browser - 你的意思是 Chrome DevTools 连接到 Android 设备 (cordova run android),对吗?因为该插件当然不能在浏览器环境(cordova run browser)中工作,因为它需要原生移动环境
  • 是的,我再次删除了平台 android 并再次添加。将我的手机与 DevTools 'cordova run android' 连接起来。插件第一次正常工作并出现“位置已禁用”警报。当我第二次启动该应用程序时,它再次无法正常工作。控制台显示“无法读取未定义的属性诊断”。
  • 最后通过在设备就绪函数中调用这个函数来解决它。谢谢@DaveAlden
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多