Example usage for android.net ConnectivityManager CONNECTIVITY_ACTION

List of usage examples for android.net ConnectivityManager CONNECTIVITY_ACTION

Introduction

In this page you can find the example usage for android.net ConnectivityManager CONNECTIVITY_ACTION.

Prototype

String CONNECTIVITY_ACTION

To view the source code for android.net ConnectivityManager CONNECTIVITY_ACTION.

Click Source Link

Document

A change in network connectivity has occurred.

Usage

From source file:com.lonepulse.icklebot.activity.support.NetworkFragmentActivity.java

/**
 * <p>Registers a {@link BroadcastReceiver} to listen for changes in the data 
 * connection state and invoke the appropriate callbacks.
 *///  w w w  . j  av  a 2s.com
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (ProfileService.getInstance(getApplicationContext()).isActive(this, Profile.NETWORK)) {

        if (!PermissionUtils.isGranted(this, Manifest.permission.ACCESS_NETWORK_STATE)) {

            Log.e(getClass().getSimpleName(), "Failed to register a receiver for changes in network state. ",
                    new IckleBotRuntimeException(new PermissionDeniedException(
                            Manifest.permission.ACCESS_NETWORK_STATE, Profile.NETWORK)));
        } else {

            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

            registerReceiver(networkStateReceiver, intentFilter);
        }
    }
}

From source file:org.lsc.hellocordova.BluetoothPlugin.java

@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
    Log.d("BluetoothPlugin", "Plugin Called");
    PluginResult result = null;/* www  .j a v a  2s  . com*/
    context = (Context) this.ctx;

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery starts
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when connectivity state changes
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);

    Looper.prepare();
    btadapter = BluetoothAdapter.getDefaultAdapter();
    found_devices = new ArrayList<BluetoothDevice>();

    if (ACTION_DISCOVER_DEVICES.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES);

            found_devices.clear();
            discovering = true;

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            Log.i("BluetoothPlugin", "Discovering devices...");
            btadapter.startDiscovery();

            while (discovering) {
            }

            String devicesFound = null;
            int count = 0;
            devicesFound = "[";
            for (BluetoothDevice device : found_devices) {
                Log.i("BluetoothPlugin",
                        device.getName() + " " + device.getAddress() + " " + device.getBondState());
                if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                    devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ,"
                            + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                            + device.getBluetoothClass().getDeviceClass() + "\" }";
                    if (count < found_devices.size() - 1)
                        devicesFound = devicesFound + ",";
                } else
                    Log.i("BluetoothPlugin",
                            device.getName() + " Problems retrieving attributes. Device not added ");
                count++;
            }

            devicesFound = devicesFound + "] ";

            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound);
            result = new PluginResult(Status.OK, devicesFound);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED);

            boolean isEnabled = btadapter.isEnabled();

            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED,
                    "Returning " + "is Bluetooth Enabled? " + isEnabled);
            result = new PluginResult(Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_ENABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT);

            boolean enabled = false;

            Log.d("BluetoothPlugin", "Enabling Bluetooth...");

            if (btadapter.isEnabled()) {
                enabled = true;
            } else {
                enabled = btadapter.enable();
            }

            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled);
            result = new PluginResult(Status.OK, enabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_DISABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT);

            boolean disabled = false;

            Log.d("BluetoothPlugin", "Disabling Bluetooth...");

            if (btadapter.isEnabled()) {
                disabled = btadapter.disable();
            } else {
                disabled = true;
            }

            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled);
            result = new PluginResult(Status.OK, disabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean paired = false;

            Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName()
                    + " and address " + device.getAddress());

            try {
                Method m = device.getClass().getMethod("createBond");
                paired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired);
            result = new PluginResult(Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_UNPAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean unpaired = false;

            Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("removeBond");
                unpaired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired);
            result = new PluginResult(Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES);

            Log.d("BluetoothPlugin", "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i("BluetoothPlugin",
                            device.getName() + " " + device.getAddress() + " " + device.getBondState());

                    if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                        resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ,"
                                + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                                + device.getBluetoothClass().getDeviceClass() + "\" }";
                        if (count < pairedDevices.size() - 1)
                            resultBoundDevices = resultBoundDevices + ",";
                    } else
                        Log.i("BluetoothPlugin",
                                device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices);
            result = new PluginResult(Status.OK, resultBoundDevices);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT);

            boolean stopped = true;

            Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices...");

            if (btadapter.isDiscovering()) {
                Log.i("BluetoothPlugin", "Stop discovery...");
                stopped = btadapter.cancelDiscovery();
                discovering = false;
            }

            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped);
            result = new PluginResult(Status.OK, stopped);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BOUND_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT);
            String addressDevice = arg1.getString(0);
            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState());

            boolean state = false;

            if (device != null && device.getBondState() == 12)
                state = true;
            else
                state = false;

            Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress());

            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state);
            result = new PluginResult(Status.OK, state);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_OPEN_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_OPEN_SOCKET);
        try {
            String addressDevice = arg1.getString(0);
            openSocket(addressDevice);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_OPEN_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_MESSAGE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_MESSAGE_SOCKET);
        try {
            String msg = arg1.getString(0);
            sendData(msg);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_MESSAGE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_CLOSE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_CLOSE_SOCKET);
        try {
            closeSocket();
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_CLOSE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PING_SOCKET.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_PING_SOCKET);

            String data = "{";
            data = data + "\"count\": " + Integer.toString(pingCount) + ",";

            String items = "";
            for (String item : pingData) {
                items = items + ",\"" + item + "\"";
            }

            data = data + "  \"data\" : [" + items.substring(1) + " ]";
            data = data + "}";

            result = new PluginResult(Status.OK, data);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PING_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    }

    else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("BluetoothPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}

From source file:de.persoapp.android.activity.dialog.NoInternetConnectionDialog.java

@SuppressWarnings("ConstantConditions")
@Override/*from  ww  w.  j a  v a  2 s  .c  om*/
public void onResume() {
    super.onResume();

    if (mNetworkHelper.isNetworkAvailable()) {
        closeDialog();

    } else {
        AlertDialog dialog = (AlertDialog) getDialog();

        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            }
        });

        getActivity().registerReceiver(mBroadcastReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        mRegistered = true;
    }
}

From source file:com.lonepulse.icklebot.fragment.support.NetworkFragment.java

/**
 * <p>Registers a {@link BroadcastReceiver} to listen for changes in the data 
 * connection state and invoke the appropriate callbacks.
 *///from w w w . j  a  v  a 2  s . co  m
@Override
public void onResume() {

    super.onResume();

    if (ProfileService.getInstance(getActivity().getApplicationContext()).isActive(this, Profile.NETWORK)) {

        if (!PermissionUtils.isGranted(this, Manifest.permission.ACCESS_NETWORK_STATE)) {

            Log.e(getClass().getSimpleName(), "Failed to register a receiver for changes in network state. ",
                    new IckleBotRuntimeException(new PermissionDeniedException(
                            Manifest.permission.ACCESS_NETWORK_STATE, Profile.NETWORK)));
        } else {

            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

            getActivity().registerReceiver(networkStateReceiver, intentFilter);
        }
    }
}

From source file:com.polyvi.xface.extension.XNetworkConnectionExt.java

/**
 * ? plugin  NetworkConnection .//from w w  w  . j  a v  a  2  s.  com
 *  Android ??BroadcastReceiver Android  broadcast
 *
 */
public void initConnectionContext() {
    Context context = getContext();
    this.mSockMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // ????connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    if (this.mReceiver == null) {
        this.mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateConnectionInfo(
                        (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
            }
        };
        context.registerReceiver(this.mReceiver, intentFilter);
    }
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public void registerConnectivityInfoListener(IConnectivityListener connectivityListener) {
    if (connectivityListenersList.isEmpty()) {
        connectivityChangeReceiver = new ConnectivityChangeReceiver();
        appInstance.registerReceiver(connectivityChangeReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }/*w ww. j  ava2s. co  m*/
    connectivityListenersList.add(connectivityListener);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        notifyListeners(networkInfo);
    }

}

From source file:it.unicaradio.android.services.StreamingService.java

private void initReceivers() {
    registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    registerReceiver(telephonyReceiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    registerReceiver(noisyAudioStreamReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
}

From source file:android.net.http.cts.ApacheHttpClientTest.java

private void connectToWifi() throws InterruptedException {
    if (!mWifiManager.isWifiEnabled()) {
        ConnectivityActionReceiver receiver = new ConnectivityActionReceiver(ConnectivityManager.TYPE_WIFI,
                State.CONNECTED);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(receiver, filter);

        assertTrue(mWifiManager.setWifiEnabled(true));
        assertTrue("Wifi must be configured to connect to an access point for this test.",
                receiver.waitForStateChange());

        mContext.unregisterReceiver(receiver);
    }//from ww  w.  ja  v a2s.c om
}

From source file:net.micode.fileexplorer.ServerControlActivity.java

public void onResume() {
    super.onResume();

    UiUpdater.registerClient(handler);/*from   w ww  . j a  v  a  2 s. c om*/
    updateUi();
    // Register to receive wifi status broadcasts
    myLog.l(Log.DEBUG, "Registered for wifi updates");
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    mActivity.registerReceiver(wifiReceiver, filter);
}

From source file:com.google.android.DemoKit.DemoKitActivity.java

/** Called when the activity is first created. */
@Override/*  w ww .j  a v a 2 s .  co  m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    context = getApplicationContext();

    //      Toast.makeText(this, "inside onCreate of DemoKitActivity", Toast.LENGTH_SHORT).show();

    IntentFilter connFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    mConnReceiver = new ConnBroadcastReceiver();

    registerReceiver(mConnReceiver, connFilter);

    setContentView(R.layout.main);

    enableControls(true);

    mNocServiceRequestor = new PeriodicScheduler(new Runnable() {
        @Override
        public void run() {
            if (mNocAccessServiceMessenger != null) {
                toastHandler("Trying to upload Sensor Data");
                //               if(mConnReceiver.isConnected) {
                Message msg = Message.obtain(null, NocMessages.URL_MSG, 0, 0);
                Bundle msgBundle = new Bundle();
                msgBundle.putString("url",
                        "http://98.26.23.101/update.php?req=UI:WLDCTLAB1;FV:1.36;E1:0;EC1:0;E2:0;EC2:0;FL:2937;FC:0;BA:1218;TO:2181;TC:2181;FL20:0;FE:0;BL:0;HTO:0;HTC:0;BP1:0;BP2:0;GF:0;UF:1;IN:0;DT:0;TCF:0;TOF:0");
                msg.setData(msgBundle);
                try {
                    mNocAccessServiceMessenger.send(msg);
                } catch (RemoteException e) {

                }

            } else {
                toastHandler("mNocAccessServiceMessenger is null!");
            }

        }
    }, 5000);

    mBTMsgHandler = new Messenger(new BluetoothMsgHandler());
    mAccessoryMsgHandler = new Messenger(new ServiceMsgHandler());
    mNocMsgHandler = new Messenger(new ServiceMsgHandler());
}