我已经在我的应用程序中实现了聊天功能。聊天使用 socket.io 进行工作。我正在尝试解决当互联网关闭和恢复时发射器监听器正常工作的问题。目前,套接字已重新启动-已连接到互联网,但一旦互联网重新连接,听众就根本无法工作。请帮助大家!
最佳答案
您可以监听连接更改并再次进行连接吗?
在您的 list 中:
<receiver android:name=".ConnectivityReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
ConnectivityReceiver 代码如下:
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if ( activeNetInfo != null )
{
//do your reconection if it's lost
}
}
关于java - 当互联网关闭和恢复时,如何使 android 中的发射器监听器工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37003316/