问题
I am having a problem Running a network service when my app connects to a WiFi network. I am getting a the following exception,
java.net.SocketException: socket failed: ENONET (Machine is not on the network)
in the openPort() method bellow
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 the Broadcast Receiver only runs on when the main activity is showing. and it works fine on the first run but when I change the WiFi network this happens. Help world be greatly appreciated.
回答1:
Use Service which start service and stop service when network would be change or when stop network.Also check same port which is using release first and after that use that port.Do whatever task 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;
}
}
来源:https://stackoverflow.com/questions/40462955/android-machine-is-not-on-the-network