Skip to content Skip to sidebar Skip to footer

Checking If There Is An Active Internet Connection

I am have been fiddling with Android development and I have been having some trouble trying to check if there is an ACTIVE Internet connection even if the net is ON. Since sometim

Solution 1:

try below code

BooleanisInternetPresent=false;
    ConnectionDetector cd;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    btnCheck = (Button) findViewById(R.id.btnCheck);

    btnCheck.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {


            cd = newConnectionDetector(getApplicationContext());
            isInternetPresent = cd.isConnectingToInternet();

            if (isInternetPresent) {

                Toast.makeText(MainActivity.this, "There is a connection", Toast.LENGTH_SHORT).show();

            }else {

              Toast.makeText(MainActivity.this, "NO connection", Toast.LENGTH_SHORT).show();

                }

            }
        });

    }

also don't forget to add internet permission in manifest file.

Post a Comment for "Checking If There Is An Active Internet Connection"