0

My Application send SMS only if Mobile Network is available. Currently i have set ON WIFI as well MOBILE NETWORK is present.

The following code snippet when executed gives me:

public boolean isNetworkAvailable(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);        
    // WIFI is ON and MOBILE Network is present.
    final NetworkInfo mobileNetwork = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    final State mobileState = mobileNetwork.getState();

    if(mobileNetwork!=null)
    {
        // RETURNS FALSE
        Log.d("Contacts","mobileNetwork.isConnected() "+mobileNetwork.isConnected());
        // RETURNS FALSE
        Log.d("Contacts","isConnectedOrConnecting() "+mobileNetwork.isConnectedOrConnecting());
        // RETURNS TRUE
        Log.d("Contacts","mobileNetwork.isAvailable()() "+mobileNetwork.isAvailable());
        return mobileNetwork.isAvailable();
    }

    return false;

}

The question is how to detect now whether i can send SMS or not based on the value returned by the above three lines?

Since isAvailable() returns true and the other 2 lines returns false;

SOLUTION

i have came up with this code:

TelephonyManager telMgr =   (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    int simCardState = telMgr.getSimState();        
    int simNetworkType = telMgr.getNetworkType();
    int simDataState = telMgr.getDataState();   
    if(simCardState == TelephonyManager.SIM_STATE_READY && simDataState == TelephonyManager.DATA_CONNECTED)
    {
        //NETWORK IS AVAILABLE FOR SENDING SMS
    }
Ravibhushan
  • 171
  • 3
  • 14
  • i believe this should help http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android – waqaslam Mar 07 '12 at 09:27
  • @Waqas Thanks for the links however irrespective of whether the network is 2g, 3g, etc, the above code returns same result. My intention is to detect mobile network presence, the above result returned are confusing (false, false, true). – Ravibhushan Mar 07 '12 at 11:00

1 Answers1

0

this code should help you to detect different states

TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int simCardState = telMgr.getSimState();
switch (simCardState) {
   case TelephonyManager.SIM_STATE_ABSENT:
       // do something
       break;
   case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
       // do something
       break;
   case TelephonyManager.SIM_STATE_PIN_REQUIRED:
       // do something
       break;
   case TelephonyManager.SIM_STATE_PUK_REQUIRED:
       // do something
       break;
   case TelephonyManager.SIM_STATE_READY:
       // here you may set a flag that the phone is ready to send SMS
       break;
   case TelephonyManager.SIM_STATE_UNKNOWN:
       // do something
       break;
}
waqaslam
  • 67,549
  • 16
  • 165
  • 178