Read the original article:🧭 Using Want and AbilityKit for Application Redirection in HarmonyOS Next
🚩Introduction
Application redirection is a widely used approach in the mobile application industry. It allows an application to redirect its users to a different system or a third-party application. This functionality helps developers guide users from one app to another to complete specific tasks — such as opening the dialer, sending feedback via email, navigating to a location, or launching a web page.
In HarmonyOS Next, application redirection is facilitated using AbilityKit, particularly through tools like Want and openLink. This article will provide practical examples of redirection implemented with ArkTS, highlight the differences between various redirection methods, and explain how to implement these patterns to enhance your app’s interactivity and user satisfaction.
In HarmonyOS Next, there are two main methods for app redirection. These capabilities are primarily supported through two core methods of application redirection:
1. Starting a Specific Application
This involves launching another app directly from your app. There are two ways to do this:
App Linking (Recommended): Uses a verified HTTPS link
- to open a specific page in another app. If the target app isn’t installed, it can gracefully fall back to a web page. This is the secure and preferred method in HarmonyOS Next.
- Explicit Want (Deprecated): Starts another app by directly specifying its bundleName and abilityName. While it still works, this method is discouraged in API version 12+ due to security concerns. Should be used only when redirecting to system apps or within the same signed application group.
2. Starting an App of a Given Type
Instead of targeting a specific app, you declare an intent (such as sending an email or viewing a location), and the system displays a list of compatible apps. This gives users the flexibility to choose how they want to complete the action.
Now that the two primary methods of application redirection in HarmonyOS Next have been outlined, we can move on to their practical implementation using AbilityKit.
⚙️ Implementation
In the following section, we’ll demonstrate real-world examples, helping clarify when and how to use startAbility, openLink, or startAbilityByType based on the specific target and context.
We will demonstrate redirections under 3 main categories:
- Redirecting to System Applications
- Redirecting to Specific Third-Party Applications
- Redirecting to Applications of a Specific Type
Now, let’s start implementing them.
📂 Category 1: Redirecting to System Applications
This category includes links to built-in system features or pre-installed apps. It is further divided into two parts:
1.1 Redirecting to System Settings Pages
Utilize this approach when you need to direct the user to specific system configuration areas, such as Bluetooth, Wi-Fi, or the general settings page. These actions cannot be performed through standard links and require the use of startAbility()
with an appropriate Want
. Below, you will find an example of how to redirect to the Bluetooth screen in the settings.
For specific permissions, use requestPermissionOnSetting if the user initially rejects it, to open the necessary permission screen of the application. Additionally, use openNotificationSetting for notifications.
this.context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let want: Want = {
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'bluetooth_entry'
};
this.context.startAbility(want)
.then(() => {
hilog.info(0x0000, 'Testlog','Bluetooth settings opened');
})
.catch((err: BusinessError) => {
hilog.error(0x0000, 'Testlog', `Failed to open Bluetooth settings. Code: ${err.code}, message: ${err.message}`);
});
1.2 Redirecting to Default System Applications
Some system applications come with predefined methods, such as call.makeCall
and calendar.addEvent
. Additionally, there are components like the Barcode Scanning Picker, Card Recognition Picker, Document Scanning Picker, Contact Selection Picker, Media Selection Picker, and File Selection Picker. These components can be utilized to guide users in completing necessary actions. Here is an example that redirects the user to the phone dial screen.
call.makeCall("901234567890").then(() => {
hilog.info(0x0000, 'Testlog',`makeCall success`);
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'Testlog',`makeCall fail, promise: err->${JSON.stringify(err)}`);
});
🧩 Category 2: Launching a Specific Third-Party Application
In certain scenarios, you may wish to launch a specific third-party application, such as WeChat, a banking app, or a partner application, directly from your app.
This can be achieved through deep linking, which enables the system to identify and open the correct application using a custom URL scheme. To implement this, you first need to add the app’s special link settings to the query scheme in the module.json5 file.
{
"module": {
"querySchemes": [
"weixin", // WeChat
]
}
....
}
Here we are redirecting to the home screen of WeChat.
You can find related query schemes for applications in the official website documentation.
let link = 'weixin://home'; // Typical WeChat scheme
context.openLink(link, { appLinkingOnly: false })
.then(() => {
hilog.info(0x0000, 'Testlog', 'openlink success.');
})
.catch((error: BusinessError) => {
hilog.error(0x0000, 'Testlog',`openlink failed. error: ${JSON.stringify(error)}`);
});
🎯 Category 3: Starting an Application of a Specific Type
This category involves launching any application that performs a specific type of task or handles particular content, without focusing on a specific app. Instead of relying on a fixed bundle name, you can define an action or scene type that allows the system to choose the most suitable app for the task. This approach is perfect for creating flexible and future-proof experiences.
Below, you will find the use case developed for the money transfer example:
this.context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let wantParam: Record<string, Object> = {
'sceneType': 1,
'bankCardNo': encodeURI('1234-5678-9876-5432')
};
let abilityStartCallback: common.AbilityStartCallback = {
onError: (code: number, name: string, message: string) => {
hilog.info(0x0000, 'Testlog',`onError code ${code} name: ${name} message: ${message}`);
},
onResult: (result) => {
hilog.info(0x0000, 'Testlog',`onResult result: ${JSON.stringify(result)}`);
}
};
this.context.startAbilityByType(
"finance",
wantParam,
abilityStartCallback,
(err) => {
if (err) {
hilog.error(0x0000, 'Testlog',`startAbilityByType fail, err: ${JSON.stringify(err)}`);
} else {
hilog.info(0x0000, 'Testlog',`success`);
}
}
);
🏁 Conclusion
Navigating application launching and redirection in HarmonyOS Next may seem complex at first, but understanding the intent-driven architecture makes it both powerful and flexible. Whether you’re opening a system settings page, launching a known third-party app, or delegating actions to any capable app through type-based invocation, AbilityKit provides the right tools.
By categorizing redirection strategies and applying the correct APIs (startAbility, openLink, startAbilityByType), you can create seamless user experiences while respecting system policies and maintaining compatibility across devices.
As HarmonyOS Next continues to evolve, mastering these foundational patterns will help you build more connected, fluid, and user-friendly applications.
Top comments (0)