From 000869ee0c923dd2014833ce4e54a15164a21791 Mon Sep 17 00:00:00 2001 From: MTing Date: Fri, 5 Sep 2025 09:26:12 +0800 Subject: [PATCH] add displayMemoryInfo in face detect view --- .../com/ouxuan/oxface/OXFaceOnlineActivity.java | 66 ++++++++++++++++++++++ app/src/main/res/layout/activity_oxface_online.xml | 15 +++++ 2 files changed, 81 insertions(+) diff --git a/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java b/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java index 28b9965..b1c98fe 100644 --- a/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java @@ -1,5 +1,6 @@ package com.ouxuan.oxface; +import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -142,6 +143,7 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi // 底部按钮 private Button btnOpenDoor; + private Button btnMemoryInfo; // 新增的内存信息按钮 // private Button btnScanQR; // private Button btnSettings; @@ -281,12 +283,16 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi // 初始化底部按钮 btnOpenDoor = findViewById(R.id.button_open_door); + btnMemoryInfo = findViewById(R.id.button_memory_info); // 初始化内存信息按钮 // btnScanQR = findViewById(R.id.button_scan_qr); // btnSettings = findViewById(R.id.button_settings); if (btnOpenDoor != null) { btnOpenDoor.setOnClickListener(this); } + if (btnMemoryInfo != null) { // 设置内存信息按钮点击事件 + btnMemoryInfo.setOnClickListener(this); + } // if (btnScanQR != null) { // btnScanQR.setOnClickListener(this); // } @@ -621,6 +627,9 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi } else if (id == R.id.button_open_door) { // 开门按钮点击事件 handleOpenDoorClick(); + } else if (id == R.id.button_memory_info) { + // 内存信息按钮点击事件 + displayMemoryInfo(); } // else if (id == R.id.button_scan_qr) { // // 扫码按钮点击事件 @@ -1371,4 +1380,61 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi } }, 2000); } + + /** + * 查询并显示当前应用的内存信息 + */ + private void displayMemoryInfo() { + Runtime runtime = Runtime.getRuntime(); + long maxMemory = runtime.maxMemory() / (1024 * 1024); // MB + long totalMemory = runtime.totalMemory() / (1024 * 1024); // MB + long freeMemory = runtime.freeMemory() / (1024 * 1024); // MB + long usedMemory = totalMemory - freeMemory; // MB + + // 获取系统内存信息(需要API 16及以上) + ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); + activityManager.getMemoryInfo(memoryInfo); + + long totalMem = memoryInfo.totalMem / (1024 * 1024); // MB + long availMem = memoryInfo.availMem / (1024 * 1024); // MB + long threshold = memoryInfo.threshold / (1024 * 1024); // MB + boolean isLowMemory = memoryInfo.lowMemory; + + // 构建内存信息字符串 + StringBuilder memoryInfoStr = new StringBuilder(); + memoryInfoStr.append("应用内存信息:\n"); + memoryInfoStr.append("最大可用内存: ").append(maxMemory).append(" MB\n"); + memoryInfoStr.append("已分配内存: ").append(totalMemory).append(" MB\n"); + memoryInfoStr.append("已使用内存: ").append(usedMemory).append(" MB\n"); + memoryInfoStr.append("空闲内存: ").append(freeMemory).append(" MB\n\n"); + + memoryInfoStr.append("系统内存信息:\n"); + memoryInfoStr.append("总内存: ").append(totalMem).append(" MB\n"); + memoryInfoStr.append("可用内存: ").append(availMem).append(" MB\n"); + memoryInfoStr.append("内存阈值: ").append(threshold).append(" MB\n"); + memoryInfoStr.append("低内存状态: ").append(isLowMemory ? "是" : "否"); + + // 显示内存信息(可以通过Toast或者对话框显示) + Toast.makeText(this, memoryInfoStr.toString(), Toast.LENGTH_LONG).show(); + + // 同时输出到日志 + LogManager.logInfo(TAG, "内存信息:\n" + memoryInfoStr.toString()); + } + + /** + * 获取当前应用内存使用情况的简要信息 + * @return 内存使用情况字符串 + */ + private String getMemoryUsageSummary() { + Runtime runtime = Runtime.getRuntime(); + long maxMemory = runtime.maxMemory() / (1024 * 1024); // MB + long totalMemory = runtime.totalMemory() / (1024 * 1024); // MB + long freeMemory = runtime.freeMemory() / (1024 * 1024); // MB + long usedMemory = totalMemory - freeMemory; // MB + float memoryUsageRatio = (float) usedMemory / maxMemory; + + return String.format("内存: %d/%d MB (%.1f%%)", + usedMemory, maxMemory, memoryUsageRatio * 100); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_oxface_online.xml b/app/src/main/res/layout/activity_oxface_online.xml index 64fb56d..eede8fe 100644 --- a/app/src/main/res/layout/activity_oxface_online.xml +++ b/app/src/main/res/layout/activity_oxface_online.xml @@ -389,4 +389,19 @@ android:layout_marginStart="16dp" android:layout_marginBottom="16dp" /> + +