wait For Wifi Off - Android Wifi

Android examples for Wifi:Wifi Connection

Description

wait For Wifi Off

Demo Code


//package com.java2s;

import android.content.Context;

import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

public class Main {
    private static void waitForWifiOff(Context context) throws Exception {
        boolean isWifiOff = false;
        long startTime = System.currentTimeMillis();
        do {/* ww w.j a va 2s .  c o m*/
            Thread.sleep(500);
            isWifiOff = !isWifiOn(context);
        } while (!isWifiOff
                && (System.currentTimeMillis() - startTime < 10000));
        if (!isWifiOff) {
            throw new Exception("Wifi is not turned off");
        }
    }

    /**
     * Verify is Wifi network on now
     * 
     * @param context
     *            - Context
     * @return true if Wifi is switched on
     * @throws SettingNotFoundException
     */
    public static boolean isWifiOn(Context context)
            throws SettingNotFoundException {
        int value = Settings.System.getInt(context.getContentResolver(),
                Settings.Secure.WIFI_ON);
        return value == 0 ? false : true;
    }
}

Related Tutorials