Handiling断开连接套接字 [英] Handiling disconnected Sockets

查看:132
本文介绍了Handiling断开连接套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序实现的插座和ofcourse它与服务器通信,我的问题是,我不能确定何时插座下降在下列情况下。

1)IAM连接WiFi网络具有活跃的互联网连接,并具有积极的套接字连接和我的互联网连接出现故障

2),当我连接到2G / 3G互联网连接,有时插座连接断开

在上述条件我不能够确定是否有活动的套接字connection.Is那里android系统中实现这一目标的任何方法?

请帮忙...


解决方案

看看Android的的确定和监测的连接状态


  

您可以使用 ConnectivityManager 来检查你真的是连接到互联网,并且如果是这样,是代替什么类型的连接。


有一些例如code:

  ConnectivityManager厘米=
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
的NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
布尔isConnected = activeNetwork = NULL&放大器;!&安培;
                      activeNetwork.isConnectedOrConnecting();

此外,


  

该ConnectivityManager广播每当连接细节已经改变了CONNECTIVITY_ACTION(android.net.conn.CONNECTIVITY_CHANGE)的行动。您可以在清单注册一个广播接收器监听这些变化和恢复(或暂停),你的背景相应地更新。

 <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>


I have implemented socket in my app and ofcourse it's communicating with server and my problem is I am not able to identify when the socket goes down in following scenarios.

1) iam connected to wifi having an active internet connection and have active socket connection and my internet connection goes down

2) when am connected to 2g/3g internet connection , sometimes socket connection goes down

In above conditions i am not able to identify whether there is an active socket connection.Is there any method for achieving this in android?

please help...

解决方案

Take a look at Android's Determining and Monitoring the Connectivity Status:

You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place.

There's some example code:

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

Also,

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

这篇关于Handiling断开连接套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆