TelephonyManager.getNetworkType() 和 HSDPA 问题:(

发布于 2024-10-05 18:34:46 字数 849 浏览 5 评论 0原文

我一直在使用以下代码来确定设备正在使用哪个网络:

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;

它工作得很好,除非我连接到 HSDPA,在这种情况下它总是返回 0 结果,在我的情况下,这使我的软件认为它已经根本没有联系:(

任何知道发生了什么的人,对此有一些经验,最重要的是对这个问题有一些解决方案???

提前致谢

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 ???

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-10-12 18:34:46

HSDPA 还有一个枚举来

检查是否有连接并且还获得一个类型,我宁愿用户 getActiveNetworkInfo 和 已连接。没有连接时返回null。您还可以通过 getTypegetSubtypeName 方法,或者您可以与你的方法。

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.

清晰传感 2024-10-12 18:34:46

这是我正在使用的代码。它将安全地检查连接,然后返回连接类型。对于我的应用程序,我只关心 Wifi、移动或未连接,因此这就是此函数将返回的内容。根据自己的情况改变。

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

您将需要在应用程序清单中获得此权限

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文