Question

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 ?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top