Android machine is not on the network

I had a problem Starting a network service when my application connects to a WiFi network. I get the following exception,

java.net.SocketException: socket failed: ENONET (Machine is not on the network) in the openPort () method below

BroadcastReceiver

 @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; } } 

BroadcastService

 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; } } 

This broadcast receiver only works when the main activity is shown. and it works great the first time it starts, but when I change the WiFi network, this happens. The world of help is commendable.

+5
source share
1 answer

Use the Service, which starts the service and stops the service when the network changes or when the network stops. Also check the same port that the release uses, and then use that port. What task do you want in 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; } } 
0
source

Source: https://habr.com/ru/post/1259398/


All Articles