如何实现桌面快捷方式【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,完工

相关推荐
wkj0013 小时前
vue中 js-cookie 用法
前端·javascript·vue.js
漠月瑾-西安8 小时前
如何在 React + TypeScript 中实现 JSON 格式化功能
javascript·jst实现json格式化
止观止9 小时前
React响应式组件范式:从类组件到Hooks
javascript·react.js·ecmascript
@大迁世界9 小时前
React 及其生态新闻 — 2025年6月
前端·javascript·react.js·前端框架·ecmascript
LJianK19 小时前
Java和JavaScript的&&和||
java·javascript·python
新酱爱学习10 小时前
前端海报生成的几种方式:从 Canvas 到 Skyline
前端·javascript·微信小程序
爱编程的喵11 小时前
深入理解JavaScript单例模式:从Storage封装到Modal弹窗的实战应用
前端·javascript
布丁052313 小时前
DOM编程实例(不重要,可忽略)
前端·javascript·html
bigyoung13 小时前
babel 自定义plugin中,如何判断一个ast中是否是jsx文件
前端·javascript·babel
草履虫建模14 小时前
Ajax原理、用法与经典代码实例
java·前端·javascript·ajax·intellij-idea