Android程序获取鸿蒙手机设备信息(是否鸿蒙手机、版本号、小版本号等)

1、效果图

鸿蒙手机 --> 关于手机的截图:

Android程序获取鸿蒙手机设备信息的截图:

2、实现

本案例DEMO的实现主要借鉴了网上现有的资料: https://blog.csdn.net/chenzhengfeng/article/details/119868210

并在此基础上进行了扩展、封装,希望能帮到有需要的小伙伴们~

2.1、鸿蒙工具类封装 HarmonyUtils

import android.text.TextUtils;
import java.lang.reflect.Method;

/**
 * 齐行超
 * 2020-02-16
 */
public class HarmonyUtils {
    /**
     * 是否为鸿蒙系统
     *
     * @return true为鸿蒙系统
     */
    public static boolean isHarmonyOs() {
        try {
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            return "Harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable x) {
            return false;
        }
    }

    /**
     * 获取鸿蒙系统版本号
     *
     * @return 版本号
     */
    public static String getHarmonyVersion() {
        return getProp("hw_sc.build.platform.version", "");
    }

    /**
     * 获取属性
     * @param property
     * @param defaultValue
     * @return
     */
    private static String getProp(String property, String defaultValue) {
        try {
            Class spClz = Class.forName("android.os.SystemProperties");
            Method method = spClz.getDeclaredMethod("get", String.class);
            String value = (String) method.invoke(spClz, property);
            if (TextUtils.isEmpty(value)) {
                return defaultValue;
            }
            return value;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    /**
     * 获得鸿蒙系统版本号(含小版本号,实际上同Android的android.os.Build.DISPLAY)
     * @return 版本号
     */
    public static String getHarmonyDisplayVersion() {
        return android.os.Build.DISPLAY;
    }
}

2.2、HarmonyUtils的调用

当前案例中,在一个activity中实现了对HarmonyUtils工具类的调用。
Activity代码:


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class HarmonyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_harmony);

        TextView tv_is_harmony = findViewById(R.id.tv_is_harmony);
        TextView tv_harmony_version = findViewById(R.id.tv_harmony_version);
        TextView tv_harmony_display_version = findViewById(R.id.tv_harmony_display_version);

        boolean isHarmony = HarmonyUtils.isHarmonyOs();
        tv_is_harmony.setText("is harmony ?  \n  "+ isHarmony);

        String harmonyVersion = HarmonyUtils.getHarmonyVersion();
        tv_harmony_version.setText("harmony version is: \n  "+harmonyVersion);

        String displayVersion = HarmonyUtils.getHarmonyDisplayVersion();
        tv_harmony_display_version.setText("harmony display version is: \n  "+displayVersion);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".HarmonyActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="is harmony ?"
        android:id="@+id/tv_is_harmony"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_is_harmony"
        android:id="@+id/tv_harmony_version"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:text="harmony version"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_harmony_version"
        android:id="@+id/tv_harmony_display_version"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:text="harmony display version"/>

</androidx.constraintlayout.widget.ConstraintLayout>
posted @ 2022-02-16 10:38  齐行超  阅读(3337)  评论(0编辑  收藏  举报