编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

鸿蒙(Harmony)开发手机管家APP示例

wxchong 2024-08-19 01:53:05 开源技术 21 ℃ 0 评论

一、设计布局文件

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="$graphic:background_ability_main"
    ohos:orientation="vertical">

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:CheckMemory"
        ohos:padding="5fp"
        ohos:top_margin="10fp"
        ohos:left_margin="20fp"
        ohos:right_margin="20fp"
        ohos:bottom_margin="10fp"
        ohos:text_size="22fp"
        ohos:text_color="#fff"
        ohos:background_element="$graphic:background_button"
        ohos:text="检查设备"></Button>

    <Text
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:ShowMemoryInfo"
        ohos:left_padding="20fp"
        ohos:right_padding="20fp"
        ohos:top_padding="0fp"
        ohos:text_size="16fp"
        ohos:text_color="#fff"
        ohos:multiple_lines="true"
        ohos:text=""></Text>

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:ClearMemory"
        ohos:padding="5fp"
        ohos:top_margin="10fp"
        ohos:left_margin="20fp"
        ohos:right_margin="20fp"
        ohos:bottom_margin="10fp"
        ohos:text_size="22fp"
        ohos:text_color="#fff"
        ohos:background_element="$graphic:background_button"
        ohos:text="清理内存与进程"></Button>
</DirectionalLayout>

二、打开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<RunningProcessInfo> 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()+"\r\n");
                ShowMemoryInfo.append("可用系统内存大小:"+systemMemoryInfo.getAvailSysMem()+"\r\n");
                ShowMemoryInfo.append("可用系统内存的阈值:"+systemMemoryInfo.getThreshold()+"\r\n");
                if (systemMemoryInfo.isLowSysMemory()==true){
                    ShowMemoryInfo.append("内存不足\r\n");
                }else {
                    ShowMemoryInfo.append("内存充足\r\n");
                }
                if (iAbilityManager.isLowRamDevice()==true){
                    ShowMemoryInfo.append("低RAM设备\r\n");
                }else {
                    ShowMemoryInfo.append("高RAM设备\r\n");
                }
                ShowMemoryInfo.append("基本内存大小:"+IAbilityManager.DEFAULT_HEAPSIZE+"\r\n");
                ShowMemoryInfo.append("默认密度:"+IAbilityManager.DEFAULT_DENSITY+"\r\n");
                ShowMemoryInfo.append("主屏幕上图标的密度:"+iAbilityManager.getHomeScreenIconDensity()+"\r\n");
                ShowMemoryInfo.append("默认图标大小:"+IAbilityManager.DEFAULT_ICONSIZE+"\r\n");
                ShowMemoryInfo.append("主屏幕上图标的大小:"+iAbilityManager.getHomeScreenIconSize()+"\r\n");
                ShowMemoryInfo.append("应用程序可以使用的最大内存:"+iAbilityManager.getAppLargeMemory()+"\r\n");
                ShowMemoryInfo.append("应用程序可以使用的内存:"+iAbilityManager.getAppMemory()+"\r\n");
                deviceConfigInfo=iAbilityManager.getDeviceConfigInfo();
                ShowMemoryInfo.append("所需的OpenGL ES版本:"+deviceConfigInfo.getDeviceGLESVersion()+"\r\n");
                if (deviceConfigInfo.getExternalInputDevices()==0){
                    ShowMemoryInfo.append("不需要外部输入设备:\r\n");
                }else if (deviceConfigInfo.getExternalInputDevices()==1){
                    ShowMemoryInfo.append("需要外部物理键盘\r\n");
                } else {
                    ShowMemoryInfo.append("需要外部五向(上,下,右,左和选择)导航设备\r\n");
                }
                if (deviceConfigInfo.getKeyBoardType()==0){
                    ShowMemoryInfo.append("不需要键盘\r\n");
                }else if (deviceConfigInfo.getKeyBoardType()==1){
                    ShowMemoryInfo.append("需要具有QWERTY布局的键盘\r\n");
                }else {
                    ShowMemoryInfo.append("需要具有12键布局的键盘\r\n");
                }
                if (deviceConfigInfo.getNavigationType()==0){
                    ShowMemoryInfo.append("不需要导航设备\r\n");
                }else if (deviceConfigInfo.getNavigationType()==1){
                    ShowMemoryInfo.append("所需的导航设备:方向键\r\n");
                }else if (deviceConfigInfo.getNavigationType()==2){
                    ShowMemoryInfo.append("所需的导航设备:跟踪球\r\n");
                }else {
                    ShowMemoryInfo.append("所需的导航设备:滚轮\r\n");
                }
                if (deviceConfigInfo.getTouchScreenType()==0){
                    ShowMemoryInfo.append("不需要触摸屏\r\n");
                }else {
                    ShowMemoryInfo.append("需要可以用指尖操作的触摸屏\r\n");
                }
                /*runningProcessInfos=iAbilityManager.getAllRunningProcesses();
                for (int i=0; i<runningProcessInfos.size(); i++){
                    ShowMemoryInfo.append("进程"+runningProcessInfos.get(i).getProcessName());
                }*/
            }catch (Exception exception){
                HiLog.error(hiLogLabel,"错误:"+exception.toString());
            }
        });
    }

    public void ClearMemoryAndKillProcesses(){
        TaskDispatcher taskDispatcher=this.getUITaskDispatcher();
        taskDispatcher.asyncDispatch(() -> {
            try {
                //iAbilityManager.clearUpApplicationData();
                //清除应用程序的用户数据,这相当于初始化应用程序。
                //此方法删除与该应用程序相关的所有动态数据和通知
                //并撤消用户已授予它的所有权限,但不删除其核心文件。使用此方法时请多加注意。
                runningProcessInfos=iAbilityManager.getAllRunningProcesses();
                for (int i=0; i<runningProcessInfos.size(); i++){
                    //iAbilityManager.killProcessesByBundleName(runningProcessInfos.get(i).getProcessName());
                    //根据实际需要杀死与指定程序包关联的所有后台进程。
                }
            }catch (Exception exception){
                HiLog.error(hiLogLabel,"错误:"+exception.toString());
            }
        });
    }

    public void LoadSampleToast(final String ToastText,final int x,final int y,final int ms,final int alignment){
        TaskDispatcher taskDispatcher=this.getUITaskDispatcher();
        taskDispatcher.asyncDispatch(() -> {
            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);
    }
}

三、编译项目并运行应用程序

四、感谢您的观看[微笑],不足之处敬请批评指正,期待您的关注与收藏,您的关注与转发就是我的动力,下期您会收到更精彩的文章,谢谢!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表