Example usage for android.net.wifi WifiManager NETWORK_STATE_CHANGED_ACTION

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

Introduction

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

Prototype

String NETWORK_STATE_CHANGED_ACTION

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

Click Source Link

Document

Broadcast intent action indicating that the state of Wi-Fi connectivity has changed.

Usage

From source file:net.helff.wificonnector.WifiConnectivityReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "action: " + intent.getAction());

    if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        Intent locationIntent = new LocationIntent();
        context.sendBroadcast(locationIntent);
    }/*from  w w w  . java2  s  .  c o  m*/

    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        Log.d(TAG, "got network_state_changed with detailed info: " + networkInfo == null ? "nothing"
                : networkInfo.getDetailedState().name());

        if (networkInfo != null && networkInfo.isConnected()
                && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean autoConnect = prefs.getBoolean("autoConnect", false);

            Log.d(TAG, "triggering WifiConnectivityService with autoConnect=" + autoConnect);

            int command = autoConnect ? WifiConnectivityService.COMMAND_AUTO_UNLOCK_CONNECTION
                    : WifiConnectivityService.COMMAND_CHECK_CONNECTION;
            Intent msgIntent = new Intent(context, WifiConnectivityService.class);
            msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND, command);
            context.startService(msgIntent);
        }

    }
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        Log.d(TAG, "triggering WifiConnectivityService state changed");

        Intent msgIntent = new Intent(context, WifiConnectivityService.class);
        msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND,
                WifiConnectivityService.COMMAND_REFRESH_STATUS);
        context.startService(msgIntent);
    }
}

From source file:com.ultrafunk.network_info.receiver.WifiStatusReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //   Log.e(this.getClass().getSimpleName(), "onReceive(): " + action);

    updateWifiViews = true;//from  w  ww  .  ja va  2s .  c om

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiState = wifiManager.getWifiState();
    wifiInfo = wifiManager.getConnectionInfo();

    if ((wifiState == WifiManager.WIFI_STATE_ENABLED) && (wifiInfo.getIpAddress() == 0))
        detailedState = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());

    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        if (isConnectionReady(intent)) {
            String securityString = WifiUtils.getSecurityString(context, wifiManager, wifiInfo.getBSSID());
            NetworkStateService.setWifiSecurityString(securityString);
            detailsString = context.getString(R.string.security) + ": " + securityString;

            Intent serviceIntent = new Intent(context, NetworkStateService.class);
            serviceIntent.setAction(Constants.ACTION_WIFI_CONNECTED);
            context.startService(serviceIntent);
        }

        partiallyUpdateWidgets(context);
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)
            || Constants.ACTION_WIFI_SCANNING.equals(action)) {
        partiallyUpdateWidgets(context);
    } else if (Intent.ACTION_SCREEN_ON.equals(action) || Constants.ACTION_WIFI_LINK_SPEED.equals(action)) {
        if (isConnected()) {
            setDetailsString(context);
            partiallyUpdateWidgets(context);
        }
    } else if (Constants.ACTION_UPDATE_WIDGET.equals(action)) {
        if (isConnected())
            setDetailsString(context);

        partiallyUpdateWidget(context, AppWidgetManager.getInstance(context),
                intent.getIntExtra(Constants.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
    }
}

From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (networkInfo.isConnected()) {
            /**//from  w  ww.j  a  va 2  s  . c o  m
             * WiFi is connected, get its broadcast 
             * address and start the discovery process
             * repeated at a fixed time interval
             */
            wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected()) {
            /**
             * WiFi is disconnected, stop the discovery
             * process repeating, it would be a waste of resources
             */
            wifiBroadcastAddress = null;
            stopAlarmRepeater(context);
        }
    } else if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))
            || intent.getAction()
                    .equals(context.getResources().getString(R.string.broadcast_cleanhostcollection))) {

        boolean startService = true;
        /**
         * Calls the DiscoverServerService asking to do a discovery
         * or a clean host collection by simply forwarding the received action
         */
        Intent service = new Intent(context, DiscoverServerService.class);
        service.setAction(intent.getAction());

        if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))) {

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            wifiBroadcastAddress = (mWifi.isConnected()) ? getWiFiBroadcastAddress(context) : null;
            startService = wifiBroadcastAddress != null;
            service.putExtra("wifiBroadcastAddress", wifiBroadcastAddress);
        }

        if (startService)
            startWakefulService(context, service);

        if (intent.getBooleanExtra("stopservice", false)) {
            context.stopService(service);
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_startalarmrepeater))) {
        /**
         * start the alarm repeater only if WiFi is connected already
         * used by ServerListFragment.onServiceConnected method to start the discovery
         * if the application is launched being already connected to a WiFi network
         */
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {
            // if we're called from the activity, try to get a broadcast address
            if (intent.getBooleanExtra("forcegetbroadcast", false))
                wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_stopalarmrepeater))) {
        /**
         *  stop the alarm repeater. period.
         *  used by DrinViewerApplication.onTerminate method
         */
        wifiBroadcastAddress = null;
        stopAlarmRepeater(context);
    }
}

From source file:me.datenknoten.tueroeffner.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }// w ww  . j  av  a  2 s .co m

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    registerReceiver(new DerivBroadcastReceiver(this, networkSSID), intentFilter);
}

From source file:org.deviceconnect.android.deviceplugin.theta.activity.ThetaDeviceActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDeviceMgr = getDeviceManager();//from w w  w  . j  a  va  2 s  . c  o  m
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_LIST);
    if (fragment == null) {
        fragment = ThetaGalleryFragment.newInstance(mDeviceMgr);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(android.R.id.content, fragment, TAG_LIST);
        ft.commit();
    }
    mListener = getDeviceManager();
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    registerReceiver(mWifiReceiver, filter);
}

From source file:org.peterbaldwin.vlcremote.app.PickServerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PickServerFragment fragment = findOrReplaceFragment(android.R.id.content, TAG, PickServerFragment.class);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mPort = getIntent().getIntExtra(PortSweeper.EXTRA_PORT, 0);
    if (mPort == 0) {
        throw new IllegalArgumentException("Port must be specified");
    }/*from   w  w w  . j a v  a  2 s. c om*/

    mFile = getIntent().getStringExtra(PortSweeper.EXTRA_FILE);
    if (mFile == null) {
        throw new IllegalArgumentException("File must be specified");
    }

    mWorkers = getIntent().getIntExtra(PortSweeper.EXTRA_WORKERS, DEFAULT_WORKERS);

    mPortSweeper = createPortSweeper(fragment);
    startSweep();

    // Registering the receiver triggers a broadcast with the initial state.
    // To tell the difference between a broadcast triggered by registering a
    // receiver and a broadcast triggered by a true network event, note the
    // time and ignore all broadcasts for one second.
    mCreateTime = SystemClock.uptimeMillis();

    mReceiver = new MyBroadcastReceiver();

    // For robustness, update the connection status for all types of events.
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    registerReceiver(mReceiver, filter);
}

From source file:com.google.android.gms.samples.vision.face.facetracker.MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override/*w w  w  .  jav  a 2s .c om*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);

    //        ---Ambil username dari sharedpreference agar user tetap dapat melihat schedule jika keluar dari apps------
    sharedPreferences = getSharedPreferences("PHAROS", MODE_PRIVATE);
    username = sharedPreferences.getString("Username", "");
    //        ---------------------------------------------------------------------------------------------------------

    //        ---Deklarasi fragment fragment yang akan di gunakan---
    Fragment CameraFragment = new CameraFragment();
    Fragment LoginFramgent = new LoginFragment();
    Fragment ScheduleFragment = new ScheduleFragment();
    //        -----------------------------------------------------

    //        Meminta permisssion untuk GPS jika menggunakan android Marshmallow keatas
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
    }
    //        -------------------------------------------------------------------------

    //       ------- Instantiasi intent filter untuk broadcast recevier------
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    IntentFilter connectionIntentFilter = new IntentFilter();
    connectionIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    //        ---------------------------------------------------------------------

    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiConfiguration = new WifiConfiguration();

    //        Registrasikan broadcast receiver
    registerReceiver(new wifiEnabled(wifiManager), intentFilter);
    registerReceiver(new wifiConnecting(wifiManager), connectionIntentFilter);
    //        ---------------------------------------

    //       -------- Deklarasi view pager---------
    viewPager = (ViewPager) findViewById(R.id.fragmentFrame);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), CameraFragment, LoginFramgent,
            ScheduleFragment);
    viewPager.setAdapter(pagerAdapter);
    //        Mengarahkan halaman awal saat membuka aplikasi ke fragment index 1
    viewPager.setCurrentItem(1);
    //        -------------------------------------

}

From source file:com.wifi.brainbreaker.mydemo.spydroid.ui.HandsetFragment.java

@Override
public void onResume() {
    super.onResume();
    getActivity().bindService(new Intent(getActivity(), CustomHttpServer.class), mHttpServiceConnection,
            Context.BIND_AUTO_CREATE);
    getActivity().bindService(new Intent(getActivity(), CustomRtspServer.class), mRtspServiceConnection,
            Context.BIND_AUTO_CREATE);
    getActivity().registerReceiver(mWifiStateReceiver,
            new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
}

From source file:com.geniatech.client_phone.wifi.WifiStatusTest.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

    mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    registerReceiver(mWifiStateReceiver, mWifiStateFilter);

    setContentView(R.layout.wifi_status_test);

    updateButton = (Button) findViewById(R.id.update);
    updateButton.setOnClickListener(updateButtonHandler);

    mWifiState = (TextView) findViewById(R.id.wifi_state);
    mNetworkState = (TextView) findViewById(R.id.network_state);
    mSupplicantState = (TextView) findViewById(R.id.supplicant_state);
    mRSSI = (TextView) findViewById(R.id.rssi);
    mBSSID = (TextView) findViewById(R.id.bssid);
    mSSID = (TextView) findViewById(R.id.ssid);
    mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid);
    mIPAddr = (TextView) findViewById(R.id.ipaddr);
    mMACAddr = (TextView) findViewById(R.id.macaddr);
    mNetworkId = (TextView) findViewById(R.id.networkid);
    mLinkSpeed = (TextView) findViewById(R.id.link_speed);
    mScanList = (TextView) findViewById(R.id.scan_list);

}

From source file:com.hzl.administrator.wifi.WifiStatusTest.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

    mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
    mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    registerReceiver(mWifiStateReceiver, mWifiStateFilter);

    setContentView(R.layout.wifi_status_test);

    updateButton = (Button) findViewById(R.id.update);
    updateButton.setOnClickListener(updateButtonHandler);

    mWifiState = (TextView) findViewById(R.id.wifi_state);
    mNetworkState = (TextView) findViewById(R.id.network_state);
    mSupplicantState = (TextView) findViewById(R.id.supplicant_state);
    mRSSI = (TextView) findViewById(R.id.rssi);
    mBSSID = (TextView) findViewById(R.id.bssid);
    mSSID = (TextView) findViewById(R.id.ssid);
    mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid);
    mIPAddr = (TextView) findViewById(R.id.ipaddr);
    mMACAddr = (TextView) findViewById(R.id.macaddr);
    mNetworkId = (TextView) findViewById(R.id.networkid);
    mLinkSpeed = (TextView) findViewById(R.id.link_speed);
    mScanList = (TextView) findViewById(R.id.scan_list);

    mPingIpAddr = (TextView) findViewById(R.id.pingIpAddr);
    mPingHostname = (TextView) findViewById(R.id.pingHostname);
    mHttpClientTest = (TextView) findViewById(R.id.httpClientTest);

    pingTestButton = (Button) findViewById(R.id.ping_test);
    pingTestButton.setOnClickListener(mPingButtonHandler);
}