Question Detail

No Internet Connected alertDialog android?

3 years ago Views 1931 Visit Post Reply

when internet not connected to my mobile or disable application should display an Alert about NO INTERNET CONNECTION FOUND PLEASE CHECK YOUR NETWORK.
How can I know that internet not working or Not connected Programmatically?

No Internet Connected sweetalertdialog QnA


Thread Reply

Correct Answer

Hemant Sharma

- 3 years ago

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Hemant on 8/8/2017.
 */

public class InternetConnectionCheck {
    static Context context;
    void InternetConnectionCheck(){
    }
    public static boolean isConnectingToInternet(Context applicationContext) {
        context=applicationContext;
        ConnectivityManager cm = (ConnectivityManager)applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            new SweetAlertDialog(applicationContext, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("No Internet Connection!")
                    .setContentText("Please Check Internet Connection.")
                    .setCancelText("It's Okay")
                    .setConfirmText("Settings!")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {
                            context.startActivity(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
                            sDialog.dismiss();

                        }
                    }).show();
            return false;
        } else
            return true;
    }
}