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?
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With