修改可以使用

This commit is contained in:
2025-07-17 22:46:35 +08:00
parent 4a7e96473a
commit 7f0df85123
114 changed files with 267690 additions and 266676 deletions

View File

@@ -41,6 +41,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class EMqttService extends Service implements NetworkUtils.OnNetworkStatusChangedListener {
private final static String TAG = "EMQTT消息";
@@ -65,10 +66,10 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
super.onCreate();
Logger.e(TAG, "服务创建成功");
NetworkUtils.registerNetworkStatusChangedListener(this);
scheduledExecutorService.scheduleWithFixedDelay(connectTask, 0, 10, TimeUnit.SECONDS);
// scheduledExecutorService.scheduleWithFixedDelay(connectTask, 0, 10, TimeUnit.SECONDS);
}
private final Runnable connectTask = this::doClientConnection;
// private final Runnable connectTask = this::doClientConnection;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
@@ -148,11 +149,11 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
*/
private String clientId = "";
private void init() {
private void init() throws MqttException {
UserBean user = CommonAppContext.getInstance().getUser();
if (mqttAndroidClient == null) {
clientId = "android-" + user.getUser_id() + "-" + MqttClient.generateClientId();
mqttAndroidClient = new MqttAndroidClient(this, HOST, clientId);
mqttAndroidClient = new MqttAndroidClient(this, HOST, clientId, MqttAndroidClient.Ack.AUTO_ACK);
}
mMqttConnectOptions = new MqttConnectOptions();
mMqttConnectOptions.setCleanSession(true);
@@ -161,16 +162,12 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
mMqttConnectOptions.setUserName("public");
mMqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
try {
mqttAndroidClient.connect(mMqttConnectOptions, null, iMqttActionListener);
mqttAndroidClient.setCallback(mqttCallback);
} catch (MqttException e) {
e.printStackTrace();
}
mqttAndroidClient.connect(mMqttConnectOptions, null, iMqttActionListener);
mqttAndroidClient.setCallback(mqttCallback);
}
private synchronized void doClientConnection() {
private synchronized void doClientConnection() throws MqttException {
Logger.e("doClientConnection", "doClientConnection");
if (!isAlreadyConnected() && isConnectIsNomarl()) {
releaseMQTT();
@@ -225,7 +222,7 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
MqttException mqttException = (MqttException) arg1;
Logger.e(TAG, "链接状态失败:" + mqttException.getMessage());
}
ThreadUtils.runOnUiThreadDelayed(connectTask, 1000);
// ThreadUtils.runOnUiThreadDelayed(connectTask, 1000);
}
};
@@ -236,7 +233,11 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
@Override
public void connectionLost(Throwable cause) {
Logger.e(TAG, "链接断开,请检查网络");
doClientConnection();
try {
doClientConnection();
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
@Override
@@ -363,7 +364,11 @@ public class EMqttService extends Service implements NetworkUtils.OnNetworkStatu
@Override
public void onConnected(NetworkUtils.NetworkType networkType) {
doClientConnection();
try {
doClientConnection();
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
@Override