目录

Home Screen Icon

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.shortcut"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import shortcut from '@system.shortcut'

Or

var shortcut = require("@system.shortcut")

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

shortcut.hasInstalled(OBJECT)

Checks whether a home screen icon has been created for a quick app.

shortcut.install(OBJECT)

Creates a home screen icon for a quick app.

shortcut.hasWidget(OBJECT)

Queries whether a widget of Quick App Center has been created on the home screen.

shortcut.addWidget(OBJECT)

Adds a widget of Quick App Center to the home screen.

[h2]shortcut.hasInstalled(OBJECT)

Description

Checks whether a home screen icon has been created for a quick app.

OBJECT parameters

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

  • true: A home screen icon has been created.
  • false: No home screen icon has been created.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Sample code

shortcut.hasInstalled({
    success: function (ret) {
        console.log('hasInstalled success ret---' + ret);
        if (ret) {
            // A home screen icon has been created.
        } else {
            // No home screen icon has been created.
        }
    }.bind(this),
    fail: function (erromsg, errocode) {
        console.log('hasInstalled fail ret---' + erromsg);
    }.bind(this),
    complete: function () {
    }
})

[h2]shortcut.install(OBJECT)

Description

Creates a home screen icon for a quick app.

Pay attention to the following points when calling this API:

OBJECT parameters

Parameter

Type

Mandatory

Description

message (1030+)

string

No

Prompt information displayed on the pop-up, which explains to users why they need to create a home screen icon.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

201

Failed to obtain the permission for creating a home screen icon for a quick game because the user rejected the request.

Sample code

shortcut.install({
    message: 'Adds the shortcut to the desktop for convenient use next time.',
    success: function (ret) {
        console.log('handling createShortCut success');
    },
    fail: function (erromsg, errocode) {
        console.log('handling createShortCut fail');
    }.bind(this),
})

[h2]shortcut.hasWidget(OBJECT) (1101+)

Description

Queries whether a widget of Quick App Center has been created on the home screen.

OBJECT parameters

Parameter

Type

Mandatory

Description

type

string

Yes

Widget type, which can be:

  • historyWidget: recently used widget
  • customWidget: customized widget

success

function

No

Callback upon success.

  • true: created
  • false: not created

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Sample code

shortcut.hasWidget({
    type: 'customWidget',
    success: function (ret) {
        console.log('hasWidget success ret---' + ret);
        if(ret) {
            // The custom widget has been created.
        } else {
            // The custom widget has not been created.
        }
    }.bind(this),
    fail: function (erromsg, errocode) {
        console.log('hasWidget fail ret---' + erromsg);
    }.bind(this),
    complete: function () {
    }
})

[h2]shortcut.addWidget(OBJECT) (1101+)

Description

Adds a widget of Quick App Center to the home screen.

Some devices, for example, whose versions are earlier than 8.0, do not support this API. If you call this API on these devices, fail will be returned.

OBJECT parameters

Parameter

Type

Mandatory

Description

type

string

Yes

Widget type, which can be:

  • historyWidget: recently used widget
  • customWidget (default): customized widget

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

2001

The device does not support adding the widget of Quick App Center.

Sample code

shortcut.addWidget({
    type:'customWidget',
    success: function (ret) {
           console.log('addWidget success ret---' + ret);
           if(ret) {
             // The custom widget is added successfully.
           } else {
             // Failed to add the custom widget.
           }
     }.bind(this),
     fail: function (erromsg, errocode) {
         console.log('addWidget fail ret---' + erromsg);
     }.bind(this),
     complete: function () {
     }
})

Demo

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container ">
    <div class="mlr-container">
      <input onclick="hasinstallShortcut" type="button" value="Check Shortcut Creation" class="btn-blue" />
      </div>
      <div class="mlr-container">
      <input onclick="setShortcut" type="button" value="Create Shortcut" class="btn-blue" />
    </div>
  </div>
</template>

<style lang="sass">
.container {
   flex-direction: column;
   justify-content: center;
   align-items: center;
}
.mlr-container {
  margin-top: 50px;
  margin-right: 30px;
  margin-left: 30px;
  flex-direction: column;
}
.btn-blue {
    background-color: #0faeff;
    color: #ffffff; 
    width:600px;
    height: 100px;
    font-size: 50px;
    border-radius:15px;
  }
</style>

<script>
  import shortcut from '@system.shortcut';
  import prompt from '@system.prompt';
  module.exports = {
    onInit: function () {
      this.$page.setTitleBar({ text: 'shortcut' });
    },
    setShortcut() {
      shortcut.install({
        message: 'Adds the shortcut to the desktop for convenient use next time.',
        success: function (ret) {
          console.log('handling createShortCut success');
        },
        fail: function (erromsg, errocode) {
          prompt.showToast({
            message: erromsg,
            duration: 2000,
            gravity: 'bottom'
          })
        }
      })
    },
    hasinstallShortcut() {
          shortcut.hasInstalled({
              success: function (ret) {
                  console.log('hasInstalled success ret---' + ret);
                  if (ret) {
            // A home screen icon has been created.
                  } else {
            // No home screen icon has been created.
                  }
              }.bind(this),
              fail: function (erromsg, errocode) {
                  console.log('hasInstalled fail ret---' + erromsg);
              }.bind(this),
              complete: function () {
              }
          })
      }
  }
</script>

The following figure shows the final effect.

Version Change History

Version

Date

Description

1101

2022-09-10

  • Added the shortcut.hasWidget(OBJECT) API to query widgets.
  • Added the shortcut.addWidget(OBJECT) API to add widgets.

1030

2018-10-31

Added the message parameter to the shortcut.install API, allowing you to configure a more targeted prompt when a user creates a home screen icon.

1020

2018-06-20

Added the systemPromptEnabled attribute to determine whether the system prompts users to create a home screen icon.

Links

[h2]Cases

Creating Home Screen Icon Quickly