一、设计布局文件
二、打开MainAbilitySlice.java文件并录入以下关键代码
打开MainAbilitySlice.java
import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.ability.DeviceConfigInfo;import ohos.aafwk.ability.RunningProcessInfo;import ohos.aafwk.ability.SystemMemoryInfo;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.Text;import ohos.agp.utils.LayoutAlignment;import ohos.app.IAbilityManager;import ohos.app.dispatcher.TaskDispatcher;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import java.util.List;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00888, "BingJunNetWorkLog"); Text ShowMemoryInfo=null; SystemMemoryInfo systemMemoryInfo =null; IAbilityManager iAbilityManager =null; List runningProcessInfos=null; DeviceConfigInfo deviceConfigInfo=null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button CheckMemory=(Button) findComponentById(ResourceTable.Id_CheckMemory); ShowMemoryInfo=(Text) findComponentById(ResourceTable.Id_ShowMemoryInfo); CheckMemory.setClickedListener(clickedListener); Button ClearMemory=(Button) findComponentById(ResourceTable.Id_ClearMemory); ClearMemory.setClickedListener(clickedListener); } public Component.ClickedListener clickedListener= component -> { switch (component.getId()){ case ResourceTable.Id_CheckMemory: ShowMemoryInfo.setText(""); this.GetSystemMemoryInfo(); break; case ResourceTable.Id_ClearMemory: ShowMemoryInfo.setText(""); this.ClearMemoryAndKillProcesses(); this.LoadSampleToast("内存清理完毕",0,0,2000, LayoutAlignment.CENTER); this.GetSystemMemoryInfo(); break; default: break; } }; public void GetSystemMemoryInfo(){ TaskDispatcher taskDispatcher=this.getUITaskDispatcher(); taskDispatcher.asyncDispatch(() -> { try { systemMemoryInfo = new SystemMemoryInfo(); iAbilityManager = getAbilityManager(); iAbilityManager.getSystemMemoryInfo(systemMemoryInfo); ShowMemoryInfo.append("总系统内存大小:" systemMemoryInfo.getTotalSysMem() "rn"); ShowMemoryInfo.append("可用系统内存大小:" systemMemoryInfo.getAvailSysMem() "rn"); ShowMemoryInfo.append("可用系统内存的阈值:" systemMemoryInfo.getThreshold() "rn"); if (systemMemoryInfo.isLowSysMemory()==true){ ShowMemoryInfo.append("内存不足rn"); }else { ShowMemoryInfo.append("内存充足rn"); } if (iAbilityManager.isLowRamDevice()==true){ ShowMemoryInfo.append("低RAM设备rn"); }else { ShowMemoryInfo.append("高RAM设备rn"); } ShowMemoryInfo.append("基本内存大小:" IAbilityManager.DEFAULT_HEAPSIZE "rn"); ShowMemoryInfo.append("默认密度:" IAbilityManager.DEFAULT_DENSITY "rn"); ShowMemoryInfo.append("主屏幕上图标的密度:" iAbilityManager.getHomeScreenIconDensity() "rn"); ShowMemoryInfo.append("默认图标大小:" IAbilityManager.DEFAULT_ICONSIZE "rn"); ShowMemoryInfo.append("主屏幕上图标的大小:" iAbilityManager.getHomeScreenIconSize() "rn"); ShowMemoryInfo.append("应用程序可以使用的最大内存:" iAbilityManager.getAppLargeMemory() "rn"); ShowMemoryInfo.append("应用程序可以使用的内存:" iAbilityManager.getAppMemory() "rn"); deviceConfigInfo=iAbilityManager.getDeviceConfigInfo(); ShowMemoryInfo.append("所需的OpenGL ES版本:" deviceConfigInfo.getDeviceGLESVersion() "rn"); if (deviceConfigInfo.getExternalInputDevices()==0){ ShowMemoryInfo.append("不需要外部输入设备:rn"); }else if (deviceConfigInfo.getExternalInputDevices()==1){ ShowMemoryInfo.append("需要外部物理键盘rn"); } else { ShowMemoryInfo.append("需要外部五向(上,下,右,左和选择)导航设备rn"); } if (deviceConfigInfo.getKeyBoardType()==0){ ShowMemoryInfo.append("不需要键盘rn"); }else if (deviceConfigInfo.getKeyBoardType()==1){ ShowMemoryInfo.append("需要具有QWERTY布局的键盘rn"); }else { ShowMemoryInfo.append("需要具有12键布局的键盘rn"); } if (deviceConfigInfo.getNavigationType()==0){ ShowMemoryInfo.append("不需要导航设备rn"); }else if (deviceConfigInfo.getNavigationType()==1){ ShowMemoryInfo.append("所需的导航设备:方向键rn"); }else if (deviceConfigInfo.getNavigationType()==2){ ShowMemoryInfo.append("所需的导航设备:跟踪球rn"); }else { ShowMemoryInfo.append("所需的导航设备:滚轮rn"); } if (deviceConfigInfo.getTouchScreenType()==0){ ShowMemoryInfo.append("不需要触摸屏rn"); }else { ShowMemoryInfo.append("需要可以用指尖操作的触摸屏rn"); } /*runningProcessInfos=iAbilityManager.getAllRunningProcesses(); for (int i=0; i { try { //iAbilityManager.clearUpApplicationData(); //清除应用程序的用户数据,这相当于初始化应用程序。 //此方法删除与该应用程序相关的所有动态数据和通知 //并撤消用户已授予它的所有权限,但不删除其核心文件。使用此方法时请多加注意。 runningProcessInfos=iAbilityManager.getAllRunningProcesses(); for (int i=0; i { ToastSample toastSample=new ToastSample(); toastSample.SampleToast(this,ToastText, alignment,x,y,ms); }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
三、编译项目并运行应用程序
运行模拟器
运行效果
运行效果
运行效果
四、感谢您的观看[微笑],不足之处敬请批评指正,期待您的关注与收藏,您的关注与转发就是我的动力,下期您会收到更精彩的文章,谢谢!
花粉社群VIP加油站
猜你喜欢