Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check whether an android device is connected to the web?

Tags:

How would i know whether my device is connected the web or not? How can i detect connectivity? Any sample code?

like image 884
Muhammad Maqsoodur Rehman Avatar asked May 07 '10 15:05

Muhammad Maqsoodur Rehman


People also ask

How do you check if a device is connected to internet?

Open the Google Wifi app . Devices. The numbers next to "Devices" represent your total Internet (WAN) traffic to and from your network. Under each device, you can view how much data each device has downloaded and uploaded.

How do Android apps connect to the internet?

Before starting your application, Android studio will display following window to select an option where you want to run your Android application. Now just click on button, It will check internet connection as well as it will download image. Out would be as follows and it has fetch the logo from internet.


1 Answers

First, you need permission to know whether the device is connected to the web or not. This needs to be in your manifest, in the <manifest> element:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Next, you need to get a reference to the ConnectivityManager:

ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE); 

From there you need to obtain a NetworkInfo object. For most, this will mean using ConnectivityManager. getActiveNetworkInfo():

NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni == null) {     // There are no active networks.     return false; } 

From there, you just need to use one of NetworkInfo's methods to determine if the device is connected to the internet:

boolean isConnected = ni.isConnected(); 
like image 111
Dan Lew Avatar answered Sep 30 '22 04:09

Dan Lew