3

我正在使用cordova和android平台。

var CORDOVA_JS_BUILD_LABEL = '3.0.0-0-ge670de9';
AVD platform 4.2.2 API level 17

我正在尝试使用连接对象,我将下一个字符串添加到 app/res/xml/config.xml

<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />

并将下一个字符串放入 app/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" />

我的代码

    onDeviceReady: function() {
    var networkState = navigator.connection.type;

    alert('networkState = '+networkState);

    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]);

    app.receivedEvent('deviceready');
},

第一次警报的结果: networkState = 0

代码的其余部分不起作用,似乎对象连接不存在。

我不知道出了什么问题,有人可以帮助我吗?

谢谢。

PS。我用过这个例子http://docs.phonegap.com/en/3.0.0/cordova_connection_connection.md.html#Connection

日志:

06-26 16:00:29.245: D/AndroidRuntime(11703): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-26 16:00:29.245: D/AndroidRuntime(11703): CheckJNI is ON
06-26 16:00:29.245: D/dalvikvm(11703): Trying to load lib libjavacore.so 0x0
06-26 16:00:29.255: D/dalvikvm(11703): Added shared lib libjavacore.so 0x0
06-26 16:00:29.255: D/dalvikvm(11703): Trying to load lib libnativehelper.so 0x0
06-26 16:00:29.255: D/dalvikvm(11703): Added shared lib libnativehelper.so 0x0
06-26 16:00:29.295: D/AndroidRuntime(11703): Calling main entry com.android.commands.pm.Pm
06-26 16:00:29.295: D/AndroidRuntime(11703): Shutting down VM
06-26 16:00:29.295: D/dalvikvm(11703): GC_CONCURRENT freed 95K, 18% free 454K/552K, paused 0ms+0ms, total 1ms
06-26 16:00:29.295: D/dalvikvm(11703): Debugger has detached; object registry had 1 entries
06-26 16:00:29.565: D/AndroidRuntime(11715): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
06-26 16:00:29.565: D/AndroidRuntime(11715): CheckJNI is ON
06-26 16:00:29.565: D/dalvikvm(11715): Trying to load lib libjavacore.so 0x0
06-26 16:00:29.565: D/dalvikvm(11715): Added shared lib libjavacore.so 0x0
06-26 16:00:29.565: D/dalvikvm(11715): Trying to load lib libnativehelper.so 0x0
06-26 16:00:29.565: D/dalvikvm(11715): Added shared lib libnativehelper.so 0x0
06-26 16:00:29.605: D/AndroidRuntime(11715): Calling main entry com.android.commands.am.Am
06-26 16:00:29.605: D/dalvikvm(11715): Note: class Landroid/app/ActivityManagerNative; has 156 unimplemented (abstract) methods
06-26 16:00:29.605: I/ActivityManager(1195): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.app/._app} from pid 11715
06-26 16:00:29.605: W/WindowManager(1195): Failure taking screenshot for (246x410) to layer 21010
06-26 16:00:29.625: D/CordovaWebView(11304): CordovaWebView is running on device made by: unknown
06-26 16:00:29.625: D/JsMessageQueue(11304): Set native->JS mode to 2
06-26 16:00:29.645: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.645: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.655: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.675: D/AndroidRuntime(11715): Shutting down VM
06-26 16:00:29.675: D/dalvikvm(11715): GC_CONCURRENT freed 96K, 18% free 483K/584K, paused 0ms+0ms, total 1ms
06-26 16:00:29.675: D/dalvikvm(11715): Debugger has detached; object registry had 1 entries
06-26 16:00:29.675: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.695: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.695: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.745: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.745: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.809: I/Web Console(11304): Could not find cordova.js script tag. Plugin loading may fail. at file:///android_asset/www/phonegap.js:1836
06-26 16:00:29.826: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.826: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:29.855: I/ActivityManager(1195): Displayed com.example.app/._app: +246ms
06-26 16:00:29.965: D/dalvikvm(11304): GC_CONCURRENT freed 403K, 19% free 3472K/4264K, paused 9ms+0ms, total 11ms
06-26 16:00:30.365: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
06-26 16:00:30.365: I/Choreographer(11304): Skipped 103 frames!  The application may be doing too much work on its main thread.
06-26 16:00:30.375: I/Choreographer(1195): Skipped 59 frames!  The application may be doing too much work on its main thread.
06-26 16:00:34.765: D/chromium(11304): Unknown chromium error: -6
06-26 16:00:34.765: D/chromium(11304): Unknown chromium error: -6
06-26 16:00:34.785: E/Web Console(11304): Uncaught ReferenceError: Connection is not defined at file:///android_asset/www/js/index.js:38
06-26 16:00:34.785: E/AudioFlinger(798): no more track names available
06-26 16:00:34.785: E/AudioTrack(1195): AudioFlinger could not create track, status: -12
06-26 16:00:34.785: E/SoundPool(1195): Error creating AudioTrack
06-26 16:00:34.856: W/InputMethodManagerService(1195): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b561e548 attribute=null, token = android.os.BinderProxy@b5613ec0

phonegap 2.9.0 很奇怪,一切正常 有人对 phonegap 3.0.0 有同样的问题吗?

4

3 回答 3

4

我注意到您的控制台日志报告

Could not find cordova.js script tag. Plugin loading may fail. at file:///android_asset/www/phonegap.js:1836

这是关于此错误的长线程: https ://github.com/phonegap/phonegap-cli/issues/134

作为临时解决方案,他们建议更改此行

<script type="text/javascript" src="phonegap.js"></script>

<script type="text/javascript" src="cordova.js"></script>

在 index.html 中

于 2013-09-17T20:26:47.400 回答
1

是的,我在Android 4.2.2 Api 17上遇到了与Phonegap 3.0相同的问题。

我在日志中注意到的是错误消息之后的以下行:

10-11 14:31:40.360: E/Web Console(): Uncaught ReferenceError: Connection is not defined
10-11 14:31:40.380: D/CordovaNetworkManager(): Connection Type: wifi

所以我认为它实际上看起来像是从CordovaNetworkManager()成功初始化 Connection.type 后的异步回调,但它不应该是。

所以我尝试了以下方法:

var networkState = navigator.connection.type;

setTimeout(function(){
    networkState = navigator.connection.type;
    alert('networkState = '+networkState);

    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);

我知道它看起来有点愚蠢,但每次都有效。它所做的是首先调用navigator.connection.type ,然后在500 毫秒后运行整个函数,给CordovaNetworkManager时间来初始化connection.type

于 2013-10-11T13:47:45.030 回答
0

上面所有的答案都是正确的,但并不完整。

如果您仍然无法获得连接,并且您的“在线”和“离线”事件无法正常工作,则意味着插件无法正常工作。

以下是我为使其工作而采取的步骤:

1/ 在您的 index.html 中,而不是加载 phonegap.js(忘记使用 phonegap cmd,而是使用 cordova)

<script type="text/javascript" src="cordova.js"></script>

2/如果你还没有添加插件

cordova plugin add org.apache.cordova.network-information

3/构建你的应用程序(将删除你的平台/你的平台/ www /目录由/www之一)

cordova build

最新的是什么让我与众不同!2/正在将文件添加到我的项目并以正确的方式编辑我的 config.xml 和 androidmanifest.xml,但没有添加源文件。做3/使一切正常

于 2014-01-03T15:18:23.050 回答