如何实现桌面快捷方式【2】--ArkTS开发

前言

我们一直会把卡片作为一种app的快捷入口来使用,但是我认为实际上这是错误的。因为卡片应当是用来做简单交互的,交互后如有需要再进入app对应的页面,而不是把"引导用户进入app"作为主要目的。大家其实一直都忽略了一种入口方式,即快捷入口。今天我用了一个下午为我的"真律法律咨询平台"app新增了这个功能,中间踩了不少的坑......为了方便大家的开发,我来分享一下具体的开发流程

示例

当长按App的图标时,除了基本的"添加应用锁"和"卸载"外,还可以弹出其他的快捷入口,更厉害的是,这几个快捷入口,是可以拖出来的,会形成一个单独的类似"app"的样式,也可以点击。

3.EntryAbility.ets中的代码修改

这里其实分为两个部分,对应两种场景。

(1) 当应用在打开的情况下

当你打开了一个app,然后将其放到后台后,即应用还活着的情况下,这时候走的是onNewWant,官方的代码是这么写的:

javascript 复制代码
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  // Receive the parameters passed by UIAbility from the caller
  this.goToSpecifyPage(want);


  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onNewWant');
}

goToSpecifyPage(want: Want) {
  let shortCutKey = want.parameters?.shortCutKey;


  if (this.uiContext && shortCutKey && shortCutKey === 'CompanyPage') {
    let router: Router = this.uiContext.getRouter();
    this.uiContext.getRouter().pushUrl({
      url: 'pages/GoCompany'
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'testTag', `Failed to push url. Code is ${err.code},message is ${err.message}`);
    });
  }
  if (this.uiContext && shortCutKey && shortCutKey === 'HousePage') {
    let router: Router = this.uiContext.getRouter();
    this.uiContext.getRouter().pushUrl({
      url: 'pages/GoHouse'
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'testTag', `Failed to push url. Code is ${err.code},message is ${err.message}`);
    });
  }
}

这里的shortCutKey就是我上一篇文章里所说的parameters,所以你可以看到,这里实际上是通过判断parameters的值,来确定你要跳转到哪个页面的,而且官方的写法中,用的是router来做页面跳转,那么也可以带一些参数过去。但是问题是,我不懂为什么要写this.uiContext......这个是我不太理解的(如果有了解的可以在评论区指点一下),我开发的时候把this.uiContext去掉了,不去判断这个是否为true,并且跳转到时候直接用了router.pushUrl。 但是,但是,又要说到这里的问题了,因为是pushUrl,那么,当你通过快捷入口打开A页面后,回到桌面,再通过快捷入口打开A页面......那么A页面点击返回,又是A页面......所以这个问题是要考虑的

(2) 当应用关闭的情况下

如果当前App本身就是关闭状态,那么当你点击快捷入口的时候,他实际上是去启动App,那么走的应该是onWindowStageCreate而不是onNewWant,这段代码在官方的文档里是没有的,只能看官方的示例代码

typescript 复制代码
  funcAbilityWant: Want | undefined = undefined;
  uiContext: UIContext | undefined = undefined;
  
 onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    let path: string | undefined = 'page/Index';
    if (this.funcAbilityWant?.parameters?.shortCutKey === 'CompanyPage') {
      path = 'pages/GoCompany';
    }
    if (this.funcAbilityWant?.parameters?.shortCutKey === 'HousePage') {
      path = 'pages/GoHouse';
    }

    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');

      let windowClass: window.Window;
      windowStage.getMainWindow((err, data) => {
        if (err.code) {
          hilog.error(0x0000, 'testTag',
            `Failed to obtain the main window. Code is ${err.code},message is ${err.message}`);
        }
        windowClass = data;
        this.uiContext = windowClass.getUIContext();
      })
    });
  }

这段代码的问题在于this.funcAbilityWant上,这玩意儿一直都是undefined啊......解决的办法就是在onCreate里给他赋值 代码如下:

php 复制代码
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.funcAbilityWant = want;
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

另外,windowStage.loadContent这里,官方代码这里明显是错误的,这么写相当于固定了还是pages/Index啊,所以这里的"pages/Index"要改成path

OK,完工

相关推荐
用户938515635071 小时前
从零构建《天龙八部》知识库:EPUB 加载→文本分割→向量嵌入→Milvus 存储→RAG 问答,一条链路打通
javascript·人工智能·全栈
柚yuzumi3 小时前
变量明明定义了,为什么访问不到?深入理解 JavaScript 作用域
javascript
何时梦醒3 小时前
React + TypeScript + Vite 实战:从零构建 Color Picker 应用
前端·javascript·架构
Highcharts.js3 小时前
基于Highcharts开发的OHLC 股票K线图完全指南
javascript·信息可视化·echarts·数据可视化·highcharts·ohlc
默_笙3 小时前
😋 我让 DeepSeek-R1 在浏览器本地跑了起来,后端同事说"你认真的?"
前端·javascript
JasonMa1534 小时前
从零搭建一个微信小程序活动管理平台(一):架构设计与技术选型
javascript·微信小程序·node.js
古韵5 小时前
你的 uni-app 分页,一个 hook 搞定竞态和加载
javascript·vue.js·uni-app
成都渲染101云渲染66667 小时前
Blender渲染时,纯CPU渲染的设置教程
前端·javascript·blender
多加点辣也没关系7 小时前
JavaScript|第30章:事件机制
开发语言·javascript
YHHLAI8 小时前
Vue 3 流式输出实战:从零掌握 LLM Streaming 与 SSE 协议
前端·javascript·vue.js