Search in sources :

Example 71 with WifiManager

use of android.net.wifi.WifiManager in project android_packages_apps_Settings by SudaMod.

the class DataPlanUsageSummaryTest method testUpdateNetworkRestrictionSummary_shouldSetSummary.

@Test
public void testUpdateNetworkRestrictionSummary_shouldSetSummary() {
    mDataUsageSummary = spy(new DataPlanUsageSummary());
    NetworkRestrictionsPreference preference = mock(NetworkRestrictionsPreference.class);
    mPolicyEditor = mock(NetworkPolicyEditor.class);
    WifiManager wifiManager = mock(WifiManager.class);
    ReflectionHelpers.setField(mDataUsageSummary, "mPolicyEditor", mPolicyEditor);
    ReflectionHelpers.setField(mDataUsageSummary, "mWifiManager", wifiManager);
    when(wifiManager.getConfiguredNetworks()).thenReturn(new ArrayList<>());
    doReturn(mContext.getResources()).when(mDataUsageSummary).getResources();
    mDataUsageSummary.updateNetworkRestrictionSummary(preference);
    verify(preference).setSummary(mContext.getResources().getQuantityString(R.plurals.network_restrictions_summary, 0, 0));
}
Also used : WifiManager(android.net.wifi.WifiManager) NetworkPolicyEditor(com.android.settingslib.NetworkPolicyEditor) Test(org.junit.Test)

Example 72 with WifiManager

use of android.net.wifi.WifiManager in project AndroidStudy by tinggengyan.

the class NetConnectBroadCastReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
    } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        System.out.println("网络状态改变");
        // wifi连接上与否
        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
            System.out.println("wifi网络连接断开");
        } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            // 获取当前wifi名称
            System.out.println("连接到网络 " + wifiInfo.getSSID());
        }
    } else // wifi打开与否
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
        if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
            System.out.println("系统关闭wifi");
        } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
            System.out.println("系统开启wifi");
        }
    } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("MOBILE")) {
                System.out.println("CONNECTIVITY_ACTION");
                if (ni.isConnected()) {
                    System.out.println("mobile is connected");
                }
            } else if (ni.getTypeName().equals("WIFI")) {
                if (ni.isConnected()) {
                    System.out.println("wifi is connected");
                }
            }
        }
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                System.out.println("连接到网络 " + activeNetworkInfo.getDetailedState());
            }
        }
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) WifiInfo(android.net.wifi.WifiInfo)

Example 73 with WifiManager

use of android.net.wifi.WifiManager in project android_packages_apps_CMParts by LineageOS.

the class SetupActionsFragment method fillProfileWithCurrentSettings.

public static void fillProfileWithCurrentSettings(Context context, Profile profile) {
    // bt
    if (DeviceUtils.deviceSupportsBluetooth()) {
        profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH, BluetoothAdapter.getDefaultAdapter().isEnabled() ? 1 : 0, true));
    }
    // gps
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_GPS, gpsEnabled ? 1 : 0, true));
    // wifi
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_WIFI, wifiManager.isWifiEnabled() ? 1 : 0, true));
    // auto sync data
    profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_SYNC, ContentResolver.getMasterSyncAutomatically() ? 1 : 0, true));
    // mobile data
    if (DeviceUtils.deviceSupportsMobileData(context)) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA, cm.getMobileDataEnabled() ? 1 : 0, true));
    }
    // wifi hotspot
    profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_WIFIAP, wifiManager.isWifiApEnabled() ? 1 : 0, true));
    // nfc
    if (DeviceUtils.deviceSupportsNfc(context)) {
        NfcManager nfcManager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
        profile.setConnectionSettings(new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_NFC, nfcManager.getDefaultAdapter().isEnabled() ? 1 : 0, true));
    }
    // alarm volume
    final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    profile.setStreamSettings(new StreamSettings(AudioManager.STREAM_ALARM, am.getStreamVolume(AudioManager.STREAM_ALARM), true));
    // media volume
    profile.setStreamSettings(new StreamSettings(AudioManager.STREAM_MUSIC, am.getStreamVolume(AudioManager.STREAM_MUSIC), true));
    // ringtone volume
    profile.setStreamSettings(new StreamSettings(AudioManager.STREAM_RING, am.getStreamVolume(AudioManager.STREAM_RING), true));
    // notification volume
    profile.setStreamSettings(new StreamSettings(AudioManager.STREAM_NOTIFICATION, am.getStreamVolume(AudioManager.STREAM_NOTIFICATION), true));
    // ring mode
    String ringValue;
    switch(am.getRingerMode()) {
        default:
        case AudioManager.RINGER_MODE_NORMAL:
            ringValue = "normal";
            break;
        case AudioManager.RINGER_MODE_SILENT:
            ringValue = "mute";
            break;
        case AudioManager.RINGER_MODE_VIBRATE:
            ringValue = "vibrate";
            break;
    }
    profile.setRingMode(new RingModeSettings(ringValue, true));
    // airplane mode
    boolean airplaneMode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    profile.setAirplaneMode(new AirplaneModeSettings(airplaneMode ? 1 : 0, true));
// lock screen mode
// populated only from profiles, so we can read the current profile,
// but let's skip this one
}
Also used : LocationManager(android.location.LocationManager) WifiManager(android.net.wifi.WifiManager) AudioManager(android.media.AudioManager) StreamSettings(cyanogenmod.profiles.StreamSettings) ConnectivityManager(android.net.ConnectivityManager) RingModeSettings(cyanogenmod.profiles.RingModeSettings) NfcManager(android.nfc.NfcManager) AirplaneModeSettings(cyanogenmod.profiles.AirplaneModeSettings) ConnectionSettings(cyanogenmod.profiles.ConnectionSettings)

Example 74 with WifiManager

use of android.net.wifi.WifiManager in project mapbox-events-android by mapbox.

the class MapEventFactory method isConnectedToWifi.

private boolean isConnectedToWifi(Context context) {
    try {
        WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        // noinspection MissingPermission
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        return isWifiConnected(wifiMgr, wifiInfo);
    } catch (Exception exception) {
        return false;
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo)

Example 75 with WifiManager

use of android.net.wifi.WifiManager in project Weather-Station by Kresshy.

the class WifiFragment method connectToSelectedWifi.

public void connectToSelectedWifi(String networkSSID, String networkPass) {
    WifiConfiguration conf = new WifiConfiguration();
    // Please
    conf.SSID = "\"" + networkSSID + "\"";
    conf.wepKeys[0] = "\"" + networkPass + "\"";
    conf.wepTxKeyIndex = 0;
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    conf.preSharedKey = "\"" + networkPass + "\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(conf);
    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            wifiManager.disconnect();
            wifiManager.enableNetwork(i.networkId, true);
            wifiManager.reconnect();
            break;
        }
    }
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration)

Aggregations

WifiManager (android.net.wifi.WifiManager)329 WifiInfo (android.net.wifi.WifiInfo)80 WifiConfiguration (android.net.wifi.WifiConfiguration)76 IOException (java.io.IOException)38 Intent (android.content.Intent)27 SuppressLint (android.annotation.SuppressLint)26 ConnectivityManager (android.net.ConnectivityManager)23 Context (android.content.Context)22 NetworkInfo (android.net.NetworkInfo)21 Test (org.junit.Test)20 AccessPoint (com.android.settingslib.wifi.AccessPoint)17 IntentFilter (android.content.IntentFilter)16 Bundle (android.os.Bundle)16 ArrayList (java.util.ArrayList)16 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 PowerManager (android.os.PowerManager)11 WifiEnterpriseConfig (android.net.wifi.WifiEnterpriseConfig)10 SocketException (java.net.SocketException)10 SharedPreferences (android.content.SharedPreferences)9 Method (java.lang.reflect.Method)9