开发者

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

开发者 https://www.devze.com 2023-04-06 10:49 出处:网络
i am trying to check whether 3G is activ开发者_如何学Ce or not in my handset and after that i have to fire an Intent.

i am trying to check whether 3G is activ开发者_如何学Ce or not in my handset and after that i have to fire an Intent. So plz anybody help me out Thanks in advance:)


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消