写给Android开发的HarmonyOS白皮书

智能手机和平板电脑等移动设备运行Android操作系统, 该系统是在开源和基于 Linux 的模式下开发的. Android平台使开发人员能够在一个统一的平台上为移动设备开发应用, 因此他们的应用有可能在不同的Android设备上运行.

另一方面, 基于微内核的操作系统, 如HarmonyOS, 被认为比基于混合内核的操作系统更安全, 更高效.

为了在其设备上成功连接其他平台, 华为开发了名为HarmonyOS的多云平台操作系统.

HarmonyOS 在诞生之初就被称为嵌入式操作系统, 适用于工业应用或属于物联网的设备.

除了是多平台操作系统和由两家领先的技术公司创建之外, Android和 HarmonyOS 还有许多相似之处. 但实际上, 这两个平台却大相径庭.

HarmonyOS 有 Linux 内核, 微内核和LiteOS. 操作系统由多个内核组成, 可为不同资源能力的设备选择合适的内核.

让我们来看看Android和 HarmonyOS 的架构图:

2430370da3c43e460a4b472929bb1825.jpeg

Android系统架构图

根据下面的架构图, Android 由软件组件堆栈组成, 大致分为五个部分和四个层.

Android 库

在这一类别中可以找到专为 Android 使用而设计的库.

以下是可供开发人员使用的一些重要 Android 库的摘要:

Android.app - 这是所有Android应用的内核, 提供对应用模型的访问.

Android.content--使应用能够访问, 发布和传递内容.

Android.database - 包含一个 SQLite 数据库和管理类, 用户可在其中访问内容.

android.OpenGL - 使用 OpenGL ES 渲染 3D 图形的 API.

Android.os - 包含标准OS服务, 如消息访问, 网络访问和进程间通信.

Android.text - 渲染文本并在显示设备上对其进行操作.

Android.view - 构成应用的UI组件.

Android.widget - 设计GUI的组件, 包括按钮, 标签, 列表视图, 布局管理器和单选按钮等元素.

Android.WebKit--提供一种将网络浏览功能纳入应用的方法.

基于 Java 的 Android 运行时库现在已经介绍完毕, 我们将在 Android 栈的这一层中了解基于 C/C++ 的库.

Android运行时

位于第二层的底部, 是架构的第三部分. 它提供了一个名为 Dalvik 虚拟机的关键组件, 这是一个特定于 Android 的 Java 虚拟机.

作为 Android 运行时的一部分, 开发人员还可以使用标准 Java 编程语言编写 Android 应用.

应用的框架

由于有了应用框架层, 应用可以使用许多更高级别的服务. 这些服务可供应用开发人员使用.

下面列出了 Android 框架提供的主要服务:

  • 活动管理器 - 管理应用的生命周期和活动堆栈.

  • 内容提供者 - 在应用之间发布和共享数据.

  • 资源管理器 - 访问嵌入的非代码资源, 如字符串, 颜色和UI布局.

  • 通知管理器 - 在应用运行时显示警报和通知.

  • 视图系统 - 通过扩展视图类创建的UI.

应用

在顶层, 你可以找到所有的 Android 应用. 这一层将是你安装应用的唯一一层. 例如, 通讯录, 浏览器, 游戏等.

fb57fc7b44fc716ef76522febb2cbec3.jpeg

HarmonyOS 架构图

HarmonyOS由四层组成.

  • 内核层

  • 系统服务层

  • 框架层

  • 应用层

内核层

内核是操作系统的基础层. 使用 HarmonyOS, 可以为具有不同资源限制的设备选择多个内核. 内核抽象层(KAL)屏蔽了内核实现的差异, 并为上层提供内核功能, 如进程, 线程, 内存和文件系统管理以及外设管理.

系统服务层

系统服务使 HarmonyOS 能够通过其框架层为应用提供服务.

基本软件服务

HarmonyOS 提供通用和普遍的功能, 包括事件, 通知, 电话, 设计等 除了移动传感开发平台 (MSDP) 和 X(DFX) 设备虚拟化 (DV).

增强型软件服务

为 HarmonyOS 集成各种硬件和软件服务, 包括专门针对智能电视, 可穿戴设备和物联网设备的服务.

硬件服务

提供针对可穿戴设备和物联网设备的硬件服务, 如地理定位和生物识别.

框架层

这一层有两类应用: 系统应用和第三方应用.

网络浏览器和电子邮件客户端就是构成应用层的软件实例. 它提供了允许软件接收和发送数据并向用户展示信息的协议.

HarmonyOS应用受益于它自己的一套功能能力(Feature Abilities, FA)和粒子能力(Particle Abilities, PA).

FA 提供了与用户交互的UI.

与UI不同, PA 处理后台任务并提供后台数据访问.

基于 FA 和 PA 开发的应用专门针对业务特点量身定制, 可以在不同设备之间进行调度和分配, 为用户提供一致, 高效的体验.

当前操作系统存在的问题

  • 提供无缝的用户体验.

  • AI的无缝整合.

  • 操作系统的挑战.

  • 有超过一百万行代码.

  • 多设备互联带来安全问题.

  • 紧密耦合阻碍了生态系统的共享.

比方说, 你正在用手机听音乐, 但你可以在同一个房间里使用功能更强大的扬声器. 同样的音乐文件也可以在音质更好的手机上播放. 我们不会提示你利用移动设备上的最佳资源. 我们的目标是将设备无缝整合.

编写的应用不是为单一设备量身定制, 而是利用虚拟设备或虚拟资源.

随着 HarmonyOS 的发展, 消费者, 开发者和设备供应商都将从中受益. 凭借其先进的操作系统, HarmonyOS 将为下一代智能手机和 IoT(物联网)应用提供动力. HarmonyOS 为用户提供了引人入胜, 多样化和丰富的体验. HarmonyOS的目标是通过邀请世界各地的开发人员来建立这个新的生态系统, 为消费者提供智能体验, 无论他们身在何处.

e0c12042d0710479f05211f7508156d7.jpeg

代码管理的复杂性

上图显示了不同设备代码库代码管理的复杂性. 开发人员必须为多个物联网设备维护多个代码库.

f411e4acc862be5d099622c078d9e11b.jpeg

其他操作系统与 Harmony OS 之间的 OneHop 网络连接.

使用其他操作系统为物联网设备配置网络会因复杂的步骤和不同的条目而困难重重. 但有了HarmonyOS OneHop 网络连接, 将有助于自动配置, 如图所示.

客户, 设备制造商和开发商都将从 HarmonyOS 中获益. 对于下一代智能手机和物联网(IOT)应用而言, HarmonyOS 是最先进的操作系统.

HarmonyOS 旨在为用户提供引人入胜的多样化体验. 因此, 我们鼓励世界各地的开发人员加入并参与这个新的生态系统. 这些技术将在任何情况下为消费者提供无缝体验.

HarmonyOS能为Android开发者带来什么?

  • 设备只有在必要时才使用它, 就像模块化操作系统一样.

  • 模块化的解耦 HarmonyOS 可以根据需要嵌入到各种设备中.

  • 适应各种设备.

  • 分布式应用可通过分布式能力工具包以创建独立应用的相同方式进行开发.

  • 用于多设备开发的集成开发环境.

  • 编译器将支持多种语言, 实现统一开发.

理想情况下, 无论软件安装在哪里, 与用户连接的所有设备都能从硬件的连接功能中受益.

HarmonyOS 帮助我们实现了这一目标, 它创建了一个公平的生态系统, 能够在任何情况下实现全面连接, 让我们实现智能生态系统.

下面列出了 HarmonyOS 与Android系统的一些比较

什么是 AndroidMainifest.xml 的等价文件?

在Android系统中, AndroidMainifest.xml 文件包含软件包的信息, 包括应用的组件, 如活动, 服务, 广播接收器, 内容提供商等. 它还包括与项目相关的信息, 如应用名称, 应用图标, 应用主题, 权限, 应用版本, sdk 版本等.

<manifestxmlns:android="http://schemas.android.com/apk/res/android"
    package="com.org.example"
    android:versionCode="1"
    android:versionName="1.0" >  
    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />  
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >  
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >  
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>  
        </activity>
    </application>
</manifest>

同样, 在 HarmonyOS 中, 我们有config.json文件, 其中包含应用, API 版本, 模块, 设备配置等信息, 还包括与项目相关的信息, 如应用名称, 应用图标, 权限等. 更多详情请点击这里.

{
   "app" : {
     "bundleName" : "com.org.example" ,
     "vendor" : "vendorName" ,
     "version" : {
       "code" : 1000000,
       "name" : "1.0.0"
     },
     "apiVersion" : {
       "compatible" : 5,
       "target" : 5,
       "releaseType" : "Release"
     }
   },
   "deviceConfig" : {},
   "module" : {
     "package" : "com.org.example" ,
     "name" : ".MyApplication" ,
     "reqPermissions" : [
       {
         "name" : "ohos.permission.VIBRATE" ,
         "reason" : "vibration" ,
         "usedScene" : {
           "ability" : [
             ".MainAbility"
             ],
           "when" : "inuse"
         }
       }
     ],
     "deviceType" : [
       "phone"
       ],
     "abilities" : [
       {
         "skills" : [
           {
             "entities" : [
               "entity.system.home"
             ],
             "actions" : [
               "action.system.home"
             ]
           }
         ],
         "orientation" : "unspecified" ,
         "name" : "com.org.example.MainAbility" ,
         "icon" : "$media:icon" ,
         "description" : "$string:mainability_description" ,
         "label" : "$string:app_name" ,
         "type" : "page" ,
         "launchType" : "standard"
         }
     ]
   }
 }

正如你所看到的, 这两个文件都包含操作系统系统所需的有关应用的所有详细信息. 但这两个文件有一个主要区别, 那就是 AndroidManifest.xml 是 xml 格式, 而 config.json 是 json 格式. 下面是一些重要元素的比较.

1.  版本详情

AndroidManifest.xml 文件中, 你必须在  标签中写入版本详细信息, 并在  标签中写入 sdk 版本.

<manifest 
 android:versionCode="1"
 android:versionName="1.0" >
 
     <uses-sdk
         android:minSdkVersion="16"
         android:targetSdkVersion="30" />
 </manifest>

config.json 文件中, 你必须在应用对象中写入版本详细信息.

"app": {
   "version": {
     "code": 101,
     "name": "1.0.0"
   },
   "apiVersion": {
     "compatible": 5,
     "target": 5,
     "releaseType": "Release"
   }
 }

2.  包名

AndroidManifest.xml 文件中, 必须在  标签的根标签中写入软件包名.

<manifest
 package="com.org.example">
     ...
 </manifest>

config.json 文件中, 将软件包名称放在模块对象中.

"module": {
     "package": "com.orzangleli.radarview
    }

3.  Application 详情

AndroidManifest.xml 文件中, 你必须在  标签下填写应用的详细信息.

<application
     android:name=".MainApplication"
     android:allowBackup="true"
     android:icon="@drawable/logo"
     android:label="@string/app_name"
     android:supportsRtl="true"
     android:theme="@style/AppTheme">
 ……
 </application>

config.json 文件中, 你必须在多个对象 deviceConfigmodule 中填写应用的详情.

"deviceConfig": {
   "default": {
     "supportBackup": false,
     "network": {
       "cleartextTraffic": true,
       "securityConfig": {
         "domainSettings": {
           "cleartextPermitted": true,
           "domains": [
             {
               "subdomains": true,
               "name": "example.ohos.com"
             }
           ]
         }
       }
     }
   }
 },
 "module": {
   "package": "com.orzangleli.radarview",
   "name": ".MyApplication",
   "deviceType": [
     "phone"
   ],
   "colorMode": "light"
 }

4. Activity和Service

AndroidManifest.xml 文件中, 必须在  标签下用  和  标签分别注册活动和服务.

<application
     android:icon="@drawable/app_icon"
     android:label="@string/app_name" >
     <activity
         android:label="@string/activity_title"
         android:name=".MainActivity" >
         <intent-filter >
             <action          
                     android:name="android.intent.action.MAIN" />
 
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
     </activity>
    
    <service 
         android:name=".MyService"
         android:exported="true"
         />
 </application>

在 HarmonyOS 中, ActivityService对应的是Ability. 因此, 在 config.json 文件中, 你必须以 abilities json 数组的形式在模块对象下注册所有能力. 你可以使用 type 属性设置能力的类型(如页面, 服务).

"module": {
   …
   "abilities": [
     {
       "name": ".MainAbility",
       "description": "himusic main ability",
       "icon": "$media:ic_launcher",
       "label": "HiMusic",
       "launchType": "standard",
       "orientation": "unspecified",
       "permissions": [
       ],
       "visible": true,
       "skills": [
         {
           "actions": [
             "action.system.home"
           ],
           "entities": [
             "entity.system.home"
           ]
         }
       ],
       "type": "page"
     },
     {
       "name": ".MyService",
       "description": "himusic play ability",
       "icon": "$media:ic_launcher",
       "label": "My Service",
       "launchType": "standard",
       "orientation": "unspecified",
       "visible": false,
       "skills": [
         {
           "actions": [
             "action.play.music",
             "action.stop.music"
           ],
           "entities": [
             "entity.audio"
           ]
         }
       ],
       "type": "service"
     }
   ] 
 }

5. 权限

在 AndroidManifest.xml 文件中, 使用 < uses-permission> 标签在 < manifest > 标签下添加权限.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.org.example">
 
     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
 …
 
 </manifest>

config.json 文件中, 必须在模块对象下以 reqPermissions json 数组的形式添加权限.

"module": {
   "package": "com.org.example",
   "name": ".MyApplication",
   "reqPermissions": [
     {
       "name": "ohos.permission.READ_MEDIA",
       "reason": "reading files",
       "usedScene": {
         "ability": [
           ".MainAbility"
         ],
         "when": "always"
       }
     },
     {
       "name": "ohos.permission.WRITE_MEDIA",
       "reason": "writing files",
       "usedScene": {
         "ability": [
           ".MainAbility"
         ],
         "when": "always"
       }
     },
     {
       "name": "ohos.permission.INTERNET"
     }
   ] 
 }

Activity及其生命周期的等价物是什么?

在Android系统中, Activity 代表一个具有UI的单个屏幕. 在 HarmonyOS 中, 你可以使用PageAbility来提供UI. 一个页面能力可能包含一个能力片段, 也可能包含多个提供高度相关能力的能力片段.

public class MainAbility extends Ability {


    @Override


    public void onStart(Intent intent) {


        super.onStart(intent);


        super.setMainRoute(MainAbilitySlice.class.getName());


    }


}

在 Android 中, 你可以覆盖 Activity 的方法来捕获 activity 本身的生命周期方法. 在 HarmonyOS 中, 你可以覆盖Ability的方法来捕获页面Ability的生命周期方法. 有关页面Ability及其生命周期的更多详情, 请查看这里.

下图显示了页面Ability不同生命周期状态之间的转换, 以及在转换过程中调用的回调.

b00fdb7fecc5db4e3f866ed99c1d1e14.jpeg

页面能力的生命周期不同状态之间的转换

Fragment及其生命周期的等价物是什么?

在Android系统中, Fragment是Activity的一个部分, 它代表一种行为或UI的一部分. Fragment是一种将代码模块化的方法, 可以为更大的屏幕组成复杂的UI, 并有助于扩展应用的UI. 在 HarmonyOS 中, 你可以使用Fraction. Fraction可以作为UI的一部分放置在Ability中, 不能单独使用. 有关Fraction及其生命周期的更多详情, 请查看这里.

在下面的示例中, 你可以看到扩展的 Fraction 类, 其所有生命周期方法均已覆盖.

public class MyFraction extends Fraction {


    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return super.onComponentAttached(scatter, container, intent);
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }


    @Override
    protected void onActive() {
        super.onActive();
    }


    @Override
    protected void onInactive() {
        super.onInactive();
    }


    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    protected void onBackground() {
        super.onBackground();
    }


    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
    }
}

Service及其生命周期的等价物是什么?

在Android系统中, 服务是一个在后台运行的组件, 用于执行长期运行的操作, 如播放音乐, 处理网络事务, 与内容提供商交互等. 它没有任何 UI. 在 HarmonyOS 中, 你可以使用服务能力. 服务能力用于在后台运行任务, 如播放音乐或下载文件. 有关服务能力及其生命周期的更多详情, 请点击这里查看.

你可以按以下方式创建服务ability.

public class ServiceAbility extends Ability {


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }


    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
    }


    @Override
    public IRemoteObject onConnect(Intent intent) {
        return super.onConnect(intent);
    }


    @Override
    public void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
    }


    @Override
    public void onStop() {
        super.onStop();
    }


}

下图显示了服务ability生命周期中不同状态之间的转换.

93cced501df7f6d2d29946064ff0b66f.jpeg

服务Ability生命周期中不同状态之间的转换

LinearLayout 的等价物是什么?

在 Android 中, LinearLayout 用于线性布局部件(水平或垂直). 在HarmonyOS中, 你可以使用DirectionalLayout. 定向布局用于水平或垂直排列一组组件. 更多详情, 请点击这里.

在Android中:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

在HarmonyOS中:

<DirectionalLayout
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:orientation="vertical" >

RelativeLayout的等价物是什么?

在Android系统中, RelativeLayout将部件相对排列. 在 HarmonyOS 中, 你可以使用DependentLayout. 依赖布局提供了更多的布局模式. 你可以指定每个组件相对于同级其他组件的位置, 也可以指定相对于父组件的位置. 更多详情请点击这里.

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:background_element="$graphic:color_light_gray_element"><Text
    ohos:id="$+id:text1"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:left_margin="15vp"
    ohos:top_margin="15vp"
    ohos:bottom_margin="15vp"
    ohos:text="text1"
    ohos:text_size="20fp"
    ohos:background_element="$graphic:color_cyan_element"/><Text
    ohos:id="$+id:text2"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:left_margin="15vp"
    ohos:top_margin="15vp"
    ohos:right_margin="15vp"
    ohos:bottom_margin="15vp"
    ohos:text="end_of text1"
    ohos:text_size="20fp"
    ohos:background_element="$graphic:color_cyan_element"
    ohos:end_of="$id:text1"/></DependentLayout>

View在HarmonyOS中的等价物是什么?

在 Android 中, View 是屏幕上显示的所有内容的基础. View是小部件的基类, 用于创建交互式UI组件(按钮, 文本字段等). 在 HarmonyOS 中, 有Component类. Component在屏幕上占据一个矩形区域, 负责在该区域内绘图和处理事件. 有关 Component 的更多详情, 请点击这里.

下图显示了View层次结构(Android).

9c647594a3f2335d122b842c30f61ce0.jpeg

View层次结构(Android)

下图展示了Component层次结构(HMOS).

7b9a05bd27daaf02f5dc68be834d4d28.jpeg

Component和ComponentContainer层次结构(HarmonyOS)

基础UI组件(如TextView, EditText等)在HarmonyOS中的等价物是什么?

在 Android 中, 有一些预定义的部件可用于创建UI, 如 TextView, EditText, Button 等. 在 HarmonyOS 中, HMOS sdk 也提供了一些预定义组件.

以下是Android部件的替代列表.

1.  TextView -> Text

在Android系统中, TextView 是一个 UI 组件, 用于在用户的显示屏幕上显示文本. 在 HarmonyOS 中, 你可以使用Text. 更多详情查看这里.

<Text
     ohos:id="$+id:text_welcome"
     ohos:height="match_content"
     ohos:width="match_content"
     ohos:layout_alignment="horizontal_center"
     ohos:text="welcome"
     ohos:text_size="26fp"
 />

2.  EditText -> TextField

在 Android 中, EditText是一个允许用户输入文本的UI控件. 在 HarmonyOS 中, 你可以使用TextField. 更多详情查看这里.

<TextField
    ohos:id="$+id:input_email"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:hint="Enter Email"
    ohos:top_margin="8vp"
    ohos:text_size="16fp"
    ohos:padding="16vp"
    ohos:background_element="$graphic:background_text_field"
    ohos:text_input_type="pattern_text"
 />

3.  Button -> Button

在Android系统中, Button是一个UI, 用于在用户点击后立即执行某些操作. 在 HarmonyOS 中也有Button. 更多详情查看这里.

<Button
    ohos:id="$+id:btn_login"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="Login"
    ohos:layout_alignment="horizontal_center"
    ohos:text_size="18fp"  
    ohos:start_padding="20vp"
    ohos:end_padding="20vp"
    ohos:top_padding="16vp"
    ohos:bottom_padding="16vp"
 />

4.  ImageView -> Image

在 Android 中, ImageView 是一个可以显示图片的视图. 在 HarmonyOS 中, 你可以使用 Image. 更多详情查看这里.

<Image
    ohos:id="$+id:icon"
    ohos:height="200vp"
    ohos:width="200vp"
    ohos:image_src="$media:profile_person"
    ohos:scale_mode="clip_center"
 />

5.  Seekbar -> Slider

在Android系统中, Seekbar 是一个扩展的进度条. Seekbar带有一个指针, 可在整个进度条左右拖动. 该指针还有助于设置进度. 这有助于用户选择特定的数值范围. 在 HarmonyOS 中, 你可以使用Slider. 有关详细信息, 请单击这里.

<Slider
    ohos:id="$+id:slider"
    ohos:width="match_content"
    ohos:height="40vp"
    ohos:max="4"
    ohos:progress="4" />

6. CheckBox -> CheckBox

CheckBox是一种 UI 控件, 有两种状态, 即选中或未选中. 与 RadioGroup 不同的是, 如果我们有一组 CheckBox, 我们可以选择任意多个. 在 HarmonyOS 中也有CheckBox. 有关详细信息, 请单击这里.

<Checkbox
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="Agree"
 />

7.  Switch -> Switch

在 Android 中, Switch是一种双态 UI 元素, 可保持 ON 或 OFF 状态. 默认情况下, 开关处于关闭状态. 用户可以多次改变其状态. 在 HarmonyOS 中也有Switch. 有关详细信息, 请单击这里.

<Switch
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="Agree"
    ohos:enabled="false"
 />

8. Progressbar -> Progressbar, RoundProgressbar

在 Android 中, 我们使用进度条来显示正在进行的某些操作的进度, 如将文件粘贴到某个位置. 进度条有两种模式: a) 确定模式, b) 不确定模式.

在 HarmonyOS 中, 有 ProgressbarRoundProgressbar. 有关详细信息, 请 点击这里.

<ProgressBar
    ohos:id="$+id:progressbar"
    ohos:progress_width="10vp"
    ohos:height="60vp"
    ohos:width="600vp"
    ohos:max="100"
    ohos:min="0"
    ohos:progress="60" />
    <RoundProgressBar
     ohos:id="$+id:round_progress_bar"
     ohos:height="200vp"
     ohos:width="200vp"
     ohos:progress_width="10vp"
     ohos:progress="20"
     ohos:progress_color="#47CC47" />

9.  Toast -> ToastDialog

在 Andorid 中, Toast可用于在短时间内显示信息. Toast包含快速显示的信息, 并在一段时间后消失. 在 HarmonyOS 中, 你可以使用ToastDialog. 更多详情请点击这里.

new ToastDialog(getContext())
        .setText( "This is a ToastDialog displayed in the middle" )
        .setAlignment(LayoutAlignment.CENTER)
        .show();

什么是HarmonyOS中的Drawable?

在 Android 中, Drawable是可绘制到屏幕上的图形的一般概念, 你可以使用 getDrawable(int) 等 API 获取该资源, 或使用 android:drawableandroid:icon 等属性将其应用到另一个 XML 资源. 有几种不同类型的可绘制资源: 如ShapeDrawable, VectorDrawable, ScaleDrawable等.

在 HarmonyOS 中, 有Element. Element是一个可以绘制的抽象类. 当应用中需要显示静态图像时, 可以使用该类及其子类来绘制形状和图像. 有几个子类可根据元素的类型使用, 如 ShapeElement, VectorElementPixelMapElement. 更多详情请点击这里.

如何在 HarmonyOS 中创建通知?

在Android系统中, 通知是在应用外向用户显示的信息. 你需要使用 NotificationCompat.Builder 对象设置通知的内容和频道. 此外, 从 Android Oreo 开始, 必须创建通知通道. 要显示通知, 你需要使用 NotificationManager 对象.

同样, 在 HarmonyOS 中, 我们也有用于在应用外显示消息的通知. 要在 HarmonyOS 中创建通知, 需要使用 NotificationRequest 对象来设置通知内容, Slot(频道)和其他细节. 在 HarmonyOS 中, 与 NotificationChannel 相对应的是 NotificationSlot. 在 NotificationHelper 类的帮助下, 你可以发布通知. 有关通知的更多详情, 请点击这里.

//Create notification slot.
 NotificationSlot notificationSlot = new NotificationSlot("notification_001", "low priority", NotificationSlot.LEVEL_LOW);
 notificationSlot.setEnableVibration(true);
 notificationSlot.setEnableLight(true);
 try {
     NotificationHelper.addNotificationSlot(notificationSlot);
 } catch (RemoteException e) {
     e.printStackTrace();
 }
 
 //Create notification.
 NotificationRequest notificationRequest = new NotificationRequest();
 NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
 content.setTitle("Notification");
 content.setText("Hi there");
 NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
 notificationRequest.setTapDismissed(true);
 notificationRequest.setSlotId("notification_001");
 notificationRequest.setContent(notificationContent);
 notificationRequest.setIntentAgent(getLaunchAction());
 try {
     NotificationHelper.publishNotification(notificationRequest);
 } catch (RemoteException e) {
     e.printStackTrace();
 } // Create Intent agent.
 private IntentAgent getLaunchAction() {
     Operation operation = new Intent.OperationBuilder()
             .withDeviceId("")
             .withBundleName(this.getBundleName())
             .withAbilityName("com.huawei.notification.SecondAbility")
             .build();
     Intent intent = new Intent();
     intent.setOperation(operation);
     List<Intent> intentList = new ArrayList<>();
     intentList.add(intent);
     List<IntentAgentConstant.Flags> flags = new ArrayList<>();
     flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
     IntentAgentInfo paramsInfo = new IntentAgentInfo(90, IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
     return IntentAgentHelper.getIntentAgent(this, paramsInfo);
 }

如何在 HarmonyOS 中的任何组件上添加动画?

在Android系统中, 你可以使用XML创建动画, 或者在视图中调用animate()方法. 此外, 使用ValueAnimator, 你还能对动画进行更多控制, 并在执行的每一步对其进行自定义.

在 HarmonyOS 中, java sdk 提供了三个主要的动画类: FrameAnimationElement, AnimatorValue, AnimatorPropertyAnimatorGroup. 详情请查看这里.

什么是 HarmonyOS 中的本地数据库(SQLite)?

在Android系统中, 我们使用 SQLite 数据库来管理应用的本地数据, 而对于少量数据, 我们则使用 SharedPreference来存储键值对中的数据.

在 HarmonyOS 中, 对象关系映射(ORM)数据库在底层 SQLite 数据库中以结构化查询语言(SQL)进行掩码操作, 并提供一系列面向对象的应用接口, 用于添加, 删除, 修改和查询实体和关系数据. ORM 是 SQLite 数据库之上的一个抽象层. 有关 ORM 数据库的更多详情, 请查看这里.

下图是 ORM 数据库的工作流程.

7a3efdd7814921ccb1170557d8292235.jpeg

ORM 数据库如何工作

在 HarmonyOS 中, 与 SharedPreference 相对应的是 Lightweight Preference. 在Lightweight Preference中, 你可以以键值对的形式存储数据. 有关Lightweight Preference的更多详情, 请查看这里.

下图是Lightweight Preference数据库的工作流程.

93f007c325cf9afa04c235f13e2489ed.jpeg

轻量级偏好数据库的工作原理:

//  Example of Lightweight Preference database:
        
     Context context = getApplicationContext(); // Storage path of data files: /data/data/{PackageName}/preferences
        
     DatabaseHelper databaseHelper = new DatabaseHelper(context); // The input parameter context is of the ohos.app.Context type.
        
     String fileName = "test_pref"; // fileName indicates the file name, which cannot be empty or contain a path. The default path can be obtained by calling context.getPreferencesDir().
        
     Preferences preferences = databaseHelper.getPreferences(fileName);
     
     //Read data
     int value = preferences.getInt("intKey", 0);
     
     //Write data
     preferences.putInt("intKey", 3);
     preferences.putString("StringKey", "String value");
     preferences.flush();

总结一下

以上是我在学习 HarmonyOS 的过程中对 Android 开发的方方面面进行的比对, 所谓比较是最好的老师, 有了对照, 学习起来会更加容易上手一些! 当然, 今天的内容只是传统的 Android 开发与 HarmonyOS 的一些整体上的对比, 而 Jetpack Compose 这一部分并没有涉及到, 有机会再仔细查看一下Jetpack Compose 和 ArtUI 之间的不同与相似之处.

以上内容纯属一家之言, 欢迎批评斧正!

关注我获取更多知识或者投稿

55fdb70690e4399596c40061f4c374c6.jpeg

8b6b01eedcba360417703da081dd5545.jpeg

作者:bytebeats
链接:https://juejin.cn/post/7404775786706518057
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值