Search code examples
androidvoip

Is there any way to check if 4g network is stabilized on Android?


I am making a VoIP client and I wanted to make the client to call to the last phone number it was talking to when 4G is connected. It doesn't automatically call if user pressed "end call" button. It only happens if connection was dropped and user couldn't press end call to finish the phone call.

So I have a BroadcastReceiver and it tells me if Internet connection is available or not pretty well. Except it tells me too much.

The phone's behavior I saw was when there's no network and it gets the connectivity from 4G network, onReceive(..) method in BroadcastReceiver is triggered about 8 times, with [connected, NOT connected, connected, NOT connected,...., connected].

Here's the code that listens to the connectivity.

@Override
public void onReceive( Context context, Intent intent ) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    NetworkInfo an = cm.getActiveNetworkInfo();

    if ( an != null ) {
        // this method just decides if it is WIFI or fast network like
        // 4G or HSPAP. 
        state = updateConnectionAvailability(context, an, tm);

        // 1: 4G/HSPAP, 3: WIFI
        if (state == 1 || state == 3) {
            Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show();
        } else {
            Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show();
        }

        if (mCCL != null) {
            mCCL.onVoipAvailable();
        }
    } else {
        Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show();
        if (mCCL != null) {
            mCCL.onVoipNotAvailable();
        }
    }
}

as you may have noticed, I created a custom Listener mCCL. It simply notifies the connectivity to the MainActivity. But then again, there is a series of onVoipAvailable() and onVoipNotAvailable() called once 4G is established.

My goal is to find out if 4G is established pretty well. It is very vague, but I can't word it differently. :-(

What should I do?


Solution

  • In my app, I do avoid duplicate broadcast. However, the sequence I receive is several "connected" for the same network, or several "disconnected" for the same network:

    public NetworkInfo previous_network = null;
    
    @Override
    public void onReceive( Context context, Intent intent ) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        NetworkInfo an = cm.getActiveNetworkInfo();
    
        ///AVOID EXTRA BROADCAST
        if (previous_network == null && an==null) {
            Log.i(mTag, "detected duplicate broadcast - no network");
            return;
        }
        if (previous_network != null && an != null) {
            if (an.toString().compareTo(previous_network.toString()) == 0) {
                Log.i(mTag, "detected duplicate broadcast - no change");
                return;
            }
        }
        previous_network = an;
        ///END OF AVOID EXTRA BROADCAST
    
        if ( an != null ) {
            // this method just decides if it is WIFI or fast network like
            // 4G or HSPAP. 
            state = updateConnectionAvailability(context, an, tm);
    
            // 1: 4G/HSPAP, 3: WIFI
            if (state == 1 || state == 3) {
                Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show();
            } else {
                Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show();
            }
    
            if (mCCL != null) {
                mCCL.onVoipAvailable();
            }
        } else {
            Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show();
            if (mCCL != null) {
                mCCL.onVoipNotAvailable();
            }
        }
    }