场景介绍
- 元服务卡片加桌
您可调用应用市场服务提供的元服务加桌loadService接口,加载元服务卡片加桌页面,用户点击“添加至桌面”按钮,将元服务卡片添加至桌面。
- 应用详情页展示
- 您可调用应用市场服务提供的loadProduct接口,直接加载应用市场的应用详情页面,用户可以在页面内点击“安装”按钮完成应用的下载安装。
- 您可使用DeepLink链接的方式拉起应用市场应用详情页,通过拼接应用市场DeepLink链接,在应用中调用或网页中点击DeepLink链接拉起应用详情页,用户可以在页面内点击“安装”按钮完成应用的下载安装。
- 您可使用App Linking链接的方式拉起应用市场应用详情页,通过拼接应用市场App Linking链接,在应用中调用或网页中点击App Linking链接拉起应用详情页,用户可以在页面内点击“安装”按钮完成应用的下载安装。
图1 元服务加桌页示例 | 图2 应用详情页示例 |
业务流程
- 用户使用元服务卡片加桌页或者打开应用详情页功能。
- 应用调用Store Kit的loadService或者loadProduct接口。
- Store Kit API获取应用传入的信息,生成展示页面。
- 展示生成的页面给用户使用。
接口说明
应用市场推荐场景提供loadService,loadProduct两个接口,具体API说明详见接口文档。
接口名 | 描述 |
---|---|
loadService(context: common.UIAbilityContext, want: Want, callback?: ServiceViewCallback): void | 加载元服务加桌页面接口。 |
loadProduct(context: common.UIAbilityContext, want: Want, callback?: ProductViewCallback): void | 加载应用详情页面接口。 |
开发步骤
元服务卡片加桌
- 导入productViewManager模块及相关公共模块。
- import { productViewManager } from '@kit.StoreKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import type { common, Want } from '@kit.AbilityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- 构造元服务卡片参数。
- 第一个参数是获取当前Page页面的UIAbilityContext。
- 第二个参数是构造一个Want类型的对象,在属性中传入要加载的元服务的加桌链接。
- 第三个参数是可选参数对象,可以传入两个属性,onError:回调函数,回调返回的信息为元服务详情页加载失败的错误信息。onReceive:回调函数,接收元服务加桌结果信息。
- const uiContext = getContext(this) as common.UIAbilityContext
- const wantParam: Want = {
- // 此处填入要加载的元服务的加桌链接
- uri: 'xxx'
- }
- const callback: productViewManager.ServiceViewCallback = {
- onReceive: (data: productViewManager.ServiceViewReceiveData) => {
- hilog.info(0x0001, 'TAG', `loadService onReceive.result is ${data.result}, msg is ${data.msg}`);
- },
- onError: (error: BusinessError) => {
- hilog.error(0x0001, 'TAG', `loadService onError.code is ${error.code}, message is ${error.message}`);
- }
- }
- 调用loadService方法,将步骤2中构造的参数依次传入接口中。
- // 调用接口,加载元服务加桌页面
- productViewManager.loadService(uiContext, wantParam, callback);
应用详情页展示
- 方式一:loadProduct接口调用
- 导入productViewManager模块及相关公共模块。
- import { productViewManager } from '@kit.StoreKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import type { common, Want } from '@kit.AbilityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- 构造应用详情页参数。
- 第一个参数是获取当前Page页面的UIAbilityContext。
- 第二个参数是构造一个Want类型的对象,在属性中传入要加载的应用包名。
- 第三个参数是可选参数对象,可以使用其onError属性来接收应用详情页加载失败时的返回的错误信息。
- const uiContext = getContext(this) as common.UIAbilityContext
- const wantParam: Want = {
- parameters: {
- // 此处填入要加载的应用包名,例如: bundleName: 'com.huawei.hmsapp.books'
- bundleName: 'com.xxx'
- }
- }
- const callback: productViewManager.ProductViewCallback = {
- onError: (error: BusinessError) => {
- hilog.error(0x0001, 'TAG', `loadProduct onError.code is ${error.code}, message is ${error.message}`);
- }
- }
- 调用loadProduct方法,将步骤2中构造的参数依次传入接口中。
- // 调用接口,拉起应用详情页
- productViewManager.loadProduct(uiContext, wantParam, callback);
- 方式二:DeepLink方式
构造拼接bundleName的DeepLink链接,其中bundleName为需要打开的应用包名,其格式为:
- store://appgallery.huawei.com/app/detail?id= + bundleName
在应用中调用context.startAbility()方法,拉起应用市场应用详情页:
- import { BusinessError } from '@kit.BasicServicesKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import type { common, Want } from '@kit.AbilityKit';
- // 拉起应用市场对应的应用详情页面
- function startAppGalleryDetailAbility(context: common.UIAbilityContext, bundleName: string): void {
- let want: Want = {
- action: 'ohos.want.action.appdetail', //隐式指定action为ohos.want.action.appdetail
- uri: 'store://appgallery.huawei.com/app/detail?id=' + bundleName, // bundleName为需要打开应用详情的应用包名
- };
- context.startAbility(want).then(() => {
- hilog.info(0x0001, 'TAG', "Succeeded in starting Ability successfully.")
- }).catch((error: BusinessError) => {
- hilog.error(0x0001, 'TAG', `Failed to startAbility.Code: ${error.code}, message is ${error.message}`);
- });
- }
- @Entry
- @Component
- struct StartAppGalleryDetailAbilityView {
- @State message: string = '拉起应用市场详情页';
- build() {
- Row() {
- Column() {
- Button(this.message)
- .fontSize(24)
- .fontWeight(FontWeight.Bold)
- .onClick(() => {
- const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
- // 按实际需求获取应用的bundleName,例如bundleName: 'com.huawei.hmsapp.books'
- const bundleName = 'xxxx';
- startAppGalleryDetailAbility(context, bundleName);
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在网页中打开DeepLink链接拉起应用市场应用详情页:
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- </head>
- <body>
- <div>
- <button type="button" οnclick="openDeepLink()">拉起应用详情页</button>
- </div>
- </body>
- </html>
- <script>
- function openDeepLink() {
- window.open('store://appgallery.huawei.com/app/detail?id=com.xxxx.xxxx')
- }
- </script>
- 方式三:App Linking方式
构造拼接bundleName的App Linking链接,其中bundleName为需要打开的应用包名,其格式为:
- https://appgallery.huawei.com/app/detail?id= + bundleName
在应用中调用openLink()接口拉起App Linking链接:
- import type { common } from '@kit.AbilityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- @Entry
- @Component
- struct Index {
- build() {
- Button('start app linking', { type: ButtonType.Capsule, stateEffect: true })
- .width('87%')
- .height('5%')
- .margin({ bottom: '12vp' })
- .onClick(() => {
- let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
- // 需要拼接不同的应用包名,用以打开不同的应用详情页,例如:bundleName: 'com.huawei.hmsapp.books'
- let bundleName: string = 'xxxx';
- let link: string = 'https://appgallery.huawei.com/app/detail?id=' + bundleName;
- // 以App Linking优先的方式在应用市场打开指定包名的应用详情页
- context.openLink(link, { appLinkingOnly: false })
- .then(() => {
- hilog.info(0x0001, 'TAG', 'openlink success.');
- })
- .catch((error: BusinessError) => {
- hilog.error(0x0001, 'TAG', `openlink failed. Code: ${error.code}, message is ${error.message}`);
- });
- })
- }
- }
在网页中打开App Linking链接:
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>跳转示例</title>
- </head>
- <body>
- <a href='https://appgallery.huawei.com/app/detail?id=bundleName'>AppLinking跳转示例</a>
- </body>
- </html>