简体   繁体   中英

how to fire my online and offline events

I'm working with cordova 3.4. (android)

I set 4 events to fire that are :

  • deviceready
  • onResume
  • online
  • offline

the problem is that online and offline events are not firing at all.

according to the documentation, i set the onResume's listener before the deviceready and online/offline after it.

here is what i did.

script for the events :

function onLoad() {
document.addEventListener("online", onGoesOnLine, false);
document.addEventListener("offline", onGoesOffLine, false);
document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
document.addEventListener("resume", onResumeApp, false);
var db = window.openDatabase("Database", "1.0", "MondialRelay", 200000);
db.transaction(creerBaseDonneesSiNonExist, function(err){
console.log("erf ...");
 }, function(){
  console.log("hell yeah");
 });
}

function onGoesOnLine(){
 alert("onLine");
}

function onGoesOffLine(){
 alert("offLine");
}

function onResumeApp(){
 alert("onResume");
 window.location = "offLineIndex.html";
}

config.xml :

<?xml version='1.0' encoding='utf-8'?>
<widget id="fr.mondialrelay.appli" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<preference name="loglevel" value="DEBUG" />
<feature name="App">
    <param name="android-package" value="org.apache.cordova.App" />
</feature>
<name>MondialRelay</name>
<description>
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
</author>
<content src="index.html" />
<access origin="*" />
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>
<feature name="Device">
    <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
<feature name="Battery">
    <param name="android-package" value="org.apache.cordova.batterystatus.BatteryListener" />
</feature>
<plugin name="Storage" value="org.apache.cordova.Storage" />
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
</widget>

AndroidManifest.xml :

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="fr.mondialrelay.appli" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="MondialRelay" android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
</manifest>

here is what i see when i run the "cordova plugin ls" command:

[ 'org.apache.cordova.battery-status',
'org.apache.cordova.device',
'org.apache.cordova.geolocation',
'org.apache.cordova.network-information' ]

i don't think i'm missing anything but still it doesn't work ...

i know this question's has been asked many times already but even after reading the answers, i can't figure out how to make it work.

First, when i was testing i was putting the app as pause state to cut off my internet connexion, i thought that could be the problem .. i then set up a wifi with my phone and then, while the application running, i just shut it down .. no way to get any alert ..

thank you for helping ;) regards.

Nothing worked ... I did find a solution to that problem...

  • save your WWW folder somewhere.
  • go to the root of your project.
  • add all the plugins you'll need if not done yet
  • Cordova Build in your cordova CLI at the root of your project

then copy / paste the content of the www folder you saved before instead of the new one created by the cordova CLI (do not replace the cordova.js cordova_plugin.js and plugins folder)

then go to your eclipse IDE, compile, it should work :)

hope that helped. regards.

The online/offline events are made to fire on state change only. If you need to check the connectivity at a certain point, you can use navigator.network.connection.type.

initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

function onGoesOnLine(){
    alert("onLine");
},

function onGoesOffLine(){
  alert("offLine");
},

onDeviceReady: function() {        
    document.addEventListener("offline", onGoesOffLine, false);
    document.addEventListener("online", onGoesOnLine, false);
    if((navigator.network.connection.type).toUpperCase() == "NONE" && 
       (navigator.network.connection.type).toUpperCase() == "UNKNOWN") {
        onGoesOffLine();
    }
    else {
        onGoesOnLine();
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM