Phonegap不起作用Connection对象

时间:2013-07-29 08:59:58

标签: android cordova

我正在使用cordova和android平台。

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

我正在尝试使用Connection对象 我将下一个字符串添加到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

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

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

感谢。

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有同样的问题?

3 个答案:

答案 0 :(得分: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中的

答案 1 :(得分:1)

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

我在日志中注意到的是ERROR消息后面的以下行:

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 ,然后运行整个函数 500ms ,稍后给 CordovaNetworkManager 初始化 connection.type

答案 2 :(得分:0)

上述所有答案都是正确但不完整。

如果您仍然无法获取Connection,并且“在线”和“离线”事件无效,则表示该插件无法正常工作。

以下是我为使其发挥作用所采取的步骤:

在index.html中输入1 /而不是加载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 / 使一切正常