Android连接
我已经创建了一个应用程序,它需要随时连接到远程套接字服务器 为此,我注册了一个Android连接,android,broadcastreceiver,connectivity,Android,Broadcastreceiver,Connectivity,我已经创建了一个应用程序,它需要随时连接到远程套接字服务器 为此,我注册了一个BroadcastReceiver以更改连接。 每次接收器启动时,应用程序都会与服务器断开连接并创建新连接。 它这样做是为了保持带电和连接的套接字连接 在我的手机和其他几部我检查过的手机中,它工作得很好。 但我在其他一些手机上发现,当连接到移动提供商的数据连接时,接收器经常被呼叫,而连接没有任何实际变化 有没有办法让我知道连接是否发生了真正的变化?此外,为什么接收器会频繁发射?好的,我找到了解决问题的方法,希望有机会能
BroadcastReceiver
以更改连接。
每次接收器启动时,应用程序都会与服务器断开连接并创建新连接。
它这样做是为了保持带电和连接的套接字连接
在我的手机和其他几部我检查过的手机中,它工作得很好。
但我在其他一些手机上发现,当连接到移动提供商的数据连接时,接收器经常被呼叫,而连接没有任何实际变化
有没有办法让我知道连接是否发生了真正的变化?此外,为什么接收器会频繁发射?好的,我找到了解决问题的方法,希望有机会能帮助其他人: 在我的广播接收器中更改连接:
@Override
public void onReceive(Context context, Intent intent) {
//my Application class where i store the last known state of connectivity
app = (AppVariables) context.getApplicationContext();
//new network state
networkState = ((NetworkInfo)intent.getExtras().get("networkInfo")).getState();
if (app.getLastNetworkState().equals(State.CONNECTED) && networkState.equals(State.DISCONNECTED))
{
Log.i("InternetConnectivity", "Internet Status Changed: disconnected");
if (app.getServerConnectionManager() != null)
app.getServerConnectionManager().stopServer(false, false);
if (app.getMainActivity() != null)
app.getMainActivity().showReconnectDialog();
app.setLastNetworkState(networkState);
}
else if (networkState.equals(State.CONNECTED))
app.setLastNetworkState(networkState);
}