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

前端 未结 5 1807
青春惊慌失措
青春惊慌失措 2020-12-13 03:15

This is what I would like to do :

=> IF WiFi is enabled AND active, launch an intent (in fact it\'s a WebView that gets its content=>the instructions of my app on th

5条回答
  •  半阙折子戏
    2020-12-13 03:40

    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.

提交回复
热议问题