打破
数据驱动:移动应用赋能万物智联的解码之道,reasoning_content:我们要求以开发工程师的口吻,写一个与技术、科技相关的标题,主题是数据驱动:移动互联时代应用赋能万物智联的解码之道标题要简短精炼,30字以内直接输出,不要额外说明nn开发工程师的口吻可能更偏向技术实践、代码、架构、赋能等可以尝试:数据驱动解码万物智联,移动应用赋能开发之道但需要更精炼比如:数据驱动:移动应用赋能万物智联 但不够开发工程师口吻或者:代码驱动万物智联:数据赋能移动应用 等等nn考虑:数据驱动赋能,解码万物智联 但可能太泛nn更好的:数据驱动:应用赋能万物智联的解码 但超过30字?数一下:数据驱动:应用赋能万物智联的解码 共16字,可以但开发工程师口吻可以加一些技术术语如API、架构、引擎等不过要简洁nn或者:数据驱动应用,赋能万物智联 9字但缺少解码之道感觉nn解码之道:数据驱动移动应用赋能万物智联 17字可以nn更开发工程师口吻:数据驱动:移动应用赋能智联的API之道 但API可能不准确nn从数据到智联:开发者的解码引擎 12字但未体现移动互联nn最终选择:数据驱动:移动应用赋能万物智联的解码 16字,符合但感觉解码后面可以加之道?原题有解码之道加上:数据驱动:移动应用赋能万物智联的解码之道 19字,可以或者精简为数据驱动:移动应用赋能智联的解码之道 少一个万物,17字考虑直接输出
安全专家:嵌入式驱动筑牢万物互联移动生态,reasoning_content:我们要求以安全专家的口吻,写一个与技术、科技相关,关于嵌入式驱动:构建万物互联时代高效移动应用生态的标题需要简短精炼,30字以内直接输出标题,不要加说明nn注意:主题是嵌入式驱动:构建万物互联时代高效移动应用生态,但需要以安全专家的口吻所以标题要体现安全性、专家视角可能的思路:强调安全驱动、安全基石、安全赋能等例如:安全专家谈嵌入式驱动:筑牢万物互联移动生态 但字数可能超或者更简洁:安全驱动,护航万物互联移动生态 但需要体现嵌入式驱动和高效移动应用生态再想想nn嵌入式驱动安全专家:构建万物互联高效移动生态 字数?嵌入式驱动安全专家:构建万物互联高效移动生态 共19字?嵌入式驱动安全专家:构建万物互联高效移动生态 算标点?其实可以或者安全专家:嵌入式驱动筑牢万物互联移动应用生态 23字或者更短安全专家:嵌入式驱动,万物互联移动生态 15字但要求以安全专家的口吻,标题中最好有安全专家字样nn输出一个安全专家之嵌入式驱动:赋能万物互联移动生态 19字?安全专家之嵌入式驱动:赋能万物互联移动生态 共18字(不含冒号?)或者安全专家:嵌入式驱动,高效移动应用生态 16字但需要包含构建万物互联时代高效移动应用生态的意思可能安全专家:嵌入式驱动构建万物互联高效移动生态 22字OKnn考虑到直接输出,选择最简洁有力的
测评:多媒体赋能移动互联,智慧万物新篇章,reasoning_content:我们要求以技术工具测评师的口吻,写一个与“技术、科技”相关,关于[多媒体赋能移动互联,开启智慧万物新篇章]的标题需要简短精炼,30字以内直接输出标题,不要加说明nn分析:技术工具测评师的口吻应该比较专业、客观、评测角度主题是多媒体赋能移动互联,开启智慧万物新篇章可以提炼关键词:多媒体、移动互联、智慧万物、赋能、新篇章可以结合测评师常用语如“实测”、“测评”、“解读”、“探秘”等例如:“实测多媒体赋能移动互联:智慧万物新篇章”但超字数?30字内,需要精简nn构思:多媒体赋能移动互联,智慧万物开启新篇或者:测评:多媒体如何撬动移动互联智慧万物?但可能不够精炼尝试:“多媒体赋能移动互联,智慧万物新篇章测评”但有点长nn更合适:“多媒体+移动互联:智慧万物新篇章实测” 字数:多媒体+移动互联:智慧万物新篇章实测——共13个汉字+符号,符合nn或者:“技术评测:多媒体赋能移动互联,开启智慧万物” 共17字nn但要求直接输出标题,不要说明我觉得可以:“测评:多媒体赋能移动互联,智慧万物新篇” 共15字nn或者更简洁:“多媒体赋能移动互联,智慧万物新篇章评测” 16字nn确保不超过30字我选择:“实测|多媒体赋能移动互联,智慧万物新篇章” 共15字,加上符号注意符号算字符吗?通常汉字算一个字,符号算一个字符但要求“字数控制在30字以内”,通常指汉字字数,但严格来说标点也占位置为了保险,用纯汉字:“多媒体赋能移动互联开启智慧万物新篇章测评” 共17个汉字但“测评”在末尾稍显突兀或者“多媒体赋能移动互联智慧万物新篇章技术测评” 18字nn考虑到口吻,可以用“技术工具测评师”视角,加入“工具”一词?比如“多媒体工具赋能移动互联,智慧万物新篇章测评” 共18字nn另一种思路:用“技术测评”开头:“技术测评:多媒体赋能移动互联,开启智慧万物” 共18字nn最终决定:简洁有力,突出测评和主题输出:“测评:多媒体赋能移动互联,智慧万物新篇章” 共15字
15 9 月 2026, 周二

自定义GlobalThis进行数据同步

  这里自定义一下GlobalThis全局对象,通过在GlobalThis对象上绑定属性/方法, 下面通过一个简单实例,全局存储context,存储属性值,存储首选项对象。

  想了解更多关于开源的内容,请访问:

  51CTO 鸿蒙开发者社区

  https://ost.51cto.com

  1、前言

  在OpenHarmony 3.2 Release版本的配套文档,对应API能力级别为API 9 Release时,使用globalThis进行数据同步:在ArkTS引擎实例内部,globalThis是一个全局对象,可以被ArkTS引擎实例内的UIAbility组件、ExtensionAbility组件和ArkUI页面(Page)访问。但在OpenHarmony 4.0 Release版本的配套文档,对应API能力级别为API 10 Release后,就弃用了globalThis全局对象,如果我们之前的项目里有用到globalThis全局对象,而在新的API 10里不能用了,我们是可以通构造一个单例对象来处理的。这里自定义一下GlobalThis全局对象,通过在GlobalThis对象上绑定属性/方法, 下面通过一个简单实例,全局存储context,存储属性值,存储首选项对象。效果图如下:

  2、自定义GlobalThis单例对象

  复制

  import common from '@ohos.app.ability.common';

  import dataPreferences from '@ohos.data.preferences';

  // 构造单例对象

  export class GlobalThis {

  private constructor() {}

  private static instance: GlobalThis;

  // 缓存context

  private _uiContexts = new Map();

  // 缓存属性值

  private _attribute = new Map();

  // 缓存首选项

  private _preferences = new Map>();

  public static getInstance(): GlobalThis {

  if (!GlobalThis.instance) {

  GlobalThis.instance = new GlobalThis();

  }

  return GlobalThis.instance;

  }

  getContext(key: string): common.UIAbilityContext | undefined {

  return this._uiContexts.get(key);

  }

  setContext(key: string, value: common.UIAbilityContext): void {

  this._uiContexts.set(key, value);

  }

  getAttribute(key: string): string | undefined {

  return this._attribute.get(key);

  }

  setAttribute(key: string, value: string): void {

  this._attribute.set(key, value);

  }

  getPreferences(key: string): Promise | undefined {

  return this._preferences.get(key);

  }

  setPreferences(key: string, value: Promise): void {

  this._preferences.set(key, value);

  }

  // 其他需要传递的内容依此扩展

  }

  3、使用说明

  在EntryAbility.ets中导入构建的单例对象GlobalThis。

  复制

  import { GlobalThis } from '../utils/GlobalThis'; // 需要根据globalThis.ets的路径自行适配

  1.

  在onCreate中添加:

  复制

  GlobalThis.getInstance().setContext("context", this.context);

  1.

  在Page中使用:

  复制

  let context: common.UIAbilityContext | undefined = GlobalThis.getInstance().getContext("context");

  1.

  4、实现效果图Demo

  效果图实例包含两个Page, 一个自定义GlobalThis单例对象。

  在ets目录下创建utils目录,并创建GlobalThis.ets文件,代码如上面。

  首页面显示在EntryAbility.ets文件onCreate()方法设置好的属性值,context, 首选项数据库.

  onCreate()方法添加代码如下:

  复制

  import { GlobalThis } from '../utils/GlobalThis';

  import dataPreferences from '@ohos.data.preferences';

  import { BusinessError } from '@ohos.base';

  const PREFERENCES_NAME = 'nutsusPreferences';

  const KEY_APP_PRIVACY = 'nutsusKey';

  export default class EntryAbility extends UIAbility {

  export default class EntryAbility extends UIAbility {

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {

  // 缓存context

  GlobalThis.getInstance().setContext("context", this.context);

  // 缓存作者属性值

  GlobalThis.getInstance().setAttribute("author", "狼哥");

  // 缓存组织属性值

  GlobalThis.getInstance().setAttribute("org", "坚果派");

  let preferences: Promise = dataPreferences.getPreferences(this.context, PREFERENCES_NAME);

  // 缓存首选项Promise

  GlobalThis.getInstance().setPreferences("preferences", preferences);

  preferences.then((result: dataPreferences.Preferences) => {

  // 存储文本到nutsusKey主键

  result.putSync(KEY_APP_PRIVACY, "坚果派由坚果创建,团队拥有8个华为HDE,3个HSD,以及若干其他领域的三十余位万粉博主运营。");

  console.log('xxx')

  }).catch((err: BusinessError) => {

  console.error('xx put the preferences failed, err: ' + err);

  });

  }

  }

  首页面使用GlobalThis对象,代码如下:

  导入构建GlobalThis单例对象

  复制

  import { GlobalThis } from '../utils/GlobalThis';

  import dataPreferences from '@ohos.data.preferences';

  import { BusinessError } from '@ohos.base';

  import router from '@ohos.router';

  import common from '@ohos.app.ability.common';

  const KEY_APP_PRIVACY = 'nutsusKey';

  复制

  首页面显示函数时,从GlobalThis对象获取数据

  1.

  复制

  onPageShow() {

  this.author = GlobalThis.getInstance().getAttribute("author");

  this.org = GlobalThis.getInstance().getAttribute("org");

  let context: common.UIAbilityContext | undefined = GlobalThis.getInstance().getContext("context");

  if (context != undefined) {

  this.label = context.abilityInfo.bundleName;

  }

  let preferences: Promise | undefined = GlobalThis.getInstance().getPreferences("preferences")

  if (preferences != undefined) {

  preferences.then((result: dataPreferences.Preferences) => {

  result.get(KEY_APP_PRIVACY, "").then((data) => {

  this.message = String(data);

  })

  }).catch((err: BusinessError) => {

  console.error('xx get the preferences failed, err: ' + err);

  })

  }

  }

  复制

  首页面布局代码如下:

  1.

  复制

  Column({space: 50}) {

  Text(`[${this.org}] ${this.author}`)

  .fontSize(50)

  .fontWeight(FontWeight.Bold)

  Divider()

  Text(`${this.message}`)

  .fontSize(20)

  Button('跳转下一页')

  .width('80%')

  .height(50)

  .onClick(() => {

  router.pushUrl({

  url: 'pages/Second'

  })

  })

  Text(`获取Context的bundleName: ${this.label}`)

  .width('100%')

  .margin({top: 50})

  }

  .width('100%')

  .height('100%')

  .padding(20)

  第二页面使用GlobalThis对象,代码如下:

  导入构建GlobalThis单例对象

  复制

  import { GlobalThis } from '../utils/GlobalThis';

  import dataPreferences from '@ohos.data.preferences';

  import { BusinessError } from '@ohos.base';

  import router from '@ohos.router';

  import promptAction from '@ohos.promptAction';

  const KEY_APP_PRIVACY = 'nutsusKey';

  复制

  第二页面页面加载前函数时,从GlobalThis获取数据

  1.

  复制

  @State author: string | undefined = GlobalThis.getInstance().getAttribute("author");

  @State org: string | undefined = GlobalThis.getInstance().getAttribute("org");

  @State message: string = "";

  private preferences: Promise | undefined = GlobalThis.getInstance().getPreferences("preferences")

  aboutToAppear() {

  if (this.preferences != undefined) {

  this.preferences.then((result: dataPreferences.Preferences) => {

  result.get(KEY_APP_PRIVACY, "").then((data) => {

  this.message = String(data);

  })

  }).catch((err: BusinessError) => {

  console.error('xx get the preferences failed, err: ' + err);

  })

  }

  }

  复制

  第二页面修改数据代码如下:

  1.

  复制

  onChange() {

  if (this.author != undefined) {

  GlobalThis.getInstance().setAttribute("author", this.author);

  }

  if (this.preferences != undefined) {

  this.preferences.then((result: dataPreferences.Preferences) => {

  result.putSync(KEY_APP_PRIVACY, this.message)

  }).catch((err: BusinessError) => {

  console.error('xx set the preferences failed, err: ' + err);

  })

  }

  promptAction.showToast({

  message: '修改成功^_^',

  duration: 2000

  });

  }

  复制

  第二页面布局代码如下:

  1.

  复制

  Column({space: 50}) {

  TextInput({text: this.author})

  .onChange((value) => {

  this.author = value;

  })

  Text(`[${this.org}]`)

  .fontSize(50)

  .fontWeight(FontWeight.Bold)

  TextArea({text: this.message})

  .onChange((value) => {

  this.message = value;

  })

  Row({space: 50}) {

  Button('返回')

  .width(100)

  .onClick(() => {

  router.back();

  })

  Button('修改')

  .width(100)

  .onClick(() => {

  this.onChange()

  })

  }

  .width('100%')

  .justifyContent(FlexAlign.Center)

  }

  .width('100%')

  .height('100%')

  .padding(20)

  5、总结

  虽然在OpenHarmony 4.0 Release,对应API能力级别为API 10 Release后不能直接使用globalThis全局对象,但通过自定义单例对象后,还是很方便实现属性/方法绑定,在UIAbility和Page之间、UIAbility和UIAbility之间、UIAbility和ExtensionAbility之间都可以使用自构建GlobalThis单例对象上绑定属性/方法,可以实现之间的数据同步。

dawei

【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

您错过了