Home android How to check the Internet connection?

How to check the Internet connection?

Author

Date

Category

I want, when you press the button a message was displayed that there is no connection to the Internet. Posted by such a method:

Protected Boolean Isonline () {
  String CS = context.connectivity_service;
  ConnectivityManager CM = (ConnectivityManager) GetSystemService (CS);
  if (cm.getActiveNetworkinfo () == null) {
    RETURN FALSE;
  } else {
    RETURN TRUE;
  }
}

Next in Akiviti, wrote the following:

if (! Isonline) {
   Toast.maketext (this, "No Internet Connection", toast.length_long) .show ();
   Myactivity.this.finish ();
}

And for some reason, all the same during the debate, this piece passes through. What is the problem?
All the necessary Permissions in the manifesta is prescribed.


Answer 1, Authority 100%

works like a clock:

Public Static Boolean Hasconnection (Final Context Context)
{
  ConnectivityManager CM = (ConnectivityManager) Context.GetSystemService (Context.Connectivity_Service);
  Networkinfo wifiinfo = cm.getnetworkinfo (connectivitymanager.type_wifi);
  if (wifiinfo! = NULL & amp; & amp; wifiinfo.isconnected ())
  {
    RETURN TRUE;
  }
  WiFiINFO = CM.GetNetworkInfo (ConnectivityManager.Type_Mobile);
  if (wifiinfo! = NULL & amp; & amp; wifiinfo.isconnected ())
  {
    RETURN TRUE;
  }
  wifiinfo = cm.getActiveNetworkinfo ();
  if (wifiinfo! = NULL & amp; & amp; wifiinfo.isconnected ())
  {
    RETURN TRUE;
  }
  RETURN FALSE;
}

Answer 2, Authority 93%

I use this method, I have never failed

Public Static Boolean Isonline (Context Context)
  {
    ConnectivityManager CM =.
        (ConnectivityManager) Context.GetSystemService (Context.connectivity_Service);
    NetworkInfo netinfo = cm.getActiveNetworkinfo ();
    If (NetInfo! = NULL & amp; & amp; netinfo.isconnectedorconnecting ())
    {
      RETURN TRUE;
    }
    RETURN FALSE;
  }

Answer 3, Authority 21%

Thanks to the author, everything works quite well, but there is a nuance

Protected Boolean Isonline () {
  String CS = context.connectivity_service;
  ConnectivityManager CM = (ConnectivityManager)
  GetSystemService (CS);
  if (cm.getActiveNetworkinfo () == null) {
  RETURN FALSE;
  } else {retrun true; }

Only here I corrected a little:

if (! Isonline) {
  Toast.maketext (this, "No Internet Connection", toast.length_long) .show ();
  Myactivity.this.finish ();
}

and it turned out:

if (! isonline ()) {
  Toast.maketext (getApplicationContext (),
 "No internet connection!", Toast.length_long) .show ();
          Return;
        }

If insert in the handler onClick then check works well.


Answer 4, Authority 7%

Chease, such as:

Public Boolean Hasconnection (Final Context Context) {
  ConnectivityManager CM = (ConnectivityManager) Context.GetSystemService (Context.Connectivity_Service);
  NetworkInfo ActiveNW = cm.getActiveNetworkinfo ();
  if (ActiveNW! = NULL & amp; & amp; activenw.isconnected ())
  {
   RETURN TRUE;
  }
  RETURN FALSE;

}

in the main body:

if (hasconnection (this)) {
  Toast.maketext (this, "Active Networks OK", toast.length_long) .show ();
  getAccountdata (token, token_data_est);
}
Else toast.maketext (this, "No Active Networks ...", toast.length_long) .show ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions