开发者

TelephonyManager.getNetworkType() and HSDPA problems :(

开发者 https://www.devze.com 2023-01-28 17:10 出处:网络
I have been using the following code to establish which network the device is using : TelephonyManager tempManager;

I have been using the following code to establish which network the device is using :

TelephonyManager tempManager;
tempManager= (TelephonyManager)myContext.getSystemService(Context.TELEPHONY_SERVICE);
int result = 0;
    if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) //do we have a UMTS connection ?
  {
   result = 2;
  }
  else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS) //or is it just a shabby 2g connection ?
  {
   result = 1;
  }
  else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UNKNOWN) //or is it just a shabby 2g connection ?
  {
   result = 4;
  }
return result;

It works pretty well unless I get on a HSDPA connection, in that case it will always return 0 as a result, which in my case makes my software think it has no connection at all :(

Anyone who knows whats happening, has some experience regarding this and most importantly has some solution to this problem ???

Tha开发者_如何学Pythonnks in advance


There is a enum also for HSDPA

To check if there is connection and also get a type, I would rather user getActiveNetworkInfo and isConnected. It returns null when there is no connection. You can also check the type of connection by getType and getSubtypeName methods or you can mix with your approach.


This is the code I am using. It will safely check for a connection and then return the connection type. For my application, I only care about Wifi, Mobile, or not connected so that is what this function will return. Change for your own situation.

//Check weather Internet connection is available or not
public int checkConnectionType()
{
    final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetInfo = conMgr.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.isAvailable() && activeNetInfo.isConnected())
    {
        int type = activeNetInfo.getType();
        if (type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_MOBILE_DUN
                || type == ConnectivityManager.TYPE_MOBILE_HIPRI || type == ConnectivityManager.TYPE_MOBILE_MMS
                || type == ConnectivityManager.TYPE_MOBILE_SUPL || type == ConnectivityManager.TYPE_WIMAX)
        {
            return ConnectivityManager.TYPE_MOBILE;
        }
        else if (type == ConnectivityManager.TYPE_WIFI)
        {
            return ConnectivityManager.TYPE_WIFI;
        }
        else
        {
            // Unknown connection type, so to be safe say mobile
            return ConnectivityManager.TYPE_MOBILE;
        }
    }
    else
    {
        // return not connected
        return -1;
    }
}

You will need this permission in your app's manifest

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
0

精彩评论

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