How to check whether 3g is active or not in android

和自甴很熟 提交于 2019-12-05 09:38:44

another snippet from an applilcation I've written recently:

TelephonyManager telManager;    
telManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
int cType = telManager.getNetworkType();
String cTypeString;
switch (cType) {
        case 1: cTypeString = "GPRS"; break;
        case 2: cTypeString = "EDGE"; break;
        case 3: cTypeString = "UMTS"; break;
        case 8: cTypeString = "HSDPA"; break;
        case 9: cTypeString = "HSUPA"; break;
        case 10:cTypeString = "HSPA"; break;
        default:cTypeString = "unknown"; break;
}

Try this stuff,

    void checkConnectionStatus()
      {
       ConnectivityManager connMgr = (ConnectivityManager)
      this.getSystemService(Context.CONNECTIVITY_SERVICE);


      final android.net.NetworkInfo wifi =
      connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


      final android.net.NetworkInfo mobile =
      connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


      if( wifi.isAvailable() ){
      Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
      }
      else if( mobile.isAvailable() ){
      Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
      }
      else
      {Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
      }
}

first you need to check if is wifi or mobile network

than just call

(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType());

not that you could be on EDGE or GPRS or something so you can also do this

if (getSsTelephony().getNetworkType() >= TelephonyManager.NETWORK_TYPE_UMTS)
    return NETWORK_3G;
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

    NetworkInfo mMobile = connManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mMobile.isAvailable() == true) {
        Intent otherActivity = new Intent();
        mapActivity.setClass(getBaseContext(), other.class);
        startActivity(otherActivity);
    }

Don't forget to add the "ACCESS_NETWORK_STATE" permission in the AndroidManifext.xml file!

This will check if you have internet connection(3G):

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!