时间修改
This commit is contained in:
parent
ee7806b79f
commit
01eee8a06f
|
|
@ -53,6 +53,7 @@ import com.bonus.canteen.fragment.PalmFragment;
|
|||
import com.bonus.canteen.fragment.StartFragment;
|
||||
import com.bonus.canteen.utils.DateTimeHelper;
|
||||
import com.bonus.canteen.utils.OkHttpService;
|
||||
import com.bonus.canteen.utils.PingUtil;
|
||||
import com.bonus.canteen.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.utils.WorkConfig;
|
||||
import com.bonus.canteen.utils.listener.DebounceClickListener;
|
||||
|
|
@ -106,6 +107,7 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> implements C
|
|||
super.onCreate(savedInstanceState);
|
||||
setFullScreen();
|
||||
getServerTime();
|
||||
// setEthEnabled();
|
||||
initView();
|
||||
initTab();
|
||||
startUpdateTime();
|
||||
|
|
@ -119,6 +121,34 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> implements C
|
|||
updateView(1);
|
||||
});
|
||||
initPort();
|
||||
|
||||
}
|
||||
|
||||
private void setEthEnabled(){
|
||||
MyManager manager = MyManager.getInstance(this);
|
||||
manager.ethEnabled(false);
|
||||
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.postDelayed(() -> {
|
||||
manager.ethEnabled(true);
|
||||
// 3. 开启网口后,不能立即Ping,需要给系统约5-10秒时间获取IP并连接链路
|
||||
handler.postDelayed(this::performPingCheck, 10000);
|
||||
}, 5000);
|
||||
}
|
||||
/**
|
||||
* 执行 Ping 验证
|
||||
*/
|
||||
private void performPingCheck() {
|
||||
PingUtil.ping("192.168.20.234", 3, (isSuccess, rtt, message) -> {
|
||||
if (isSuccess) {
|
||||
Log.d(TAG, "Ping 成功!网络已恢复。RTT: " + rtt + "ms");
|
||||
// 在这里可以通知 UI 或执行后续业务逻辑
|
||||
} else {
|
||||
Log.e(TAG, "Ping 失败,准备重新重置网口...");
|
||||
// 4. 如果 Ping 不通,递归调用重置流程
|
||||
setEthEnabled();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getServerTime() {
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ public class DateTimeHelper {
|
|||
return format(new Date(),"HH:mm:ss");
|
||||
}
|
||||
public static String getTime(){
|
||||
return format(new Date(),"YYYY-MM-dd HH:mm:ss");
|
||||
return format(new Date(),"yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
public static String getNowDate(){
|
||||
return format(new Date(),"YYYY-MM-dd");
|
||||
return format(new Date(),"yyyy-MM-dd");
|
||||
}
|
||||
public static String getCurrentTimeStr(){
|
||||
return format(new Date(),"yyyyMMddHHmmssSSS");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2026 xuexiangjys(xuexiangjys@163.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.bonus.canteen.utils;
|
||||
|
||||
public interface PingCallback {
|
||||
/**
|
||||
* @param isSuccess 是否 Ping 通
|
||||
* @param rtt 往返时间 (ms),如果失败为 0
|
||||
* @param message 完整的日志信息或错误信息
|
||||
*/
|
||||
void onComplete(boolean isSuccess, float rtt, String message);
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (C) 2026 xuexiangjys(xuexiangjys@163.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.bonus.canteen.utils;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class PingUtil {
|
||||
|
||||
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
/**
|
||||
* 执行 Ping 操作
|
||||
* @param ip 目标 IP 地址 (如 "192.168.20.234")
|
||||
* @param count ping 的次数
|
||||
* @param callback 回调
|
||||
*/
|
||||
public static void ping(final String ip, final int count, final PingCallback callback) {
|
||||
executor.execute(() -> {
|
||||
StringBuilder log = new StringBuilder();
|
||||
boolean isSuccess = false;
|
||||
float rtt = 0;
|
||||
|
||||
try {
|
||||
// -c: 次数, -W: 超时时间(秒)
|
||||
String command = "/system/bin/ping -c " + count + " -W 2 " + ip;
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
log.append(line).append("\n");
|
||||
// 简单解析 RTT (形如 time=12.3 ms)
|
||||
if (line.contains("time=")) {
|
||||
String timePart = line.substring(line.indexOf("time=") + 5);
|
||||
String value = timePart.split(" ")[0];
|
||||
rtt = Float.parseFloat(value);
|
||||
}
|
||||
}
|
||||
|
||||
// 等待命令执行完成,返回 0 代表网络通
|
||||
int status = process.waitFor();
|
||||
isSuccess = (status == 0);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.append("Error: ").append(e.getMessage());
|
||||
isSuccess = false;
|
||||
}
|
||||
|
||||
// 结果回调到主线程
|
||||
final boolean finalSuccess = isSuccess;
|
||||
final float finalRtt = rtt;
|
||||
final String finalLog = log.toString();
|
||||
|
||||
mainHandler.post(() -> {
|
||||
if (callback != null) {
|
||||
callback.onComplete(finalSuccess, finalRtt, finalLog);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue