cordova 3.0:Android:未定义连接 [英] cordova 3.0: Android: Connection is not defined
问题描述
我第一次尝试使用Apache Cordova 3.0。
my first time experimenting with Apache Cordova 3.0.
已下载lib,解压缩cordova-android和cordova-js并创建了一个项目:
downloaded lib, unziped cordova-android and cordova-js and created a project:
./create ~/Documents/andriod-projects/HelloWorld com.x.HelloWorld HelloWorld
- OK
res / xml / config.xml
res/xml/config.xml
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
AndroidManifest.xml
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
on index.js设备就绪:
on index.js device ready:
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
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("Network: "+states[networkState]);
}
当我在我的orriod上模拟项目我在LogCat 错误:未定义连接
:
when I emulate the project on my andriod I got in LogCat Error:Connection is not defined
:
我缺少什么?我必须附加一个.js以便连接声明?
what I am missing? I have to attach a .js in order to Connection be declared?
推荐答案
未捕获的反射错误:未定义连接
Uncaught Refference error: Connection is not defined
与缺少连接对象相关,根据我对corodva 3.1.0的经验,变得可用,甚至在benka建议的延迟后。这个特殊的问题可以通过使用navigator.connection对象的常量来解决,如下所示:
is related to lack of a "Connection" object, which based on my experience with corodva 3.1.0 does not become available, even after a delay as benka suggested. This particular issue can be solved by using the constants of the navigator.connection object as below:
var states = {};
states[navigator.connection.UNKNOWN] = 'Unknown connection';
states[navigator.connection.ETHERNET] = 'Ethernet connection';
states[navigator.connection.WIFI] = 'WiFi connection';
states[navigator.connection.CELL_2G] = 'Cell 2G connection';
states[navigator.connection.CELL_3G] = 'Cell 3G connection';
states[navigator.connection.CELL_4G] = 'Cell 4G connection';
states[navigator.connection.CELL] = 'Cell generic connection';
states[navigator.connection.NONE] = 'No network connection';
不幸的是,在我的情况下,这只是在android上网络状态的问题的开始
unfortunately in my case this was only the beginning of issues with network status on android as
navigator.connection.type
$ b b
始终返回0,这是未知连接。两者在Android上的模拟器和一个设备。对我有用的解决方法是直接调用插件类:
would always return 0 which is Unknown connection. Both on the android emulator and a device. A workaround which works for me is to call the plugin class directly:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var conn = checkConnection();
alert("Connection:"+conn);
}
function checkConnection(){
var networkState;
var test = cordova.exec(
function(winParam) {networkState = winParam;},
function(error) {alert("Network Manager error: "+error);},
"NetworkStatus",
"getConnectionInfo",
[]
);
return networkState;
}
这个代码在函数中有一个丑陋的networkState赋值,可能会异步执行后的checkConnection返回语句,但作为本机代码返回PluginResult里面的execute函数 - 这工作。返回的networkState值与navigator.connection不匹配。常数如:
this code has an ugly networkState assignment inside a function that could potentially be executed asynchronously after the checkConnection return statement, but as the native code returns PluginResult inside the execute function - this works. The networkState value returned does not match the navigator.connection. constants like:
navigator.connection.WIFI
您可以在这里看到插件源代码中返回的值:https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java
You can see the values returned in the plugins source code here: https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java
这篇关于cordova 3.0:Android:未定义连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!