Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Connectivity

I've created an app which needs to be connected to a remote socket server at all time.

i'n order to do so I've registered a BroadcastReceiver for connectivity changes. Every time the Receiver fires - the app is disconnecting from the server and creating a new connection. it does that in order preserve a live and connected socket connection.

in my phone and few of the other phones I've checked it works great. but i some other phones I've found out that when connected to the Mobile Provider data connection - the Receiver is being called very often without any real change in the connectivity.

is there a way for me to find out if there's a real change in the connectivity? and besides, why the receiver fires so often ?

like image 362
Asaf Nevo Avatar asked Sep 04 '26 11:09

Asaf Nevo


1 Answers

ok i found a solution for my problem, hope i will help someone else sometime:

in my BroadCast recevier for Connectivity Changes:

@Override
public void onReceive(Context context, Intent intent) {
        //my Application class where i store the last known state of connectivity
    app = (AppVariables) context.getApplicationContext();
        //new network state
    networkState = ((NetworkInfo)intent.getExtras().get("networkInfo")).getState();
    if (app.getLastNetworkState().equals(State.CONNECTED) && networkState.equals(State.DISCONNECTED))
    {
        Log.i("InternetConnectivity", "Internet Status Changed: disconnected");
        if (app.getServerConnectionManager() != null)
            app.getServerConnectionManager().stopServer(false, false);
        if (app.getMainActivity() != null)
            app.getMainActivity().showReconnectDialog();
        app.setLastNetworkState(networkState);
    }
    else if (networkState.equals(State.CONNECTED))
        app.setLastNetworkState(networkState);
}
like image 108
Asaf Nevo Avatar answered Sep 06 '26 02:09

Asaf Nevo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!