Adrian M.
Adrian M.

Reputation: 7443

Android check internet connection, retry or exit if offline

I am trying to make the app check for internet connection and if it's offline, redirect to a offline html file in webview. Then what I don't know is how to enable user to retry or cancel and exit the app..

This is my code:

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.app.Activity;
import android.os.Bundle;
//import android.view.Menu;
//import android.view.MenuItem;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import android.view.KeyEvent;

public class MainActivity extends Activity {
WebView browser;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the WebView by name
    browser=(WebView)findViewById(R.id.wvwMain);

    // Enable javascript
    browser.getSettings().setJavaScriptEnabled(true);  

    // Set WebView client
    browser.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
                }
        });

     // Load the webpage 
        ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nf=cn.getActiveNetworkInfo();

        if(nf != null && nf.isConnected()==true )
        {
            browser.loadUrl("http://website.com/");
            }
        else
        {
            Toast.makeText(this, "Network Not Available", Toast.LENGTH_LONG).show();
            browser.loadUrl("file:///android_asset/noconnection.html");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
            browser.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Upvotes: 2

Views: 6455

Answers (2)

dumazy
dumazy

Reputation: 14445

You'll have to implement something like a button to let the user retry. Then when you notice there's no connection, show the button and retry on click.

Something helpful in general, create a utils class to check if you're connected.

public class ConnectionUtils {

public static boolean isConnected(Context context){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if ((wifiInfo != null && wifiInfo.isConnected()) || (mobileInfo != null && mobileInfo.isConnected())) {
            return true;
    }else{
            return false;
    }
}

}

Then you can call ConnectionUtils.isConnected(this) or ConnectionUtils.isConnected(getActivity) at any time.

Upvotes: 5

Badrul
Badrul

Reputation: 1642

Try this for checking connectivity

public boolean isConnectedToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) 
    {
        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;
}

then use this wherever you want to check connectivity.

if(isConnectedToInternet())

{
    if(tCommentsFlag != 1)
    {
        // Your code

    }
}

To exit android app use this:

 finish();  

Upvotes: 3

Related Questions