2. 进程模型
系统的进程模型如下图所示
- 应用中(同一Bundle名称)的所有UIAbility、ServiceExtensionAbility和DataShareExtensionAbility均是运行在同一个独立进程(主进程)中,如下图中绿色部分的“Main Process”。
- 应用中(同一Bundle名称)的所有同一类型ExtensionAbility(除ServiceExtensionAbility和DataShareExtensionAbility外)均是运行在一个独立进程中,如下图中蓝色部分的“FormExtensionAbility Process”、“InputMethodExtensionAbility Process”、其他ExtensionAbility Process。
- WebView拥有独立的渲染进程,如下图中黄色部分的“Render Process”。
编辑
2.1 公共事件机制
编辑
通过一个应用向系统发送事件从而影响其他应用
2.2 卡片与应用通信
先创建一个卡片
通过应用发送事件,通过卡片接收
通过卡片发送事件,通过应用接收
准备一个发布订阅工具
import commonEventManager from '@ohos.commonEventManager'
class SubscriberClass {
subscriber?: commonEventManager.CommonEventSubscriber
publishCount: number = 0
publish(eventType: string, data: string = '') {
commonEventManager.publish(eventType, { data }, (err) => {
})
}
subscribe(eventType: string, callback: (event: string) => void) {
// 1.创建订阅者
commonEventManager.createSubscriber({ events: [eventType] }, (err, data) => {
if (err) {
return console.log('logData:', '创建订阅者失败')
}
// 2.data是订阅者
this.subscriber = data
if (this.subscriber) {
// 3.订阅事件
commonEventManager.subscribe(this.subscriber, (err, data) => {
if (err) {
return console.log('logData:', '订阅者事件失败')
}
if (data.data) {
callback(data.data)
}
})
}
})
}
}
export const subscriberClass = new SubscriberClass()
1.应用发
import { subscriberClass } from '../utils/SubscriberClass';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Column() {
Button('测试通知卡片')
.onClick(()=>{
subscriberClass.publish('cardUpdate','time')
})
}
.height('100%')
.width('100%')
}
}
2.卡片收
import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
import { Want } from '@kit.AbilityKit';
import { subscriberClass } from '../utils/SubscriberClass';
export default class EntryFormAbility extends FormExtensionAbility {
onAddForm(want: Want) {
// Called to return a FormBindingData object.
let formData = '';
let formId: string = want.parameters![formInfo.FormParam.IDENTITY_KEY] as string
subscriberClass.subscribe('cardUpdate', (event) => {
switch (event) {
case 'time':
formProvider.updateForm(formId, formBindingData.createFormBindingData({
time: Date.now()
}))
}
})
return formBindingData.createFormBindingData(formData);
}
onCastToNormalForm(formId: string) {
// Called when the form provider is notified that a temporary form is successfully
// converted to a normal form.
}
onUpdateForm(formId: string) {
// Called to notify the form provider to update a specified form.
}
onFormEvent(formId: string, message: string) {
// Called when a specified message event defined by the form provider is triggered.
}
onRemoveForm(formId: string) {
// Called to notify the form provider that a specified form has been destroyed.
}
onAcquireFormState(want: Want) {
// Called to return a {@link FormState} object.
return formInfo.FormState.READY;
}
}
3.卡片页面
const localStorage = new LocalStorage()
@Entry(localStorage)
@Component
struct WidgetCard {
@LocalStorageProp('time')
time: number = 0
build() {
Column() {
Text(this.time + '')
.fontSize($r('app.float.font_size'))
.fontWeight(FontWeight.Medium)
.fontColor($r('app.color.item_title_font'))
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
4.卡片发
const localStorage = new LocalStorage()
@Entry(localStorage)
@Component
struct WidgetCard {
@LocalStorageProp('time')
time: number = 0
build() {
Column() {
Text(this.time + '')
.fontSize($r('app.float.font_size'))
.fontWeight(FontWeight.Medium)
.fontColor($r('app.color.item_title_font'))
Button('测试通知应用')
.onClick(()=>{
postCardAction(this,{
'action':'message'
})
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
5.卡片ability
onFormEvent(formId: string, message: string) {
// Called when a specified message event defined by the form provider is triggered.
subscriberClass.publish('appUpdate',Date.now().toString())
}
6.页面收
import { subscriberClass } from '../utils/SubscriberClass';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
aboutToAppear(): void {
subscriberClass.subscribe('appUpdate', (event) => {
this.message = event
})
}
build() {
Column() {
Button('测试通知卡片')
.onClick(() => {
subscriberClass.publish('cardUpdate', 'time')
})
Text('当前卡片发送的消息:' + this.message)
}
.height('100%')
.width('100%')
}
}
2.3 进程间通信服务(优化)
采用@ohos.rpc (RPC通信)改造卡片应用通信方案
卡片发:
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params: {
method: 'time'
}
})
应用收:
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
this.callee.on("time", (data) => {
//TODO:拿到数据可以进行响应的操作
return new Params() // 只是为了不报错
})
}
HarmonyOS赋能资源丰富度建设(第四期)-吴东林
developer.huawei.com/consumer/cn…