javascript - Offline Event In Android Using PhoneGap -
hi, created app in data server want if internet not connected user not able use app. put
document.addeventlistener("deviceready", function(){ ondeviseready(); }, false); function ondeviseready() { document.addeventlistener("offline", offline, false); } function offline() { navigator.notification.alert( 'no internet connected',message,'message','done'); }
now should in function message(){}
user not able move here until user connected internet put in alert box in message function not want
preface
your app
needs internet connection run, should check either device
connected internet
or not. can create utility
function (say hasconnection
) returns boolean true
on internet connection or boolean false
on no internet connection.
the hasconnection
function
function hasconnection() { var networkstate = navigator.network.connection.type; if(networkstate === connection.none) { return false; } return true; }
and depending on hasconnction
return value can take right decision.
sample example
document.addeventlistener('deviceready',ondeviceready, false); function ondeviceready(){ if(!hasconnection()){ //there no internet connection navigator.notification.alert( 'no internet connection!', // message function(){ /* if using jquery mobile ui can create seperate page #no-connection-page , display page : $.mobile.changepage('#no-connection-page',{'chagehash':false}); */ }, // callback 'connection required', // title 'ok' // buttonname ); return false; } else { //there internet connection, data server , display end user //again, if using jquery mobile, display page should displayed when internet connection available //$.mobile.changepage('#has-connection-page'); } /* if device connected internet while app running, can add listener 'online' event , take action. example : */ document.addeventlistener('online', function(){ //now device has internet connection //you can display '#has-connection-page' : //$.mobile.changepage('#has-connection-page'); }); //you can use listener 'offline' event track app if connection has gone while app running. }
one note
make sure have :
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />
in android manifest.
at last
i creating android app
using phonepage
/ cordova
, jquery-mobile
needs internet connection , using approach, working fine me. hope helps you.
Comments
Post a Comment