使用java:
bash
List<TableMessage> dataList=new ArrayList<TableMessage>();
//获取设备信息
Hashtable<String,String> ht= MyDeviceInfo.getDeviceAllInfo2(LoginActivity.this);
for (Map.Entry<String, String> entry : ht.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d("Hashtable", "Key: " + key + ", Value: " + value);
dataList.add(new TableMessage(key,value));
}
// String cpuABI = Build.CPU_ABI; // CPU架构(如arm64、x86)
//int cpuCount = Runtime.getRuntime().availableProcessors(); // 逻辑核心数
// String info = "架构:" + cpuABI + "+
// 核心数:" + cpuCount;
// dataList.add(new TableMessage("核心数:",cpuCount+""));
DialogTableDeviceInfo dialogTableDeviceInfo=new DialogTableDeviceInfo();
dialogTableDeviceInfo.show(LoginActivity.this,"设备信息",dataList);
显示xml:
bash
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/TextViewTitleSucces"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAlignment="center"
android:textColor="@color/btnsetting"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- 此处省略的组件的配置 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TextViewId"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:text="@string/txt_dialog_id"
android:textAlignment="center" />
<TextView
android:id="@+id/TextViewName"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:text="@string/txt_login_menu_about_name"
android:textAlignment="center" />
<TextView
android:id="@+id/TextViewQuantity"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:text="@string/txt_login_menu_about_size"
android:textAlignment="center" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="0dp"
android:scrollbars="horizontal|vertical" />
</LinearLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
<!-- <ListView-->
<!-- android:id="@+id/ListView_table"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:scrollbars="horizontal|vertical">-->
<!-- </ListView>-->
<!-- <ListView-->
<!-- android:id="@+id/ListView_table_fail"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_weight="1"-->
<!-- android:scrollbars="horizontal|vertical" />-->
</LinearLayout>
bash
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
tools:ignore="MissingDefaultResource">
<TextView
android:id="@+id/TextViewId"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:text="@string/txt_dialog_id"
android:textAlignment="center" />
<TextView
android:id="@+id/TextViewName"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:paddingRight="5dp"
android:text=""
android:textAlignment="textEnd" />
<TextView
android:id="@+id/TextViewQuantity"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:background="@drawable/border_background"
android:text=""
android:textAlignment="viewStart" />
</LinearLayout>
设备信息java代码:
bash
package com.demo.util;
import static android.content.Context.ACTIVITY_SERVICE;
import static android.util.Log.ERROR;
import static com.demo.util.FileUtilsPi.externalMemoryAvailable;
import android.app.ActivityManager;
import android.content.Context;
import android.icu.text.DecimalFormat;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Locale;
public class MyDeviceInfo {
//内存(RAM)信息获取
public void getMemoryInfo() {
String str1 = "/proc/meminfo";
String str2="";
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
Log.d("TAG", "---" + str2);
}
} catch (IOException e) {
}
}
/**
* 获取可用手机内存(RAM)
* @return
*/
public static long getAvailMemory(Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
/**
* 获取手机内部空间大小
* @return
*/
public static long getTotalInternalStorgeSize() {
File path = Environment.getDataDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long totalBlocks = mStatFs.getBlockCount();
return totalBlocks * blockSize;
}
/**
* 获取手机内部可用空间大小
* @return
*/
public static long getAvailableInternalStorgeSize() {
File path = Environment.getDataDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long availableBlocks = mStatFs.getAvailableBlocks();
return availableBlocks * blockSize;
}
/**
* 获取手机外部空间大小
* @return
*/
public static long getTotalExternalStorgeSize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long totalBlocks = mStatFs.getBlockCount();
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取手机外部可用空间大小
* @return
*/
public static long getAvailableExternalStorgeSize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long availableBlocks = mStatFs.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}
/* 返回为字符串数组[0]为大小[1]为单位KB或MB */
public static String formatSize(long size) {
String suffix = "";
float fSzie = 0;
if (size >= 1024) {
suffix = "KB";
fSzie = size / 1024;
if (fSzie >= 1024) {
suffix = "MB";
fSzie /= 1024;
if (fSzie >= 1024) {
suffix = "GB";
fSzie /= 1024;
}
}
}
DecimalFormat formatter = new DecimalFormat("#0.00");// 字符显示格式
/* 每3个数字用,分隔,如1,000 */
formatter.setGroupingSize(3);
StringBuilder resultBuffer = new StringBuilder(formatter.format(fSzie));
if (suffix != null) {
resultBuffer.append(suffix);
}
return resultBuffer.toString();
}
/* 返回为字符串数组[0]为大小[1]为单位KB或MB */
public static String[] formatSizeArr(long size) {
String[] arr = new String[2];
String suffix = "";
float fSzie = 0;
arr[0]="";
if (size >= 1024) {
suffix = "KB";
fSzie = size / 1024;
if (fSzie >= 1024) {
suffix = "MB";
fSzie /= 1024;
if (fSzie >= 1024) {
suffix = "GB";
fSzie /= 1024;
}
}
}
DecimalFormat formatter = new DecimalFormat("#0.00");// 字符显示格式
/* 每3个数字用,分隔,如1,000 */
formatter.setGroupingSize(3);
StringBuilder resultBuffer = new StringBuilder(formatter.format(fSzie));
if (suffix != null) {
resultBuffer.append(suffix);
}
return arr;
}
/**
* 获取设备宽度(px)
*
*/
public static int getDeviceWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取设备高度(px)
*/
public static int getDeviceHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取设备的唯一标识, 需要 "android.permission.READ_Phone_STATE"权限
*/
public static String getIMEI(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
if (deviceId == null) {
return "UnKnown";
} else {
return deviceId;
}
}
/**
* 获取厂商名
* **/
public static String getDeviceManufacturer() {
return android.os.Build.MANUFACTURER;
}
/**
* 获取产品名
* **/
public static String getDeviceProduct() {
return android.os.Build.PRODUCT;
}
/**
* 获取手机品牌
*/
public static String getDeviceBrand() {
return android.os.Build.BRAND;
}
/**
* 获取手机型号
*/
public static String getDeviceModel() {
return android.os.Build.MODEL;
}
/**
* 获取手机主板名
*/
public static String getDeviceBoard() {
return android.os.Build.BOARD;
}
/**
* 设备名
* **/
public static String getDeviceDevice() {
return android.os.Build.DEVICE;
}
/**
*
*
* fingerprit 信息
* **/
public static String getDeviceFubgerprint() {
return android.os.Build.FINGERPRINT;
}
/**
* 硬件名
*
* **/
public static String getDeviceHardware() {
return android.os.Build.HARDWARE;
}
/**
* 主机
*
* **/
public static String getDeviceHost() {
return android.os.Build.HOST;
}
/**
*
* 显示ID
* **/
public static String getDeviceDisplay() {
return android.os.Build.DISPLAY;
}
/**
* ID
*
* **/
public static String getDeviceId() {
return android.os.Build.ID;
}
/**
* 获取手机用户名
*
* **/
public static String getDeviceUser() {
return android.os.Build.USER;
}
/**
* 获取手机 硬件序列号
* **/
public static String getDeviceSerial() {
return android.os.Build.SERIAL;
}
/**
* 获取手机Android 系统SDK
*
* @return
*/
public static int getDeviceSDK() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* 获取手机Android 版本
*
* @return
*/
public static String getDeviceAndroidVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 获取当前手机系统语言。
*/
public static String getDeviceDefaultLanguage() {
return Locale.getDefault().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*/
public static String getDeviceSupportLanguage() {
Log.e("wangjie", "Local:" + Locale.GERMAN);
Log.e("wangjie", "Local:" + Locale.ENGLISH);
Log.e("wangjie", "Local:" + Locale.US);
Log.e("wangjie", "Local:" + Locale.CHINESE);
Log.e("wangjie", "Local:" + Locale.TAIWAN);
Log.e("wangjie", "Local:" + Locale.FRANCE);
Log.e("wangjie", "Local:" + Locale.FRENCH);
Log.e("wangjie", "Local:" + Locale.GERMANY);
Log.e("wangjie", "Local:" + Locale.ITALIAN);
Log.e("wangjie", "Local:" + Locale.JAPAN);
Log.e("wangjie", "Local:" + Locale.JAPANESE);
StringBuilder sb=new StringBuilder();
sb.append(Locale.CHINESE+"、"+Locale.US);
return sb.toString();
}
public static String getDeviceAllInfo(Context context) {
return "\n\n1. IMEI:\n\t\t" + getIMEI(context)
+ "\n\n2. 设备宽度:\n\t\t" + getDeviceWidth(context)
+ "\n\n3. 设备高度:\n\t\t" + getDeviceHeight(context)
+ "\n\n4. 是否有内置SD卡:\n\t\t" + SDCardUtils.isSDCardMount()
+ "\n\n5. RAM 信息:\n\t\t" + SDCardUtils.getRAMInfo(context)
+ "\n\n6. 内部存储信息\n\t\t" + SDCardUtils.getStorageInfo(context, 0)
+ "\n\n7. SD卡 信息:\n\t\t" + SDCardUtils.getStorageInfo(context, 1)
// + "\n\n8. 是否联网:\n\t\t" + Utils.isNetworkConnected(context)
//
// + "\n\n9. 网络类型:\n\t\t" + Utils.GetNetworkType(context)
+ "\n\n10. 系统默认语言:\n\t\t" + getDeviceDefaultLanguage()
+ "\n\n11. 硬件序列号(设备名):\n\t\t" + android.os.Build.SERIAL
+ "\n\n12. 手机型号:\n\t\t" + android.os.Build.MODEL
+ "\n\n13. 生产厂商:\n\t\t" + android.os.Build.MANUFACTURER
+ "\n\n14. 手机Fingerprint标识:\n\t\t" + android.os.Build.FINGERPRINT
+ "\n\n15. Android 版本:\n\t\t" + android.os.Build.VERSION.RELEASE
+ "\n\n16. Android SDK版本:\n\t\t" + android.os.Build.VERSION.SDK_INT
+ "\n\n17. 安全patch 时间:\n\t\t" + android.os.Build.VERSION.SECURITY_PATCH
// + "\n\n18. 发布时间:\n\t\t" + Utils.Utc2Local(android.os.Build.TIME)
+ "\n\n19. 版本类型:\n\t\t" + android.os.Build.TYPE
+ "\n\n20. 用户名:\n\t\t" + android.os.Build.USER
+ "\n\n21. 产品名:\n\t\t" + android.os.Build.PRODUCT
+ "\n\n22. ID:\n\t\t" + android.os.Build.ID
+ "\n\n23. 显示ID:\n\t\t" + android.os.Build.DISPLAY
+ "\n\n24. 硬件名:\n\t\t" + android.os.Build.HARDWARE
+ "\n\n25. 产品名:\n\t\t" + android.os.Build.DEVICE
+ "\n\n26. Bootloader:\n\t\t" + android.os.Build.BOOTLOADER
+ "\n\n27. 主板名:\n\t\t" + android.os.Build.BOARD
+ "\n\n28. CodeName:\n\t\t" + android.os.Build.VERSION.CODENAME
+ "\n\n29. 语言支持:\n\t\t" + getDeviceSupportLanguage();
}
public static String GetDeivceAllInfo2(){
String DeviceMessage=
"产品 :" + android.os.Build.PRODUCT
+"\nCPU_ABI :" + android.os.Build.CPU_ABI
+ "\n 标签 :" + android.os.Build.TAGS
+ "\n VERSION_CODES.BASE:" + android.os.Build.VERSION_CODES.BASE
+ "\n 型号:" + android.os.Build.MODEL
+"\n SDK : " + android.os.Build.VERSION.SDK
+"\n Android 版本 : " + android.os.Build.VERSION.RELEASE
+ "\n 驱动 : " + android.os.Build.DEVICE+
"\n DISPLAY: " + android.os.Build.DISPLAY+
"\n 品牌 : " + android.os.Build.BRAND
+"\n 基板 : " + android.os.Build.BOARD
+"\n 设备标识 : " + android.os.Build.FINGERPRINT
+"\n 版本号 : " + android.os.Build.ID
+ "\n 制造商 : " + android.os.Build.MANUFACTURER
+"\n 用户 : " + android.os.Build.USER
+"\n CPU_ABI2 : "+android.os.Build.CPU_ABI2
+"\n 硬件 : "+ android.os.Build.HARDWARE
+"\n 主机地址 :"+android.os.Build.HOST
+"\n 未知信息 : "+android.os.Build.UNKNOWN
+"\n 版本类型 : "+android.os.Build.TYPE
+"\n 时间 : "+String.valueOf(android.os.Build.TIME)
+"\n Radio : "+android.os.Build.RADIO
+"\n 序列号 : "+android.os.Build.SERIAL;
return DeviceMessage;
}
public static Hashtable<String,String> getDeviceAllInfo2(Context context) {
Hashtable<String,String> ht=new Hashtable<String,String>();
// ht.put("IMEI:",getIMEI(context));
ht.put("设备宽度:",getDeviceWidth(context)+"");
ht.put("设备高度:",getDeviceHeight(context)+"");
ht.put("RAM 信息:",SDCardUtils.getRAMInfo(context)+"");
ht.put("内部存储信息:",SDCardUtils.getStorageInfo(context, 0)+"");
ht.put("是否有内置SD卡:",SDCardUtils.isSDCardMount()+"");
ht.put("SD卡信息:",SDCardUtils.getStorageInfo(context, 1)+"");
ht.put("系统默认语言:",getDeviceDefaultLanguage()+"");
ht.put("硬件序列号(设备名):", android.os.Build.SERIAL+"");
ht.put("设备型号:",android.os.Build.MODEL+"");
ht.put("生产厂商:",android.os.Build.MANUFACTURER+"");
// ht.put("手机Fingerprint标识:",android.os.Build.FINGERPRINT+"");
ht.put("Android 版本:",android.os.Build.VERSION.RELEASE+"");
ht.put("Android SDK版本:",android.os.Build.VERSION.SDK_INT+"");
ht.put("安全patch 时间:",android.os.Build.VERSION.SECURITY_PATCH+"");
ht.put("版本类型:",android.os.Build.TYPE+"");
ht.put("用户名:",android.os.Build.USER+"");
ht.put("ID:",android.os.Build.ID+"");
ht.put("显示ID:",android.os.Build.DISPLAY+"");
ht.put("硬件名:",android.os.Build.HARDWARE+"");
ht.put("产品名:",android.os.Build.DEVICE+"");
ht.put("Bootloader:",android.os.Build.BOOTLOADER+"");
ht.put("主板名:",android.os.Build.BOARD+"");
ht.put("主机:", Build.HOST+"");
ht.put("CodeName:",android.os.Build.VERSION.CODENAME+"");
ht.put("语言支持:",getDeviceSupportLanguage()+"");
//支持卡槽数量(sdk_version>=23才可取值,否则为0slot_count
// ht.put(" 支持卡槽数量:",android.os.Build.+"");
ht.put("CPU名字:", Build.CPU_ABI+"");
ht.put("CPU_ABI2:", android.os.Build.CPU_ABI2+"");
ht.put("CPU核心数:", Runtime.getRuntime().availableProcessors()+"");
// ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// if (activityManager == null) {
// throw new IllegalStateException("ActivityManager is not available");
// }
//
// // 创建MemoryInfo对象并填充数据
// ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
// activityManager.getMemoryInfo(memoryInfo);
//
// // 提取总运行内存和可用运行内存
// long totalMemory = memoryInfo.totalMem; // 总运行内存(含已用)
// long availableMemory = memoryInfo.availMem; // 可用运行内存(不含已用)
// 转换为 MB(保留两位小数)
// String totalMemoryMB = String.format("%.2f GB", totalMemory / (1024.0 * 1024*1024));
// String availableMemoryMB = String.format("%.2f GB", availableMemory / (1024.0 * 1024*1024));
// ht.put("运行内存:", "总运行内存/可用运行内存"+totalMemoryMB+"/"+availableMemoryMB+"");
// ht.put(" 蓝牙mac地址:", android.os.Build+"");
// String message= "\n\n1. IMEI:\n\t\t" + getIMEI(context)
//
// + "\n\n2. 设备宽度:\n\t\t" + getDeviceWidth(context)
//
// + "\n\n3. 设备高度:\n\t\t" + getDeviceHeight(context)
//
// + "\n\n4. 是否有内置SD卡:\n\t\t" + SDCardUtils.isSDCardMount()
//
// + "\n\n5. RAM 信息:\n\t\t" + SDCardUtils.getRAMInfo(context)
//
// + "\n\n6. 内部存储信息\n\t\t" + SDCardUtils.getStorageInfo(context, 0)
//
// + "\n\n7. SD卡 信息:\n\t\t" + SDCardUtils.getStorageInfo(context, 1)
//
+ "\n\n8. 是否联网:\n\t\t" + Utils.isNetworkConnected(context)
+ "\n\n9. 网络类型:\n\t\t" + Utils.GetNetworkType(context)
//
// + "\n\n10. 系统默认语言:\n\t\t" + getDeviceDefaultLanguage()
//
// + "\n\n11. 硬件序列号(设备名):\n\t\t" + android.os.Build.SERIAL
//
// + "\n\n12. 手机型号:\n\t\t" + android.os.Build.MODEL
//
// + "\n\n13. 生产厂商:\n\t\t" + android.os.Build.MANUFACTURER
//
// + "\n\n14. 手机Fingerprint标识:\n\t\t" + android.os.Build.FINGERPRINT
//
// + "\n\n15. Android 版本:\n\t\t" + android.os.Build.VERSION.RELEASE
//
// + "\n\n16. Android SDK版本:\n\t\t" + android.os.Build.VERSION.SDK_INT
//
// + "\n\n17. 安全patch 时间:\n\t\t" + android.os.Build.VERSION.SECURITY_PATCH
//
+ "\n\n18. 发布时间:\n\t\t" + Utils.Utc2Local(android.os.Build.TIME)
//
// + "\n\n19. 版本类型:\n\t\t" + android.os.Build.TYPE
//
// + "\n\n20. 用户名:\n\t\t" + android.os.Build.USER
//
// + "\n\n21. 产品名:\n\t\t" + android.os.Build.PRODUCT
//
// + "\n\n22. ID:\n\t\t" + android.os.Build.ID
//
// + "\n\n23. 显示ID:\n\t\t" + android.os.Build.DISPLAY
//
// + "\n\n24. 硬件名:\n\t\t" + android.os.Build.HARDWARE
//
// + "\n\n25. 产品名:\n\t\t" + android.os.Build.DEVICE
//
// + "\n\n26. Bootloader:\n\t\t" + android.os.Build.BOOTLOADER
//
// + "\n\n27. 主板名:\n\t\t" + android.os.Build.BOARD
//
// + "\n\n28. CodeName:\n\t\t" + android.os.Build.VERSION.CODENAME
// + "\n\n29. 语言支持:\n\t\t" + getDeviceSupportLanguage();
getCPUInfo();
return ht;
}
public static String getCPUInfo() {
StringBuilder cpuInfo = new StringBuilder();
try {
// 执行cat命令读取文件
Process process = Runtime.getRuntime().exec("cat /proc/cpuinfo");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
cpuInfo.append(line).append("");
Log.d("MemoryInfo", "Available RAM: " + line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return "获取失败";
}
return cpuInfo.toString();
}
public static String getRAM(Context context){
// // 获取内存信息
// ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
// activityManager.getMemoryInfo(memoryInfo);
//
提取数据并转换单位
// long totalMemory = memoryInfo.totalMem;
// long availableMemory = memoryInfo.availMem;
//
转换为 MB(保留两位小数)
// String totalMemoryMB = String.format("%.2f MB", totalMemory / (1024.0 * 1024));
// String availableMemoryMB = String.format("%.2f MB", availableMemory / (1024.0 * 1024));
//
// Log.d("MemoryInfo", "Total RAM: " + totalMemoryMB);
// Log.d("MemoryInfo", "Available RAM: " + availableMemoryMB);
return "";
}
/**
* 获取CPU总运行内存和可用运行内存(单位:字节)
*
* @param context 应用上下文
* @return 包含总运行内存和可用运行内存的数组,索引0为总内存,索引1为可用内存
*/
public static String[] getCpuAndAvailableMemory(Context context) {
// 获取ActivityManager实例
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
throw new IllegalStateException("ActivityManager is not available");
}
// 创建MemoryInfo对象并填充数据
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// 提取总运行内存和可用运行内存
long totalMemory = memoryInfo.totalMem; // 总运行内存(含已用)
long availableMemory = memoryInfo.availMem; // 可用运行内存(不含已用)
转换为 MB(保留两位小数)
String totalMemoryMB = String.format("%.2f MB", totalMemory / (1024.0 * 1024));
String availableMemoryMB = String.format("%.2f MB", availableMemory / (1024.0 * 1024));
return new String[]{totalMemoryMB, availableMemoryMB};
}
}
bash
package com.uhf200.demo.util;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.uhf200.demo.R;
import com.uhf200.demo.ui.model.TableMessage;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
public class DialogTableDeviceInfo {
public static AlertDialog alertDialog3; //输入弹出框
public static ListView dialog_ListView;
public static DialogAdapter dialogAdapter;
private static List<TableMessage> mArrCard;
//返回失败的信息
public static ListView dialog_ListView_fail;
public static DialogFailAdapter dialogFailAdapter;
private static List<TableMessage> mArrCard_fail;
public void show(Context context, String title_succes,
List<TableMessage> dataList) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle("设备信息");
mArrCard=new ArrayList<TableMessage>();
mArrCard_fail=new ArrayList<TableMessage>();
mArrCard=dataList;
// 引入自己设计的xml//成功的提示信息
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_table_device_info,null);
TextView TextViewSucces= dialogView.findViewById(R.id.TextViewTitleSucces);
LinearLayout LinearLayout1= dialogView.findViewById(R.id.LinearLayout1);
LinearLayout LinearLayout2= dialogView.findViewById(R.id.LinearLayout2);
// dialog_ListView = (ListView) dialogView.findViewById(R.id.ListView_table);
//
// dialogAdapter = new DialogAdapter(context, mArrCard);
//
// dialog_ListView.setAdapter(dialogAdapter);
//
// dialog_ListView.setVerticalScrollBarEnabled(true);
//失败的信息
// dialog_ListView_fail = (ListView) dialogView.findViewById(R.id.ListView_table_fail);
//
// dialogFailAdapter = new DialogFailAdapter(context, mArrCard_fail);
//
// dialog_ListView_fail.setAdapter(dialogFailAdapter);
//
// dialog_ListView_fail.setVerticalScrollBarEnabled(true);
// TableLayout tableLayout = dialogView.findViewById(R.id.table_layout);
TextViewSucces.setText(title_succes);
// dialogAdapter.notifyDataSetChanged();
// dialogFailAdapter.notifyDataSetChanged();
alertBuilder.setView(dialogView);
alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog3.dismiss();
}
});
//成功的信息
RecyclerView recyclerView = dialogView.findViewById(R.id.recyclerView);
// 设置垂直列表布局
recyclerView.setLayoutManager(new LinearLayoutManager(context));
// 准备测试数据
List<TableMessage> userList = new ArrayList<>();
// for (int i=0;i<100;i++){
// userList.add(new TableMessage("张三"+i, "2"+i));
// }
//
// userList.add(new TableMessage("李四", "30"));
// userList.add(new TableMessage("王五", "28"));
// 添加更多数据...
if(dataList.size()>0){
userList=dataList;
}else {
TextViewSucces.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
LinearLayout1.setVisibility(View.GONE);
LinearLayout2.setVisibility(View.GONE);
}
// 创建并设置 Adapter
UserAdapter adapter = new UserAdapter(userList);
recyclerView.setAdapter(adapter);
//添加数据
// for (int i = 0; i < dataList.size(); i++) {
//
// TableMessage tableMessage=dataList.get(i);
// addDataRow(context,tableLayout, i + 1,
// tableMessage.getName(),
// tableMessage.getQuantity());
// }
// alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
//
// alertDialog3.dismiss();
//
// }
// });
alertDialog3 = alertBuilder.create();
alertDialog3.show();
}
public void show(Context context, String title_succes,String title_fail,
List<TableMessage> dataList, List<TableMessage> dataList_fail,String errorMessage) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle("设备信息");
mArrCard=new ArrayList<TableMessage>();
mArrCard_fail=new ArrayList<TableMessage>();
mArrCard=dataList;
mArrCard_fail=dataList_fail;
// 引入自己设计的xml//成功的提示信息
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_table_device_info,null);
TextView TextViewSucces= dialogView.findViewById(R.id.TextViewTitleSucces);
LinearLayout LinearLayout1= dialogView.findViewById(R.id.LinearLayout1);
LinearLayout LinearLayout2= dialogView.findViewById(R.id.LinearLayout2);
// dialog_ListView = (ListView) dialogView.findViewById(R.id.ListView_table);
//
// dialogAdapter = new DialogAdapter(context, mArrCard);
//
// dialog_ListView.setAdapter(dialogAdapter);
//
// dialog_ListView.setVerticalScrollBarEnabled(true);
//失败的信息
// dialog_ListView_fail = (ListView) dialogView.findViewById(R.id.ListView_table_fail);
//
// dialogFailAdapter = new DialogFailAdapter(context, mArrCard_fail);
//
// dialog_ListView_fail.setAdapter(dialogFailAdapter);
//
// dialog_ListView_fail.setVerticalScrollBarEnabled(true);
// TableLayout tableLayout = dialogView.findViewById(R.id.table_layout);
TextViewSucces.setText(title_succes);
// dialogAdapter.notifyDataSetChanged();
// dialogFailAdapter.notifyDataSetChanged();
alertBuilder.setView(dialogView);
alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog3.dismiss();
}
});
//成功的信息
RecyclerView recyclerView = dialogView.findViewById(R.id.recyclerView);
// 设置垂直列表布局
recyclerView.setLayoutManager(new LinearLayoutManager(context));
// 准备测试数据
List<TableMessage> userList = new ArrayList<>();
// for (int i=0;i<100;i++){
// userList.add(new TableMessage("张三"+i, "2"+i));
// }
//
// userList.add(new TableMessage("李四", "30"));
// userList.add(new TableMessage("王五", "28"));
// 添加更多数据...
if(dataList.size()>0){
userList=dataList;
}else {
TextViewSucces.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
LinearLayout1.setVisibility(View.GONE);
LinearLayout2.setVisibility(View.GONE);
}
// 创建并设置 Adapter
UserAdapter adapter = new UserAdapter(userList);
recyclerView.setAdapter(adapter);
//添加数据
// for (int i = 0; i < dataList.size(); i++) {
//
// TableMessage tableMessage=dataList.get(i);
// addDataRow(context,tableLayout, i + 1,
// tableMessage.getName(),
// tableMessage.getQuantity());
// }
// alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
//
// alertDialog3.dismiss();
//
// }
// });
alertDialog3 = alertBuilder.create();
alertDialog3.show();
}
private static void addDataRow(Context context,TableLayout tableLayout,
int index,
String name,
String quantity) {
TableRow dataRow = new TableRow(tableLayout.getContext());
dataRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// 序号列
TextView tvIndex = createTextView(context,String.valueOf(index),
Color.BLACK,
Gravity.LEFT);
dataRow.addView(tvIndex);
// 名称列
TextView tvName = createTextView(context,name,
Color.BLACK,
Gravity.START);
dataRow.addView(tvName);
// 数量列
TextView tvQuantity = createTextView(context,quantity,
Color.BLACK,
Gravity.CENTER);
dataRow.addView(tvQuantity);
// 添加底部边框
// View divider = new View(tableLayout.getContext());
// divider.setLayoutParams(new TableRow.LayoutParams(
// TableRow.LayoutParams.MATCH_PARENT,
// 2));
// divider.setBackgroundColor(Color.LTGRAY);
// dataRow.addView(divider);
tableLayout.addView(dataRow);
}
private static TextView createTextView(Context context,String text,
int textColor,
int gravity) {
TextView tv = new TextView(context);
tv.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.WRAP_CONTENT,
1f));
tv.setText(text);
tv.setTextColor(textColor);
tv.setGravity(gravity);
tv.setPadding(16, 12, 16, 12);
return tv;
}
public class DialogAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
public List<TableMessage> arraylist;
public int i=1;
//刷新数据
public int optionindex;
public DialogAdapter(Context context) {
super();
this.context = context;
inflater = LayoutInflater.from(context);
arraylist = new ArrayList<TableMessage>();
}
public DialogAdapter( Context context, List<TableMessage> arraylist) {
super();
inflater = LayoutInflater.from(context);
this.context = context;
this.arraylist = arraylist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewDialog viewDialog=null;
try {
if (convertView == null) {
viewDialog=new ViewDialog();
convertView=inflater.inflate(R.layout.device_item_info, null);
viewDialog.tvId = (TextView)convertView.findViewById(R.id.TextViewId);
viewDialog.tv_Name= (TextView) convertView.findViewById(R.id.TextViewName);
viewDialog.tv_Quantity= (TextView) convertView.findViewById(R.id.TextViewQuantity);
}else {
viewDialog = (ViewDialog)convertView.getTag();
// resetViewHolder(viewDialog);
}
TableMessage tableMessage = arraylist.get(position);
viewDialog.tvId.setText((position+1)+"");
viewDialog.tv_Name.setText(tableMessage.getName());
viewDialog.tv_Quantity.setText(tableMessage.getQuantity());
return convertView;
} catch (Exception e) {
Log.d("debugAdd", "3测试添加数据有误" + e.toString());
return convertView;
// throw new RuntimeException(e);
}
/// return view;
}
protected void resetViewHolder(ViewDialog p_ViewHolder)
{
p_ViewHolder.tvId.setText(null);
p_ViewHolder.tv_Name.setText(null);
p_ViewHolder.tv_Quantity.setText(null);
}
private class ViewDialog{
private TextView tvId;
private TextView tv_Name;
private TextView tv_Quantity;
}
}
public class DialogFailAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
public List<TableMessage> arraylist;
public Hashtable<String,String> ht_AssetNames;
public int i=1;
//刷新数据
public int optionindex;
public DialogFailAdapter(Context context) {
super();
this.context = context;
inflater = LayoutInflater.from(context);
arraylist = new ArrayList<TableMessage>();
ht_AssetNames=new Hashtable<String,String>();
}
public DialogFailAdapter( Context context, List<TableMessage> arraylist) {
super();
inflater = LayoutInflater.from(context);
this.context = context;
this.arraylist = arraylist;
ht_AssetNames=new Hashtable<String,String>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewDialog3 viewDialog3=null;
try {
if (view == null) {
viewDialog3=new ViewDialog3();
view=inflater.inflate(R.layout.device_item_info, null);
viewDialog3.tvId = (TextView)view.findViewById(R.id.TextViewId3);
viewDialog3.tv_Name= (TextView) view.findViewById(R.id.TextViewName3);
viewDialog3.tv_Quantity= (TextView) view.findViewById(R.id.TextViewQuantity3);
}else {
viewDialog3 = (ViewDialog3)view.getTag();
// resetViewHolder3(viewDialog3);
}
TableMessage tableMessage = arraylist.get(position);
viewDialog3.tvId.setText((position+1)+"");
viewDialog3.tv_Name.setText(tableMessage.getName());
viewDialog3.tv_Quantity.setText(tableMessage.getQuantity());
return view;
} catch (Exception e) {
Log.d("debugAdd", "3测试添加数据有误" + e.toString());
return view;
// throw new RuntimeException(e);
}
/// return view;
}
protected void resetViewHolder3(ViewDialog3 p_ViewHolder)
{
p_ViewHolder.tvId.setText(null);
p_ViewHolder.tv_Name.setText(null);
p_ViewHolder.tv_Quantity.setText(null);
}
private class ViewDialog3{
private TextView tvId;
private TextView tv_Name;
private TextView tv_Quantity;
}
}
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private List<TableMessage> userList;
public UserAdapter(List<TableMessage> userList) {
this.userList = userList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.device_item_info, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// 获取当前数据
TableMessage tableMessage = userList.get(position);
// 设置序号(position 从 0 开始,需 +1)
holder.tvId.setText(String.valueOf(position + 1));
// 设置姓名和年龄
holder.tv_Name.setText(tableMessage.getName());
holder.tv_Quantity.setText(tableMessage.getQuantity());
}
@Override
public int getItemCount() {
return userList.size();
}
// ViewHolder 内部类
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvId, tv_Name, tv_Quantity;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvId = itemView.findViewById(R.id.TextViewId);
tv_Name = itemView.findViewById(R.id.TextViewName);
tv_Quantity = itemView.findViewById(R.id.TextViewQuantity);
}
}
}
public class UserFailAdapter extends RecyclerView.Adapter<UserFailAdapter.ViewHolder> {
private List<TableMessage> userList;
public UserFailAdapter(List<TableMessage> userList) {
this.userList = userList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.device_item_info, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// 获取当前数据
TableMessage tableMessage = userList.get(position);
// 设置序号(position 从 0 开始,需 +1)
holder.tvId.setText(String.valueOf(position + 1));
// 设置姓名和年龄
holder.tv_Name.setText(tableMessage.getName());
holder.tv_Quantity.setText(tableMessage.getQuantity());
}
@Override
public int getItemCount() {
return userList.size();
}
// ViewHolder 内部类
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvId, tv_Name, tv_Quantity;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvId = itemView.findViewById(R.id.TextViewId3);
tv_Name = itemView.findViewById(R.id.TextViewName3);
tv_Quantity = itemView.findViewById(R.id.TextViewQuantity3);
}
}
}
}