Reputation: 705
I am using cordova and android platform.
var CORDOVA_JS_BUILD_LABEL = '3.0.0-0-ge670de9';
AVD platform 4.2.2 API level 17
I am trying to use Connection object I added next string into app/res/xml/config.xml
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
and next string into 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" />
my code
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');
},
result of first alert:
networkState = 0
the remaining part of code not works, it seems like object Connection not exists.
I have no idea what is wrong, can anybody to help me?
Thanks.
PS. I had used this example http://docs.phonegap.com/en/3.0.0/cordova_connection_connection.md.html#Connection
Logs:
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
Very strange with phonegap 2.9.0 all works perfect Does anybody have the same problem with phonegap 3.0.0 ?
Upvotes: 3
Views: 3709
Reputation: 196
All the answers above are kind of right but not complete.
If you are still not able to get the Connection and if your "online" and "offline" event are not working, it means that the plugin is not working properly.
Here are the steps I did to make it work:
1/ in your index.html instead of loading phonegap.js (and forget about using the phonegap cmd but use cordova instead)
<script type="text/javascript" src="cordova.js"></script>
2/ add the plugin if you didn't yet
cordova plugin add org.apache.cordova.network-information
3/ build your app (will erase your platforms/yourplatform/www/ directory by the /www one)
cordova build
The latest is what made the difference for me ! 2/ was adding the files to my project and editing my config.xml and androidmanifest.xml the right way, but was not adding the source files. Doing 3/ made everything work properly
Upvotes: 0
Reputation: 4742
Right, I got the same issue with Phonegap 3.0 on Android 4.2.2 Api 17.
What I have noticed in the logs is the following line right after the ERROR message:
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
So I was thinking that it actually looks like it was an async callback after successfully initializing Connection.type from CordovaNetworkManager() however it shouldn't be.
So I tried the following:
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);
I know it looks a bit stupid but works every time. What it does is first calls navigator.connection.type and then runs the whole function 500ms later giving time to CordovaNetworkManager to initialize the connection.type.
Upvotes: 1
Reputation: 583
I've noticed that your console log reports
Could not find cordova.js script tag. Plugin loading may fail. at file:///android_asset/www/phonegap.js:1836
This is a long thread about this error: https://github.com/phonegap/phonegap-cli/issues/134
As a temporary solution they suggest to change this line
<script type="text/javascript" src="phonegap.js"></script>
with
<script type="text/javascript" src="cordova.js"></script>
in index.html
Upvotes: 4