You can write your own BroadcastReceiver. Your recipient will receive the connection changes and inform your desired instance of the change (for example, your own CommunicationManager):
public class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i(getClass().getName(), "A change in network connectivity has occurred. Notifying communication manager for further action."); NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); if(info != null) { Log.v(getClass().getName(), "Reported connectivity status is " + info.getState() + "."); } CommunicationManager.updateConnectivityState();
For example, your CommunicationManager instance that will be notified of connectivity:
protected static void updateConnectivityState() { boolean isConnected = false; if (_connec != null && (_connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(_connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ isConnected = true; Log.i(CommunicationManager.class.getName(), "Device is connected to the network. Online mode is available."); }else if (_connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || _connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) { isConnected = false; Log.w(CommunicationManager.class.getName(), "Device is NOT connected to the network. Offline mode."); } _isConnected = isConnected; }
Check out NetworkInfo for more information on connection availability.
Remember to register ACCESS_NETWORK_STATE resolution in the manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Hope this helps. Relations
saxos source share