TelephonyManager.getNetworkType() 和 HSDPA 问题:(
我一直在使用以下代码来确定设备正在使用哪个网络:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HSDPA 还有一个枚举来
检查是否有连接并且还获得一个类型,我宁愿用户 getActiveNetworkInfo 和 已连接。没有连接时返回null。您还可以通过 getType 和 getSubtypeName 方法,或者您可以与你的方法。
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.
这是我正在使用的代码。它将安全地检查连接,然后返回连接类型。对于我的应用程序,我只关心 Wifi、移动或未连接,因此这就是此函数将返回的内容。根据自己的情况改变。
您将需要在应用程序清单中获得此权限
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.
You will need this permission in your app's manifest