0

I have a button with an onclick event that opens another class. The other class is making network calls to retrieve data and so if there is no connection then the app force closes.

How would I implement a try catch or if statement that could check for the data connection and if there isn't one, display a toast saying so. And if there is then proceed to second class.

6 Answers6

0

I use:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
iaindownie
  • 1,046
  • 12
  • 28
0

Do something like this to check internet connectivity :

static public boolean isInternetActive() 
{
    ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = (NetworkInfo) connectivity.getActiveNetworkInfo();

    if (info == null || !info.isConnected()) 
    {
        return false;
    }
    if (info.isRoaming()) 
    {
        return false;
    }
    return true;
}
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0
public boolean isOnline() {
ConnectivityManager cm =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

return cm.getActiveNetworkInfo() != null && 
   cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

You will also need:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your android manifest.

bofredo
  • 2,348
  • 6
  • 32
  • 51
0

this code check whether the is internet connection on or off the code checks wifi and phones net.

public boolean CheckInternet() 
{
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    // Here if condition check for wifi and mobile network is available or not.
    // If anyone of them is available or connected then it will return true, otherwise false;

    if (wifi.isConnected()) {
        return true;
    } else if (mobile.isConnected()) {
        return true;
    }
    return false;
}

Manifest file::

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Prakhar
  • 2,270
  • 19
  • 26
0

Try this, this will help you..

public class CheckNetworkInfo {

    public static boolean haveNetworkConnection(Context context) {

         ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            // test for connection
            if (cm.getActiveNetworkInfo() != null
                    && cm.getActiveNetworkInfo().isAvailable()
                    && cm.getActiveNetworkInfo().isConnected()) {
                Log.v("Yeah", "Internet is working");
                // txt_status.setText("Internet is working");
                return true;
            } else {
                // txt_status.setText("Internet Connection Not Present");
                Log.v("Sorry", "Internet Connection Not Present");
                return false;
            }
}

// Call above class static method where u want to check network Availability

class Myactivity extends Activity{

public void onCreate(Bundle savedInstanceState) {

    if(CheckNetworkInfo.haveNetworkConnection(Myactivity.this)){
            startActivity(new Intent(HomePage.this,SearchTerm.class));
        }
        else{
            AlertDialog.Builder a=new AlertDialog.Builder(Myactivity.this);
            a.setTitle("Check Network Connection");
            a.setPositiveButton("OK",new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                dialog.dismiss();   
                }
            });
            a.show();
        }
}
}
Mukesh Garg
  • 711
  • 1
  • 6
  • 20
0

Simply use it.

Call this code from anywhere it will return you the current state of the Network.

public static boolean isInternetAvailable(Context c)
    {
         ConnectivityManager connectivityManager 
         = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
         return activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
    }

return true if it is connected, false otherwise. Take any desired action on its output.

Note: Its a static method. So remove static keyword if you just want to call it from within an Android Activity class.

Master
  • 2,945
  • 5
  • 34
  • 65