prokopis prokopis - 10 months ago 112
Java Question

wait until wifi connected on android

I have a small program that trying to connect to a wifi network.
It enables the wifi on the device then if it's the first time that is connect to the certain networkss It loads the device wifi in order to select and add the password for connection.
Until I add the password in order to connect the program should not be finished.
How can I add something to wait until I get from the wifi manager that is connected?
I try sleep but it freeze the application and am not getting the wifi pop up menu to connect?
are there any other ways?

R4j R4j
Answer Source

I have found the solution for your problem a month ago, just use Thread put method isConnected() in it.
In this case, I use WifiExplorerActivity to display all wifi network and allow user connect to it.

        Thread t = new Thread() {
        public void run() {
            try {
                    //check if connected!
                while (!isConnected(WifiExplorerActivity.this)) {
                    //Wait to connect

                Intent i = new Intent(WifiExplorerActivity.this, MainActivity.class);

            } catch (Exception e) {

And this is function to check wifi has connected or not:

  public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getActiveNetworkInfo();

    return networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED;

Finally, make sure your Androidmanifest.xml look like this:

    <activity android:name=".WifiExplorerActivity" >        
            <action android:name="" />

Also, you can use ProgressDialog to wait connect. See