6

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?

Praveen
  • 90,477
  • 74
  • 177
  • 219

4 Answers4

11

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
}
neteinstein
  • 17,529
  • 11
  • 93
  • 123
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • how to trace the status of GPS? – Praveen Jun 04 '10 at 13:29
  • 1
    There's no guarantee that the mobile is networkInfo 0 and the wifi will be network info 1. Might be better to check the type of what's returned by 'getActiveNetworkInfo' – haseman Aug 25 '10 at 13:55
  • 0 here means ConnectivityManager.TYPE_MOBILE, 1 - ConnectivityManager.TYPE_WIFI – Vadim Apr 08 '11 at 15:15
6

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.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
0
private boolean isNetworkAvailable() {
    ConnectivityManager connManager = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
    return activeNetworkInfo.isConnected();
}

public void onClick(DialogInterface dialog, int id) {
    // ...
    if (isNetworkAvailable()) {
        t3.setText("The Internet is available");
    } else {
        t3.setText("internet is not available");
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
0
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();
Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55