Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check WiFi and GPS isConnected or Not in Android?

Tags:

android

wifi

gps

I would need to check the wifi is on or off in the phone at the runtime?

if it is not connected, i want to show dialog and goto directly Setting/Wireless Controls to enable it by user.

its for both wifi and Gps staus of the phone. How to do it? which intent to wake for this? Any idea?

like image 918
Praveen Avatar asked Jun 04 '10 10:06

Praveen


2 Answers

To check if the device is connected via mobile or wifi you can use this code:

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

//mobile
State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

//wifi
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

and then use it like that:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
}
like image 91
RoflcoptrException Avatar answered Sep 18 '22 02:09

RoflcoptrException


You can use the WifiManager class to get the state of Wi-Fi.

See this question for opening Wi-Fi settings. And this question for GPS status.

like image 23
kgiannakakis Avatar answered Sep 21 '22 02:09

kgiannakakis