简体   繁体   中英

cordova-android & phonegap - check from Java if there is Internet available

I'm working with the Android's SDK, PhoneGap, Eclipse.

Short story: how can I check from cordova-android/phonegap (Java) if there is Internet available?

Long story: Im working in a PhoneGap app that shows my website doing a window.location = " http://mydomain.com ".

If there's no internet and a user clicks on a link, I need to prevent this link from being loaded (and hence the browser error).

I decided to override the shouldOverrideUrlLoading method of the CordovaWebViewClient.

So I need to put there a condidition

if (internetAvailable) {
    //load url
} else {
    //do not load
}

How can I achieve this?

Thanks

OK, so here's what I did:

My little PhoneGap App loads my dynamic website using window.location = "http://mydomain.com" , and only allows to navigate around the site if the phone has internet connection available.

If not, the link doesn't do anything. Of course, later on we'll implement alternatives views, messages, etc. But this is a functional prototype:

package com.pm.pm;

import android.os.Bundle;
import org.apache.cordova.*;

import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.DroidGap;

import android.util.Log;
import android.webkit.WebView;

import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class MyClass extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();

        CordovaWebViewClient webViewClient = new CustomCordovaWebViewClient(this);
        webViewClient.setWebView(this.appView);
        this.appView.setWebViewClient(webViewClient);

        super.loadUrl(Config.getStartUrl());
    }    

    public class CustomCordovaWebViewClient extends CordovaWebViewClient {

         public CustomCordovaWebViewClient(DroidGap ctx) {
           super(ctx);
         }

         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
             if (isNetworkAvailable()) {
                 Log.i("TEST", "shouldOverrideUrlLoading, we have internet, loading: " + url);
                 return false;
             }
             Log.i("TEST", "shouldOverrideUrlLoading, NO internet, NOT loading: " + url);
             return true;
         }

         public boolean isNetworkAvailable() {
             ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
             NetworkInfo ni = connec.getActiveNetworkInfo();
             if (ni == null) {
                 // There are no active networks.
                 return false;
             }
             return true;
         }
    }
}

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