Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check the internet connection in android? [duplicate]

I want to check the internet connectivity in each activity. If it is lost a message should be displayed.

Can any one guide me how to achieve this?

like image 591
UMAR-MOBITSOLUTIONS Avatar asked Feb 24 '10 14:02

UMAR-MOBITSOLUTIONS


People also ask

How can I check the reliability of my Internet connection?

Online services such as Pingtest or Speedtest enable you to test the reliability of your device's Internet connection by running various tests. Internet users can experience a variety of problems when connecting to websites or when they are using services on the Internet.

How do you check internet is on or off in android programmatically?

In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.


1 Answers

Only one connection can be active at any one point. So a simpler answer is:

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnected()) {     // notify user you are online } else {     // notify user you are not online }  

It also caters for any new type of network such as ConnectivityManager#TYPE_WIMAX


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
like image 133
William Avatar answered Sep 22 '22 18:09

William