ANDROID: if WiFi is enabled AND active, launch an intent

无人久伴 提交于 2019-11-28 17:06:26
Henrique Rocha

You can use the following code to check for connectivity:

private static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}

Please make sure that you've registered the android.net.conn.CONNECTIVITY_CHANGE intent in your Manifest, or else, you'll never receive a notification that you're online.

I've been struggling with this issue for the last couple of days and I just now realized that I needed to register CONNECTIVITY_CHANGE and not only WIFI_STATE_CHANGED.

lencinhaus

Try android.net.ConnectivityManager.getActiveNetworkInfo(): if it returns null you have no connection; if it returns a NetworkInfo object, you can check the connection's state with NetworkInfo.getState(), and if it's NetworkInfo.State.CONNECTED then you're connected, else you're not.

You can do it as follows:

  @Override
  public void onReceive(Context context, Intent intent) {

  String action = intent.getAction();

        if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)){

         Log.d("WIFI", "WIFI has changed");
         int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
         Log.d("WIFI", "WIFI State = " + wifiState);
         setCurrentWifiState(wifiState);

         }  

You will get 0,1,2,3 depending on which state the Wifi is in, so for example 2 is connecting, you can check the rest in the documents

In your BroadcastReceiver class:

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){                
        boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
        if (connected){
            // start your service here
        }
    }                   
}

And in your AndroidManifest.xml make sure you register for the android.net.wifi.supplicant.CONNECTION_CHANGE broadcast intent.

<intent-filter >    
    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
Agustin Gandara

isConnected() doesnt work fully ok, research something else

final ConnectivityManager connMgr = (ConnectivityManager)
        this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
    connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile =
    connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if( wifi.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED){
        Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
    }
    else if( mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED ){
        Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
    }
    else
    {   
        Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
    }

this code check if you are with wifi or 3g or nothing , in the case of wifi on but not connected to a net or 3g have signal problem it detect this details, with DetailedStates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!