How to show dialog box that No internet connection in your android app

 @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isConnected()) {
            buildDialog(MainActivity.this).show();
        }
        else            {
                //what ever Your code is please put it here. 
            }

}

public boolean isConnected() {

    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();

    if (netinfo != null && netinfo.isConnected())
    {
        android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if((mobile != null && mobile.isConnected()) || (wifi != null && wifi.isConnected()))
            return true;

         else return false;
    }
    else    return false;
}

public AlertDialog.Builder buildDialog(Context c) {

    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("please trun on wifi or Mobile Data to access the app and again run the app. Press ok to Exit");

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });

    return builder;
}

No comments:

Post a Comment

How to set  interstitial  ads to be show for each n seconds ? ScheduledExecutorService schedul = Executors. newSingleThreadScheduledExec...