基本ok
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
@@ -46,7 +48,11 @@
|
||||
<receiver
|
||||
android:name=".receiver.CommandBroadcastReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
android:exported="false" >
|
||||
<intent-filter>
|
||||
<action android:name="cn.ykbox.dashboard.ACTION_SEND_COMMAND" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -5,6 +5,7 @@ import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
@@ -12,6 +13,11 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.webkit.WebResourceError;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebResourceResponse;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -36,8 +42,13 @@ public class BuildingDashboardActivity extends FullscreenActivity {
|
||||
private static final long CONFIG_LOAD_INTERVAL = 10 * 60 * 1000; // 10 minutes
|
||||
private static final int MAX_COMMAND_ALARMS = 20; // 支持的最大闹钟指令数量
|
||||
|
||||
private Context mContext;
|
||||
private String mainUrl;
|
||||
private int retryCount = 0;
|
||||
private static final int MAX_RETRY = 99; // 最大重试次数
|
||||
private static final int RETRY_DELAY = 60000; // 1分钟 = 60000毫秒
|
||||
private String configUrl;
|
||||
|
||||
private ConfigReader configReader;
|
||||
private String lastAppliedSerialConfig = null;
|
||||
|
||||
@@ -88,9 +99,34 @@ public class BuildingDashboardActivity extends FullscreenActivity {
|
||||
configUrl = url + "/data/config.json";
|
||||
|
||||
configLoadHandler.post(configLoadRunnable);
|
||||
loadUrlWithRetry();
|
||||
}
|
||||
|
||||
private void loadUrlWithRetry() {
|
||||
binding.webview.loadUrl(mainUrl);
|
||||
}
|
||||
|
||||
private void handleLoadError() {
|
||||
if (retryCount < MAX_RETRY) {
|
||||
retryCount++;
|
||||
Log.d("WebView", "加载失败,将在1分钟后重试 (第" + retryCount + "次重试)");
|
||||
|
||||
// 使用Handler延迟执行重试
|
||||
configLoadHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d("WebView", "开始重试加载...");
|
||||
loadUrlWithRetry();
|
||||
}
|
||||
}, RETRY_DELAY);
|
||||
|
||||
} else {
|
||||
Log.e("WebView", "重试次数已达上限,加载失败");
|
||||
// 这里可以显示错误页面或提示用户
|
||||
showToast("网页加载失败,请检查网络连接");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
@@ -117,8 +153,45 @@ public class BuildingDashboardActivity extends FullscreenActivity {
|
||||
}
|
||||
|
||||
private void initWebView() {
|
||||
binding.webview.getSettings().setJavaScriptEnabled(true);
|
||||
binding.webview.setWebViewClient(new WebViewClient());
|
||||
WebSettings webSettings = binding.webview.getSettings();
|
||||
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
|
||||
binding.webview.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public void onReceivedError(WebView view, WebResourceRequest request,
|
||||
WebResourceError error) {
|
||||
super.onReceivedError(view, request, error);
|
||||
|
||||
// 只处理主页面的错误,不处理资源文件错误
|
||||
if (request.getUrl().toString().equals(mainUrl)) {
|
||||
handleLoadError();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedHttpError(WebView view, WebResourceRequest request,
|
||||
WebResourceResponse errorResponse) {
|
||||
super.onReceivedHttpError(view, request, errorResponse);
|
||||
|
||||
// 处理HTTP错误(如404, 500等)
|
||||
if (request.getUrl().toString().equals(mainUrl)) {
|
||||
handleLoadError();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
|
||||
// 页面加载成功,重置重试计数
|
||||
if (url.equals(mainUrl)) {
|
||||
retryCount = 0;
|
||||
Log.d("WebView", "页面加载成功: " + url);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void initConfigLoader() {
|
||||
@@ -209,8 +282,19 @@ public class BuildingDashboardActivity extends FullscreenActivity {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
||||
}
|
||||
|
||||
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
|
||||
Log.d(TAG, "Set repeating alarm for " + time + " with command " + hex + " on port " + portPath + " at " + baudRate + " baud");
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
|
||||
Log.d(TAG, "setExactAndAllowWhileIdle, Set repeating alarm for " + time + " with command " + hex + " on port " + portPath + " at " + baudRate + " baud");
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
|
||||
Log.d(TAG, "setExact, Set repeating alarm for " + time + " with command " + hex + " on port " + portPath + " at " + baudRate + " baud");
|
||||
} else {
|
||||
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
|
||||
Log.d(TAG, "Set repeating alarm for " + time + " with command " + hex + " on port " + portPath + " at " + baudRate + " baud");
|
||||
}
|
||||
|
||||
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +312,8 @@ public class BuildingDashboardActivity extends FullscreenActivity {
|
||||
}
|
||||
|
||||
private void showToast(String message) {
|
||||
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||
runOnUiThread(() -> Toast.makeText(mContext,
|
||||
message,
|
||||
Toast.LENGTH_SHORT).show());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ public class CommandBroadcastReceiver extends BroadcastReceiver {
|
||||
if (intent != null && ACTION_SEND_COMMAND.equals(intent.getAction())) {
|
||||
String hexCommand = intent.getStringExtra(EXTRA_COMMAND_HEX);
|
||||
String portPath = intent.getStringExtra(EXTRA_PORT_PATH);
|
||||
// 新增:从 Intent 中获取波特率,如果不存在则默认为 9600
|
||||
int baudRate = intent.getIntExtra(EXTRA_BAUD_RATE, 9600);
|
||||
|
||||
if (hexCommand != null && !hexCommand.isEmpty() && portPath != null && !portPath.isEmpty()) {
|
||||
|
||||
@@ -38,12 +38,6 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="设置" />
|
||||
<Button
|
||||
android:id="@+id/power_off_tv_button"
|
||||
style="?android:attr/buttonBarButtonStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="关电视" />
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user