gpt4 book ai didi

java - Android机器不在网络上

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:50 26 4
gpt4 key购买 nike

当我的应用程序连接到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com