1. 程式人生 > >判斷WIFi狀態的NetUtils

判斷WIFi狀態的NetUtils

public class NetUtils {

    public static boolean isConnected() {
        ConnectivityManager connectivity = (ConnectivityManager) MyApplication.sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            return info != null && info.isConnected();
        }
        return false;
    }

    public static boolean isWifiConnected() {
        ConnectivityManager connectivity = (ConnectivityManager) MyApplication.sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            return info != null && info.getType() == ConnectivityManager.TYPE_WIFI;
        }
        return false;
    }

    public static boolean isWifiOpen() {
        WifiManager wifiManager = (WifiManager) MyApplication.sContext.getSystemService(Context.WIFI_SERVICE);
        return wifiManager != null && wifiManager.isWifiEnabled();
    }

    public static boolean isLocationOpen() {
        LocationManager locationManager = (LocationManager) MyApplication.sContext.getSystemService(Context.LOCATION_SERVICE);
        return locationManager != null && (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER));
    }
//需要實現 PermissionManager.OnRequestPermissionsListener   介面監聽許可權獲取狀態  
     (重寫onPermissionGranted()  onPermissionDenied()兩個函式)
    public void getPermission() {
        mPermissionManager = PermissionManager.Factory.get(this, this);
        mPermissionManager
            .checkPermission(PermissionManager.Permission.WIFI_PERMISSION, REQUEST_PERMISSION, 0, R.string
                    .permission_hint_wifi_history);
     }
    private checkPermission() {
        if (ContextCompat.checkSelfPermission(mContext, PermissionManager.Permission.WIFI_PERMISSION[0]) 
               !=  PackageManager.PERMISSION_GRANTED) {
                getPermission()
         }
     }

 

}
}