Example usage for android.net.wifi WifiManager EXTRA_WIFI_STATE

List of usage examples for android.net.wifi WifiManager EXTRA_WIFI_STATE

Introduction

In this page you can find the example usage for android.net.wifi WifiManager EXTRA_WIFI_STATE.

Prototype

String EXTRA_WIFI_STATE

To view the source code for android.net.wifi WifiManager EXTRA_WIFI_STATE.

Click Source Link

Document

The lookup key for an int that indicates whether Wi-Fi is enabled, disabled, enabling, disabling, or unknown.

Usage

From source file:Main.java

static public boolean isDisconnectedIntent(Intent intent) {
    boolean res = false;
    NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if (networkInfo != null) {
        NetworkInfo.State state = networkInfo.getState();
        res = (state.equals(NetworkInfo.State.DISCONNECTING) || state.equals(NetworkInfo.State.DISCONNECTED))
                && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);

    } else {//from w w  w  . j  a  va 2s. c  o  m
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        if (wifiState == WifiManager.WIFI_STATE_DISABLED || wifiState == WifiManager.WIFI_STATE_DISABLING) {
            res = true;
        }
    }
    return res;
}

From source file:com.prey.events.factories.EventFactory.java

public static Event getEvent(Context ctx, Intent intent) {
    String message = "getEvent[" + intent.getAction() + "]";
    PreyLogger.d(message);//  w  ww  . j av  a2 s . com
    if (BOOT_COMPLETED.equals(intent.getAction())) {
        notification(ctx);
        if (PreyConfig.getPreyConfig(ctx).isSimChanged()) {
            JSONObject info = new JSONObject();
            try {
                info.put("new_phone_number", PreyTelephonyManager.getInstance(ctx).getLine1Number());
            } catch (Exception e) {
            }
            return new Event(Event.SIM_CHANGED, info.toString());
        } else {
            return new Event(Event.TURNED_ON);
        }
    }
    if (ACTION_SHUTDOWN.equals(intent.getAction())) {
        return new Event(Event.TURNED_OFF);
    }
    if (BATTERY_LOW.equals(intent.getAction())) {
        return new Event(Event.BATTERY_LOW);
    }
    if (CONNECTIVITY_CHANGE.equals(intent.getAction())) {
        JSONObject info = new JSONObject();
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        PreyLogger.d("__wifiState:" + wifiState);
        try {
            boolean connected = false;
            if (!PreyConnectivityManager.getInstance(ctx).isWifiConnected()) {
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    if ("connected".equals(extras.getString(ConnectivityManager.EXTRA_REASON))) {
                        connected = true;
                    }
                }
            }
            if (!PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
                info.put("connected", "mobile");
                if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
                    connected = true;
                }
            }
            if (connected) {
                try {
                    Thread.sleep(4000);
                } catch (Exception e) {
                }
                PreyConfig.getPreyConfig(ctx).registerC2dm();
                Intent intentFile = new Intent(ctx, FileretrievalService.class);
                ctx.startService(intentFile);
            }
        } catch (Exception e) {
        }
        return new Event(Event.WIFI_CHANGED, info.toString());
    }
    if (WIFI_STATE_CHANGED.equals(intent.getAction())) {
        JSONObject info = new JSONObject();
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        PreyLogger.d("___wifiState:" + wifiState);

        try {
            if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
                info.put("connected", "wifi");
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                }
                PreyConfig.getPreyConfig(ctx).registerC2dm();
                Intent intentFile = new Intent(ctx, FileretrievalService.class);
                ctx.startService(intentFile);
            }
        } catch (Exception e) {
        }
        return new Event(Event.WIFI_CHANGED, info.toString());
    }
    if (AIRPLANE_MODE.equals(intent.getAction())) {
        if (!isAirplaneModeOn(ctx)) {
            notification(ctx);
            boolean connected = false;
            if (!PreyConnectivityManager.getInstance(ctx).isWifiConnected()) {
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    if ("connected".equals(extras.getString(ConnectivityManager.EXTRA_REASON))) {
                        connected = true;
                    }
                }
            }
            if (!PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
                int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_UNKNOWN);
                if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
                    connected = true;
                }
            }
            if (connected) {
                PreyBetaController.startPrey(ctx);
                PreyConfig.getPreyConfig(ctx).registerC2dm();
                Intent intentFile = new Intent(ctx, FileretrievalService.class);
                ctx.startService(intentFile);
            }
        }
    }

    return null;
}

From source file:org.servDroid.server.service.ServerService.java

@Override
public void onCreate() {
    super.onCreate();

    wifiStateChangedReceiver = new BroadcastReceiver() {

        @Override//from   ww  w .j  av  a 2  s . c o m
        public void onReceive(Context context, Intent intent) {
            int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);

            switch (extraWifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                // Logger.d(TAG, "WIFI STATE DISABLED");
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                if (mPreferenceHelper.isAutostopWifiEnabled() && getServerStatus() == STATUS_RUNNING) {
                    addLog("", "", "", "Wifi connection down... Stopping server");
                    stopServer();
                }
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                // Logger.d(TAG, "WIFI STATE ENABLED");
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                // Logger.d(TAG, "WIFI STATE ENABLING");
                break;
            case WifiManager.WIFI_STATE_UNKNOWN:
                // Logger.d(TAG, "WIFI STATE UNKNOWN");
                break;
            }

        }
    };

    registerReceiver(wifiStateChangedReceiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

}

From source file:com.android.launcher3.widget.DigitalAppWidgetProvider.java

@Override
public void onReceive(Context context, Intent intent) {
    mComtext = context;/*  w w w. j  ava 2s  . co  m*/
    String action = intent.getAction();
    Log.i("sai", "onReceive: " + action);

    super.onReceive(context, intent);

    if (ACTION_ON_QUARTER_HOUR.equals(action) || Intent.ACTION_DATE_CHANGED.equals(action)
            || Intent.ACTION_TIMEZONE_CHANGED.equals(action) || Intent.ACTION_TIME_CHANGED.equals(action)
            || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        if (appWidgetManager != null) {
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(getComponentName(context));
            for (int appWidgetId : appWidgetIds) {
                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
                float ratio = WidgetUtils.getScaleRatio(context, null, appWidgetId);
                // SPRD for bug421127 add am/pm for widget
                WidgetUtils.setTimeFormat(widget,
                        (int) context.getResources().getDimension(R.dimen.widget_label_font_size),
                        R.id.the_clock);
                WidgetUtils.setClockSize(context, widget, ratio);
                //refreshAlarm(context, widget);
                appWidgetManager.partiallyUpdateAppWidget(appWidgetId, widget);
            }
        }
        if (!ACTION_ON_QUARTER_HOUR.equals(action)) {
            cancelAlarmOnQuarterHour(context);
        }
        startAlarmOnQuarterHour(context);
    }
    // cg sai.pan begin
    else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        if (appWidgetManager != null) {
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(getComponentName(context));
            for (int appWidgetId : appWidgetIds) {
                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
                refreshBtStatus(context, widget);
                appWidgetManager.partiallyUpdateAppWidget(appWidgetId, widget);
            }
        }
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        int wifiStatus = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        Log.e("sai", "wifiStatus" + wifiStatus);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        if (appWidgetManager != null) {
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(getComponentName(context));
            for (int appWidgetId : appWidgetIds) {
                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
                if (WifiManager.WIFI_STATE_ENABLED == wifiStatus
                        || WifiManager.WIFI_STATE_ENABLING == wifiStatus) {
                    widget.setImageViewResource(R.id.wifi, R.drawable.status_wifi_on);
                } else {
                    widget.setImageViewResource(R.id.wifi, R.drawable.status_wifi_off);
                }
                appWidgetManager.partiallyUpdateAppWidget(appWidgetId, widget);
            }
        }
    } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        if (appWidgetManager != null) {
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(getComponentName(context));
            for (int appWidgetId : appWidgetIds) {
                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);
                refreshWifiStatus(context, widget);
            }
        }
    } else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(action)) {
        if (isNetworkConnected(context)) {
            Log.e("sai", "isNetworkConnected true");
            requestLocation(context);
        } else {
            Log.e("sai", "isNetworkConnected false");
        }
    }
}

From source file:org.metawatch.manager.Monitors.java

private static void createWifiReceiver(final Context context) {
    if (wifiReceiver != null)
        return;//  ww  w  .j  a  va2 s.co  m
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wm.getConnectionInfo();
    if (info != null)
        SignalData.wifiBars = 1 + WifiManager.calculateSignalLevel(info.getRssi(), 4);
    wifiReceiver = new BroadcastReceiver() {
        int wifiBars = 0;

        @Override
        public void onReceive(Context c, Intent intent) {
            String action = intent.getAction();
            if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_UNKNOWN) != WifiManager.WIFI_STATE_ENABLED) {
                    wifiBars = 0;
                }
            } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (!intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
                    wifiBars = 0;
                }
            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (netInfo.getState() != NetworkInfo.State.CONNECTED) {
                    wifiBars = 0;
                } else {
                    WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
                    if (wifiInfo == null) {
                        wifiBars = 0;
                    } else {
                        wifiBars = 1 + WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4);
                    }
                }
            } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
                final int newRssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
                wifiBars = 1 + WifiManager.calculateSignalLevel(newRssi, 4);
            }
            if (wifiBars != SignalData.wifiBars) {
                SignalData.wifiBars = wifiBars;
                Idle.updateIdle(context, true);
            }
        }
    };
    IntentFilter f = new IntentFilter();
    f.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    f.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    f.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    f.addAction(WifiManager.RSSI_CHANGED_ACTION);
    context.registerReceiver(wifiReceiver, f);
}