PhoneGap的:的ReferenceError:连接没有定义 [英] PhoneGap: ReferenceError: Connection is not defined
问题描述
我忙着学习的PhoneGap和构建应用程序为Android。
I'm busy learning PhoneGap and building an application for Android.
得到了我的基本应用与设备准备工作的罚款。
Got the basic my-app with device ready working fine.
现在我想测试没有成功的网络连接。
Now I'm trying to test the network connection with no success.
我安装插件的网络信息:
I installed the plugin-network-information:
$ cordova plugin add org.apache.cordova.network-information<br>
$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git<br>
新增功能的config.xml:
Added feature to config.xml:
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>
一切OK了。
当我做了
$ phonegap local build android
它建立应用程序的罚款。
当我打开内置AndroidManifest.xml文件,下面的条目有:
It builds the app fine. When I open the built AndroidManifest.xml file, the following entry is there:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
当我运行应用程序,我得到一个的ReferenceError:连接没有定义
When I run the app, I get a "ReferenceError: Connection is not defined"
下面是我的code,我得到这个从的PhoneGap
Here is my code, I got this from PhoneGap:
try {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
} catch (e) {
alert(e);
}
有没有人有同样的问题,并希望解决的办法?
Has anyone had the same problem and hopefully a solution?
推荐答案
尝试添加一个超时功能延迟 navigator.connection.type
电话。我有同样的问题。这是我如何解决它:
Try to add a timeout function to delay the navigator.connection.type
call. I had the same issue. This is how i solved it:
首先做的:
var networkState = navigator.connection.type;
然后设置超时并运行 navigator.connection.type
又说:
setTimeout(function(){
networkState = navigator.connection.type; // have to do this second time to pick up the refreshed value
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}, 500);
我知道这很奇怪,但是这给时间(500毫秒在这个例子中),以navigator.connection.type刷新它的价值。的它的作用就像是一个异步调用。的
您可以用替换试验的 500毫秒的一个更大或更小的值。
You can experiment with replacing 500 ms with a bigger or smaller value.
这篇关于PhoneGap的:的ReferenceError:连接没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!