<返回更多

鸿蒙系统(HarmonyOS)获取APP与ability信息

2021-05-14    DevEcoStudio
加入收藏

一、设计布局

<Button
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:id="$+id:GetAppInfo"
    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:ShowAPPInfo"
    ohos:left_padding="20fp"
    ohos:right_padding="20fp"
    ohos:top_padding="0fp"
    ohos:text_size="14fp"
    ohos:text_color="#fff"
    ohos:multiple_lines="true"
    ohos:text=""></Text>

二、打开MainAbilitySlice.JAVA文件录入以下关键代码

private static final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00888, "BingJunNetWorkLog");

private Text ShowAPPInfo;
@Override
public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);
    Button GetAPPInfo=(Button) findComponentById(ResourceTable.Id_GetAPPInfo);
    GetAPPInfo.setClickedListener(clickedListener);
    ShowAPPInfo=(Text) findComponentById(ResourceTable.Id_ShowAPPInfo);
}

public Component.ClickedListener clickedListener= component -> {
    switch (component.getId()){
        case ResourceTable.Id_GetAPPInfo:
            ShowAPPInfo.setText("");
            this.UpdateAPPInfo();

            break;
        default:
            break;
    }
};

public void UpdateAPPInfo(){
    TaskDispatcher taskDispatcher=this.getUITaskDispatcher();
    taskDispatcher.asyncDispatch(() -> {
        AbilityInfo abilityInfo = this.getAbilityInfo();;
        try{
            //ShowAPPInfo.append("包名:"+abilityInfo.bundleName+"rn");
            //ShowAPPInfo.append("类名:"+abilityInfo.className+"rn");
            //ShowAPPInfo.append("标签:"+abilityInfo.label+"rn");
            //ShowAPPInfo.append("描述与资源索引:"+abilityInfo.getIconPath()+"rn");
            //ShowAPPInfo.append("专用标签:"+abilityInfo.getBackgroundModes()+"rn");
            ShowAPPInfo.append("包名:"+abilityInfo.getBundleName()+"rn");
            ShowAPPInfo.append("类名:"+abilityInfo.getClassName()+"rn");
            //ShowAPPInfo.append("原始类名:"+abilityInfo.getOriginalClassName()+"rn");
            //ShowAPPInfo.append("进程名:"+abilityInfo.getProcess()+"rn");
            //ShowAPPInfo.append("默认宽度:"+abilityInfo.getDefaultFormWidth());
            //ShowAPPInfo.append("默认高度:"+abilityInfo.getDefaultFormHeight()+"rn");
            ShowAPPInfo.append("描述:"+abilityInfo.getDescription()+"rn");
            /*if (abilityInfo.getFormEnabled()==true){
                ShowAPPInfo.append("启用AbilityForm:是rn");
            }else {
                ShowAPPInfo.append("启用AbilityForm:否rn");
            }
            ShowAPPInfo.append("显示AbilityForm的区域:"+abilityInfo.getFormEntity()+"rn");*/
            ShowAPPInfo.append("图标路径:"+abilityInfo.getIconPath()+"rn");
            ShowAPPInfo.append("标签:"+abilityInfo.getLabel()+"rn");
            if (abilityInfo.getLaunchMode()== AbilityInfo.LaunchMode.STANDARD){
                ShowAPPInfo.append("模式:多例模式rn");
            }else {
                ShowAPPInfo.append("模式:单例模式rn");
            }
            //ShowAPPInfo.append("最小宽度:"+abilityInfo.getMinFormWidth());
            //ShowAPPInfo.append("最小高度:"+abilityInfo.getMinFormHeight()+"rn");
            ShowAPPInfo.append("模块标签:"+abilityInfo.getModuleName()+"rn");
            if (abilityInfo.getOrientation()==AbilityInfo.DisplayOrientation.UNSPECIFIED){
                ShowAPPInfo.append("显示方向:由系统决定显示方向rn");
            }else if (abilityInfo.getOrientation()== AbilityInfo.DisplayOrientation.PORTRAIT){
                ShowAPPInfo.append("显示方向:纵向rn");
            }else if (abilityInfo.getOrientation()== AbilityInfo.DisplayOrientation.LANDSCAPE){
                ShowAPPInfo.append("显示方向:横向rn");
            }else if (abilityInfo.getOrientation()== AbilityInfo.DisplayOrientation.FOLLOWRECENT){
                ShowAPPInfo.append("显示方向:与堆栈中最接近的能力方向相同rn");
            }
            //ShowAPPInfo.append("读取数据所需许可:"+abilityInfo.getReadPermission()+"rn");
            //ShowAPPInfo.append("编写数据所需许可:"+abilityInfo.getWritePermission()+"rn");
            //ShowAPPInfo.append("目标能力:"+abilityInfo.getTargetAbility()+"rn");
            if (abilityInfo.getType()== AbilityInfo.AbilityType.DATA){
                ShowAPPInfo.append("模板类型:数据访问服务rn");
            }else if (abilityInfo.getType()== AbilityInfo.AbilityType.PAGE){
                ShowAPPInfo.append("模板类型:UI页面rn");
            }else if (abilityInfo.getType()== AbilityInfo.AbilityType.SERVICE){
                ShowAPPInfo.append("模板类型:服务rn");
            }else if (abilityInfo.getType()== AbilityInfo.AbilityType.WEB){
                ShowAPPInfo.append("模板类型:华为浏览服务rn");
            }else if (abilityInfo.getType()== AbilityInfo.AbilityType.UNKNOWN){
                ShowAPPInfo.append("模板类型:未知的能力类型rn");
            }
            //ShowAPPInfo.append("统一资源标识符:"+abilityInfo.getURI()+"rn");
            /*if (abilityInfo.isDifferentName()==true){
                ShowAPPInfo.append("过渡:是rnrn");
            }else {
                ShowAPPInfo.append("过渡:否rnrn");
            }
            if (abilityInfo.isVisible()==true){
                ShowAPPInfo.append("能够被其他能力调用:是rnrn");
            }else {
                ShowAPPInfo.append("能够被其他能力调用:否rnrn");
            }*/
            //ShowAPPInfo.append("名称和hashCode:"+abilityInfo.toString()+"rn");
            if (abilityInfo.enabled==true){
                ShowAPPInfo.append("是否可以实例化:是rn");
            }else {
                ShowAPPInfo.append("是否可以实例化:否rn");
            }
            /*List<String> DeviceTypesList=abilityInfo.getDeviceTypes();
            for (int i=0; i<DeviceTypesList.size(); i++){
                ShowAPPInfo.append("能够运行设备类型:"+DeviceTypesList.get(i)+"rn");
            }
            List<String> DeviceCapabilitiesList=abilityInfo.getDeviceCapabilities();
            for (int i=0; i<DeviceCapabilitiesList.size(); i++){
                ShowAPPInfo.append("所需设备功能:"+DeviceCapabilitiesList.get(i)+"rn");
            }*/
            List<String> PermissionsList=abilityInfo.getPermissions();
            for (int i=0; i<PermissionsList.size(); i++){
                ShowAPPInfo.append("所需权限:"+PermissionsList.get(i)+"rnrn");
            }

        }catch (Exception exception){
            HiLog.error(hiLogLabel,"能力信息错误:"+exception.toString());
        }finally {

        }

        ApplicationInfo applicationInfo=this.getApplicationInfo();
        try {
            //ShowAPPInfo.append("APP描述与资源索引:"+applicationInfo.getDescription()+"rn");
            //ShowAPPInfo.append("图标路径:"+applicationInfo.getIcon()+"rn");
            ShowAPPInfo.append("app名:"+applicationInfo.getName()+"rn");
            ShowAPPInfo.append("应用程序Entry.hap路径:"+applicationInfo.getEntryDir()+"rn");
            //ShowAPPInfo.append("app标志:"+applicationInfo.getFlags()+"rn");
            //ShowAPPInfo.append("APP标签:"+applicationInfo.getLabel()+"rn");
            //ShowAPPInfo.append("进程名:"+applicationInfo.getProcess()+"rn");
            if (applicationInfo.getSupportedModes()==1){
                ShowAPPInfo.append("行驶模式:支持rn");
            }else {
                ShowAPPInfo.append("行驶模式:不支持rn");
            }
            /*List<ModuleInfo> ModuleInfoList= applicationInfo.getModuleInfos();
            for (int i=0; i<ModuleInfoList.size(); i++){
                ShowAPPInfo.append("模块:"+ModuleInfoList.get(i)+"rn");
            }
            List<String> ModuleSourceDirsList=applicationInfo.getModuleSourceDirs();
            for (int i=0; i<ModuleSourceDirsList.size(); i++){
                ShowAPPInfo.append("模块资源路径:"+ModuleInfoList.get(i)+"rn");
            }
            List<String> PermissionsList=applicationInfo.getPermissions();
            for (int i=0; i<PermissionsList.size(); i++){
                ShowAPPInfo.append("所需权限:"+PermissionsList.get(i)+"rn");
            }*/
        }catch (Exception exception){
            HiLog.error(hiLogLabel,"APP信息错误:"+exception.toString());
        }finally {

        }
    });

}

三、编译项目并运行

鸿蒙系统(HarmonyOS)获取APP与ability信息

编译项目


鸿蒙系统(HarmonyOS)获取APP与ability信息
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>