Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 navigator.online does not work in WebView

Tags:

html

android

I am creating a hybrid application by using android and offline html5 pages in android asset. navigator.online is not working in Android Webview.

Please help

like image 651
mercury Avatar asked Jan 31 '13 11:01

mercury


4 Answers

If anyone is still searching for this one. The solution for me was to register a broadcast receiver in my activity to detect connection change.

    @Override
protected void onPause() {
    super.onPause();

    if (connectivityChangeReceiver!=null) unregisterReceiver(connectivityChangeReceiver);
}

@Override
protected void onResume()
{
    super.onResume();

    IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(connectivityChangeReceiver,intentFilter);
}

private BroadcastReceiver connectivityChangeReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) 
    {
        mWebView.setNetworkAvailable(isNetworkAvailable(context));
    }
}; 
like image 134
user1732313 Avatar answered Oct 20 '22 19:10

user1732313


Make sure that you provide below permissions in the AndroidMenifest.xml file:

<uses-permission android:name="android.permission.INTERNET />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
like image 37
Fabian Fdz Avatar answered Oct 20 '22 20:10

Fabian Fdz


you might want to add the following line of code for your events

document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);

make sure you check the values in the functions function onOnline(){} & function onOffline(){}

like image 20
nsgulliver Avatar answered Oct 20 '22 19:10

nsgulliver


Are you sure you are not mistaking the spelling?

it should be navigator.onLine (camelCase)

like image 40
Moinkhan Avatar answered Oct 20 '22 20:10

Moinkhan