Browse Source

add displayMemoryInfo in face detect view

main
MTing 1 week ago
parent
commit
000869ee0c
  1. 66
      app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java
  2. 15
      app/src/main/res/layout/activity_oxface_online.xml

66
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);
}
}

15
app/src/main/res/layout/activity_oxface_online.xml

@ -389,4 +389,19 @@
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp" />
<!-- 内存信息查询按钮,放在屏幕右下角 -->
<Button
android:id="@+id/button_memory_info"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="内存信息"
android:textSize="14sp"
android:textColor="#FFFFFF"
android:background="@drawable/primary_color_rounded_background"
android:minHeight="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp" />
</RelativeLayout>
Loading…
Cancel
Save