java - Android机器不在网络上

标签 java android serversocket

当我的应用程序连接到 WiFi 网络时,我在运行网络服务时遇到问题。 我收到以下异常,

java.net.SocketException: socket failed: ENONET (Machine is not on the network) 在下面的 openPort() 方法中

广播接收器

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {

        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

        if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
            welcomeService = new BroadcastService(context, "Welcome");
            Log.i(TAG, "onReceive: SUPPLICANT-STATE ---> Connected");
            //do something
            if (!serviceRegistered) {
                welcomeService.registerService();
                serviceRegistered = true;
            }
        }

        if (networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {
            Log.i(TAG, "onReceive: SUPPLICANT-STATE ---> Disconnected");
            //do something
            unRegisterService();
        }
    }

}

public void unRegisterService() {
    if (serviceRegistered && welcomeService != null) {
        welcomeService.unregisterService();
        serviceRegistered = false;

    }
}

广播服务

public void registerService() {
    NsdServiceInfo serviceInfo  = new NsdServiceInfo();
    serviceInfo.setServiceName(mServiceName);
    serviceInfo.setServiceType("_http._tcp.");
    serviceInfo.setPort(openPort());

    mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);

    mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}

private int openPort() {
    try{
        // Line that throws the exception
        return  new ServerSocket(0).getLocalPort();
    }catch (IOException ioe){
        Crashlytics.logException(ioe);
        ioe.printStackTrace();
        return 0;
    }
}

广播接收器仅在主 Activity 显示时运行。它在第一次运行时运行良好,但当我更改 WiFi 网络时,就会发生这种情况。帮助世界得到极大的赞赏。

最佳答案

当网络发生变化或停止网络时,使用启动服务和停止服务的服务。同时检查首先使用版本的相同端口,然后使用该端口。在 onStartCommand().

@Override
public void onReceive(Context context, Intent intent) {
  Date now = new Date();
  Long time = now.getTime();
  String action = intent.getAction();



  NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

  if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {


    boolean check = isMyServiceRunning(context,
    service.getName());
    if (check) {
      Intent i = new Intent(sqlitewraper.context,service.class);
      stopService(i);
    }
    else{
      Intent i = new Intent(sqlitewraper.context,service.class);
      startservice(i);

    }

  }

  if (networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {

    boolean check = isMyServiceRunning(context,
    service.getName());
    if (check) {
      Intent i = new Intent(sqlitewraper.context,service.class);
      stopService(i);
    }
  }


  private boolean isMyServiceRunning(Context context, String Myservice) {
    ActivityManager manager = (ActivityManager) context
    .getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
    .getRunningServices(Integer.MAX_VALUE)) {
      if (Myservice.equals(service.service.getClassName())) {

        return true;
      }
    }
    return false;
  }
}

关于java - Android机器不在网络上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462955/

相关文章:

java - .notify() 没有通知线程的 .wait()

java - Eclipse:如何在我的自定义调试器中对我的源代码启用监视和检查操作?

java - 如何即时从类定义中删除方法?

android - 如何在我制作的位图按钮 java 源代码中找到布局的 ID

python - __init__ OSError : [Errno 24] Too many open files中的 “/usr/lib/python3.5/socket.py”文件,第134行

java - 将 LinearLayout 调整为 ImageView 的宽度

android - 即使可见性设置为消失,FloatingActionButton 仍可见一段时间

android - 在触摸屏上检测移动 imageview x 位置

java - 为什么我的客户端 Socket 无法连接到我的 ServerSocket?

java - Actionscript 游戏/程序与他人联系