android8.0 9.0 10.0获取当前WIFI

Android8.0添加权限


 


android9.0添加权限,打开GPS



 


Android10.0添加权限

判断GPS是否打开

/**
 * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
 *
 * @param context 上下文
 * @return true 表示开启
 */
public static boolean isOpen(final Context context) {
    LocationManager locationManager
            = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
    boolean gps = Objects.requireNonNull(locationManager).isProviderEnabled(LocationManager.GPS_PROVIDER);
    // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
    boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    return gps || network;
}

打开GPS

if (!GpsHelper.isOpen(this)) {
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, REQUEST_GPS);
}
/**
 * 获取当前WIFI名称
 *
 * @param context 上下文
 * @return
 */
public static String getCurrentSsid(Context context) {
    String ssid = "unknown id";

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O
            || Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {

        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        assert wifiManager != null;
        WifiInfo info = wifiManager.getConnectionInfo();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            ssid = info.getSSID();
        } else {
            ssid = info.getSSID().replace("\"", "");
        }
        //部分手机拿不到WiFi名称
        int networkId = info.getNetworkId();
        List configuredNetworks = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration config : configuredNetworks) {
            if (config.networkId == networkId) {
                ssid = config.SSID;
                break;
            }
        }
        return ssid;
    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {

        ConnectivityManager connManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        assert connManager != null;
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo.isConnected()) {
            if (networkInfo.getExtraInfo() != null) {
                return networkInfo.getExtraInfo().replace("\"", "");
            }
        }
    }
    return ssid;
}

你可能感兴趣的:(android)