提交 c37295bc 编写于 作者: L liuliu

fix arkts

Signed-off-by: Nliuliu <liuliu40@huawei.com>
Change-Id: Iad59db095a2d10996ea7b242863e269b0574d545
上级 c6a78eba
......@@ -30,7 +30,7 @@ async function restartAbility() {
在目标端PageAbility的onNewWant回调中获取包含页面信息的want参数:
```ts
import Want from '@ohos.app.ability.Want';
// GlobalContext.ts 构造单例对象
export class GlobalContext {
private constructor() {}
private static instance: GlobalContext;
......@@ -51,6 +51,11 @@ export class GlobalContext {
this._objects.set(key, objectClass);
}
}
```
```ts
import Want from '@ohos.application.Want';
import { GlobalContext } from './GlobalContext';
export default class EntryAbility{
onNewWant(want: Want) {
......@@ -63,8 +68,9 @@ export default class EntryAbility{
在目标端页面的自定义组件中获取包含页面信息的want参数并根据uri做路由处理:
```ts
import Want from '@ohos.application.Want';
import router from '@ohos.router';
import { GlobalContext } from '../GlobalContext'
import { GlobalContext } from '../GlobalContext';
@Entry
@Component
......@@ -73,10 +79,12 @@ struct Index {
onPageShow() {
console.info('Index onPageShow')
let newWant: Want = GlobalContext.getContext().getObject("newWant")
if (newWant.hasOwnProperty("page")) {
router.push({ url: newWant.page });
GlobalContext.getContext().setObject("newWant", undefined)
let newWant = GlobalContext.getContext().getObject("newWant") as Want
if (newWant.parameters) {
if (newWant.parameters.page) {
router.push({ url: newWant.parameters.page });
GlobalContext.getContext().setObject("newWant", undefined)
}
}
}
......@@ -160,7 +168,7 @@ export default class EntryAbility {
if (want.parameters) {
if (want.parameters.page) {
router.push({
url: want.parameters.page
url: want.parameters.page as string
})
}
}
......
......@@ -333,7 +333,7 @@ import { GlobalContext } from '../GlobalContext'
export default class MySecondAbility extends UIAbility {
onDestroy() {
let applicationContext = this.context.getApplicationContext();
let lifecycleId: number = GlobalContext.getContext().getObject("lifecycleId");
let lifecycleId = GlobalContext.getContext().getObject("lifecycleId") as number;
// 3.通过applicationContext注销监听应用内生命周期
applicationContext.off('abilityLifecycle', lifecycleId, (error) => {
if (error && error.code !== 0) {
......
......@@ -27,53 +27,30 @@ onApplicationForeground(): void;
import UIAbility from '@ohos.app.ability.UIAbility';
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';
// 构造单例对象
export class GlobalContext {
private constructor() {}
private static instance: GlobalContext;
private _objects = new Map<string, Object>();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
onApplicationForeground() {
console.info('applicationStateChangeCallback onApplicationForeground');
},
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
}
GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback);
export default class MyAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext());
// 1.获取applicationContext
let applicationContext = GlobalContext.getContext().getObject("applicationContext");
let applicationContext = this.context.getApplicationContext();
// 2.通过applicationContext注册应用前后台状态监听
if (applicationContext != undefined) {
applicationContext.on('applicationStateChange',
GlobalContext.getContext().getObject("applicationStateChangeCallback"));
applicationContext.on('applicationStateChange', applicationStateChangeCallback);
}
}
onDestroy() {
let applicationContext = GlobalContext.getContext().getObject("applicationContext");
let applicationContext = this.context.getApplicationContext();
// 1.通过applicationContext解除注册应用前后台状态监听
if (applicationContext != undefined) {
applicationContext.off('applicationStateChange',
GlobalContext.getContext().getObject("applicationStateChangeCallback"));
applicationContext.off('applicationStateChange', applicationStateChangeCallback);
}
}
}
......@@ -93,53 +70,31 @@ onApplicationBackground(): void;
import UIAbility from '@ohos.app.ability.UIAbility';
import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback';
export class GlobalContext {
private constructor() {}
private static instance: GlobalContext;
private _objects = new Map<string, Object>();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}
let applicationStateChangeCallback: ApplicationStateChangeCallback = {
onApplicationForeground() {
console.info('applicationStateChangeCallback onApplicationForeground');
},
onApplicationBackground() {
console.info('applicationStateChangeCallback onApplicationBackground');
}
}
GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback);
export default class MyAbility extends UIAbility {
onCreate() {
console.log('MyAbility onCreate');
GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext());
// 1.获取applicationContext
let applicationContext = GlobalContext.getContext().getObject("applicationContext");
let applicationContext = this.context.getApplicationContext();
// 2.通过applicationContext注册应用前后台状态监听
if (applicationContext != undefined) {
applicationContext.on('applicationStateChange',
GlobalContext.getContext().getObject("applicationStateChangeCallback"));
applicationContext.on('applicationStateChange', applicationStateChangeCallback);
}
console.log('Resgiter applicationStateChangeCallback');
}
onDestroy() {
let applicationContext = GlobalContext.getContext().getObject("applicationContext");
let applicationContext = this.context.getApplicationContext();
// 1.通过applicationContext解除注册应用前后台状态监听
if (applicationContext != undefined) {
applicationContext.off('applicationStateChange',
GlobalContext.getContext().getObject("applicationStateChangeCallback"));
applicationContext.off('applicationStateChange', applicationStateChangeCallback);
}
}
}
......
......@@ -37,10 +37,7 @@ ExtensionContext主要用于查询所属Extension的信息、Module的配置信
三个Module内都定义一个相同名称的ServiceExtension:
```ts
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';
import rpc from '@ohos.rpc';
// 单例对象 GlobalContext.ts
export class GlobalContext {
private constructor() {}
private static instance: GlobalContext;
......@@ -61,6 +58,13 @@ export class GlobalContext {
this._objects.set(key, objectClass);
}
}
```
```ts
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';
import rpc from '@ohos.rpc';
import { GlobalContext } from '../GlobalContext'
export default class TheServiceExtension extends ServiceExtension {
onCreate(want: Want) {
......@@ -109,7 +113,8 @@ export default class EntryAbility extends UIAbility {
在entry内新建一个ServiceModule.ts,专用于执行业务逻辑
```ts
import { GlobalContext } from '../TheServiceExtension'
import common from '@ohos.app.ability.common';
import { GlobalContext } from '../GlobalContext';
export default class ServiceModel {
moduleName: string = '';
......@@ -122,7 +127,8 @@ export default class ServiceModel {
return;
}
this.moduleName = GlobalContext.getContext().getObject('ExtensionContext').currentHapModuleInfo.name;
let extensionContext = GlobalContext.getContext().getObject('ExtensionContext') as common.ServiceExtensionContext;
this.moduleName = extensionContext.currentHapModuleInfo.name;
// 根据moduleName执行不同的业务逻辑,实现对不同性能设备的区分
switch (this.moduleName) {
case 'highPerformance':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册