androidandroid-volleyjob-queue

How to Call a method every time when Network is Available in Android?


Scenario is When my Application is offline, i am Storing Some information in database which i need to send to server. So when Network is available i want to send those by Api calls. Is there any way to call a method every time when Network is available??


Solution

  • create broadcastReceiver:

    public class InternetBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "InternetReceiver";
    private static int networkType = -1;
    private static boolean connectedWithNetwork = false;
    
    public static void initNetworkStatus(Context context) {
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        networkType = -1;
        if (networkInfo != null) {
            Log.d(TAG, "Init: ACTIVE NetworkInfo: " + networkInfo.toString());
            if (networkInfo.isConnected()) {
                networkType = networkInfo.getType();
            }
        }
        Log.d(TAG, "initNetworkStatus -> " + networkType);
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive " + intent.getAction());
    
        try {
            if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
                Log.d(TAG, "System shutdown, stopping yaxim.");
            } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
    
    
                ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    
                boolean isConnected = (networkInfo != null) && (networkInfo.isConnected() == true);
                boolean wasConnected = (networkType != -1);
    
                if (connectedWithNetwork && isConnected)
                    return;
    
                connectedWithNetwork = isConnected;
    
                if (wasConnected && !isConnected) {
                    Log.d(TAG, "we got disconnected");
                    networkType = -1;
    
                } else if (isConnected && (networkInfo.getType() != networkType)) {
                    Log.d(TAG, "we got (re)connected: " + networkInfo.toString());
                    networkType = networkInfo.getType();
                    try {
                        Intent bIntent = new Intent("custom-event-name");
                        bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (isConnected && (networkInfo.getType() == networkType)) {
                    Log.d(TAG, "we stay connected, sending a ping");
                    try {
                        Intent bIntent = new Intent("custom-event-name");
                        bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else
                    return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
    

    and register in manifest file

    <receiver android:name=".packagename.InternetBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>