I am checking the internet connection at the start of my app and if not connected then it should show a alert message

869 views Asked by At
if(haveNetworkConnection()==false)
    {
         AlertDialog alertDialog = new AlertDialog.Builder(this).create();

            alertDialog.setTitle("Info");
            alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);

            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                 finish();

               }
            });

            alertDialog.show();

    }

I am checking the internet connection at the start of my app and if not connected then it should show a alert message, and on pressing ok should exit the app.This code does not work.

2

There are 2 answers

1
Sanjeev On

Since you haven't provided any code for haveNetworkConnection() try replacing with this

public boolean haveNetworkConnection{

ConnectivityManager connectivity = (ConnectivityManager) context.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;

}
1
Eoin On

Really you shouldnt close your app just because you dont have a connection. Mobile connections can sometimes be erratic and this should be handled. But if you have to.

To check if you have an active connection you first have to make sure you have the right permissions set in your manifest

uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

then you can use a method like this to check your connection

private Boolean haveNetworkConnection(){
     ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

     NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
     boolean isConnected = activeNetwork != null &&
             activeNetwork.isConnectedOrConnecting();

     return isConnected;
}

finally to make sure you exit the app and not just close the activity call System.exit(0); instead of finish()

if(!haveNetworkConnection())
{
     AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        alertDialog.setTitle("Info");
        alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
             System.exit(0);
           }
        });

        alertDialog.show();
}