Search code examples
androidandroid-5.0-lollipop

How can I receive a notification when the device loses network connectivity in android L (API 21)


I'm using this code to be notified when the connection is lost in API 20 and down.

registerReceiver(getConnectivityStateBroadcastReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

private class ConnectivityStateBroadcastReceiver extends BaseBroadcastReceiver {

    /**
     * @param userLoggedIn
     * @param context
     * @param intent
     */
    @Override
    protected void onReceive(Boolean userLoggedIn, Context context, Intent intent) {

        Bundle extras = intent.getExtras();
        boolean notConnected = extras.getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

        // DO something
    }
}

but it's not working in API 21.

How can I fix that? maybe it's got to do with ConnectivityManager.NetworkCallbak but I didn't find any example to how to use it. Thanks.


Solution

  • OK, so I figure out how to do it but would appreciate confirmation that this solution is the right one.

    All I did is add a call to this code in the onCreate of my application class

    /**
     *
     */
    @SuppressLint("NewApi")
    private void registerConnectivityNetworkMonitorForAPI21AndUp() {
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return;
        }
    
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
    
        connectivityManager.registerNetworkCallback(
                builder.build(),
                new ConnectivityManager.NetworkCallback() {
                    /**
                     * @param network
                     */
                    @Override
                    public void onAvailable(Network network) {
    
                        sendBroadcast(
                                getConnectivityIntent(false)
                        );
    
                    }
    
                    /**
                     * @param network
                     */
                    @Override
                    public void onLost(Network network) {
    
                        sendBroadcast(
                                getConnectivityIntent(true)
                        );
    
                    }
                }
    
        );
    
    }
    
     /**
     * @param noConnection
     * @return
     */
    private Intent getConnectivityIntent(boolean noConnection) {
    
        Intent intent = new Intent();
    
        intent.setAction("mypackage.CONNECTIVITY_CHANGE");
        intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, noConnection);
    
        return intent;
    
    }
    

    and in the IntentFilter that already monitoring my connectivity for API 20 and less I added this

    IntentFilter filter = new IntentFilter();
    
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    filter.addAction("mypackage.CONNECTIVITY_CHANGE");
    

    and now my already working broadcast receiver get notification about network changes in API 21 too.