如何以编程方式处理 WiFi 到移动网络的切换?
现在,我的应用程序可以与 WiFi 配合使用,但是当我要使用移动提供商网络时,我的应用程序无法运行。我维护了一项检查网络的后台服务,但我不知道如何处理网络切换 WiFi 到移动设备和移动设备到 WiFi?我不知道如何处理 WiFi 到移动网络的切换,因为 WiFi 已经启用并且我不在 WiFi 覆盖区域;在这种情况下,我想自动切换到移动网络,反之亦然。我的方法如下,但不起作用:
String networkStatus = "disconnected";
int netType = 0;
try{
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager != null ){
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null){
netType = networkInfo.getType();
Log.d("Log", "connetion is available");
}else {
Log.d("Log", "connetion is not available");
return networkStatus;
}
// if(networkInfo.isAvailable()){ // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){ // New change added here
if(netType == ConnectivityManager.TYPE_WIFI)
{}
else if(netType == ConnectivityManager.TYPE_MOBILE )
{}
}
}
}catch(Exception e){
Log.d("Log", "checkNetworkConnection" + e.toString());
return networkStatus;
}
我已经阅读了很多帖子,但仍然不明白。谁能给我任何想法或网址,我可以在其中获得相同的实施方法?
提前致谢。
Right now, I am having application which works with WiFi, but while I am going to mobile providers network my application does not working. I have maintained one background service which checks for network, but im not getting how to handle network switch WiFi to Mobile and Mobile to WiFi? I am not getting how to handle WiFi to mobile network switch because already WiFi is enabled and I am not in WiFi coverage area; in this situation I want to get switched to mobile network automatically and vice-versa. My approach is as follows which is not working:
String networkStatus = "disconnected";
int netType = 0;
try{
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager != null ){
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null){
netType = networkInfo.getType();
Log.d("Log", "connetion is available");
}else {
Log.d("Log", "connetion is not available");
return networkStatus;
}
// if(networkInfo.isAvailable()){ // Old one
if(networkInfo.isAvailable() && networkInfo.isConnected()){ // New change added here
if(netType == ConnectivityManager.TYPE_WIFI)
{}
else if(netType == ConnectivityManager.TYPE_MOBILE )
{}
}
}
}catch(Exception e){
Log.d("Log", "checkNetworkConnection" + e.toString());
return networkStatus;
}
Already I have read many posts over hear still not getting idea. Can anyone give me any idea or url where I can get same approach to implement?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了解决方案:
I got the solution:
http://thiranjith.wordpress。 com/2011/03/31/how-to-monitor-network-connectivity-in-android/
希望这会对您有所帮助。
http://thiranjith.wordpress.com/2011/03/31/how-to-monitor-network-connectivity-in-android/
Hope this will help you..