0

Morning,

I am using the following code in my Android app to load externally hosted web app This works fine and looks better than I expected (no webview nav buttons)

public class MainActivity extends DroidGap {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.loadUrl("http://www.external-url.com/webapp");
  } 
}

However, if the server is down or device has no internet connection, I get the ugly looking Android "Web Page not available" page. I want to avoid this by only calling the super.loadUrl if the web page is available.

What is the most efficient way to do this?

Jez D
  • 1,461
  • 2
  • 25
  • 52

3 Answers3

1

It usually comes, How can you overcome this is just check the network connections and Load the URL only if the connections are available else just display the Dialog.

if(isOnline()) super.loadUrl("http://www.external-url.com/webapp",1000);

You can add the delay also in loading the URL.

to check the network connection:

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting())    // netInfo.isConnected is used if you confirmly want the network connection.

   {
            return true;
        }
        return false;
    }

Also add the following permission to the Android Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48
  • Thanks. I already had the user permission. I added your function isOnline to my activity, changed isConnectedOrConnecting to isConnected and then proceeded super.loadUrl(... with if(isOnline().. now I just need to do a check to see if the webpage is live (maybe ping) – Jez D Oct 09 '13 at 10:46
0

Try the following, first check if internet is available then load url or else show a warning or anything:

DetectConnection.java:

    import android.content.Context;
    import android.net.ConnectivityManager;

    public class DetectConnection {
        /*
         * Checking internet connection
         */
        public static boolean checkInternetConnection(Context context) {

            ConnectivityManager con_manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            if (con_manager.getActiveNetworkInfo() != null
                    && con_manager.getActiveNetworkInfo().isAvailable()
                    && con_manager.getActiveNetworkInfo().isConnected()) {
                return true;
            } else {
                return false;
            }
        }
    }

MainActivity.java:

 public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    if (DetectConnection.checkInternetConnection(ViewNotifications.this)) {
                super.loadUrl("http://www.external-url.com/webapp");
            }else{
//Show warning
}


      } 
    }
fida1989
  • 3,234
  • 1
  • 27
  • 30
0

I Hope this will work for you

           public class Util {
public static boolean isNetworkAvailable(Activity activity) {
    ConnectivityManager connectivity = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}}

and use this in-

   if (Util.isNetworkAvailable(Home.this)) {
    // do your coding here
    }
DJhon
  • 1,548
  • 3
  • 22
  • 39