Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.net.wifi.WifiManager;

public class Main {
    public static boolean setWifi(Context context, boolean enable) throws Exception {
        if (isWifiOpen(context) != enable) {
            ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).setWifiEnabled(enable);
        }

        return true;
    }

    public static boolean isWifiOpen(Context context) throws Exception {
        int wifiState = getWifiState(context);
        return wifiState == 3 || wifiState == 2;
    }

    public static int getWifiState(Context context) throws Exception {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            return wifiManager.getWifiState();
        } else {
            throw new Exception("wifi device not found!");
        }
    }
}