We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp ReferencesQuick AppAPISystem CapabilitiesBattery Level

Battery Level

API Declaration

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

Collapse
Word wrap
Dark theme
Copy code
  1. {"name": "system.battery"}

Module Import

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

Collapse
Word wrap
Dark theme
Copy code
  1. import battery from '@system.battery'

Or

Collapse
Word wrap
Dark theme
Copy code
  1. var battery = require("@system.battery")

Application Scope

Expand

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

Expand

API

Description

battery.getStatus(OBJECT)

Obtains the battery level of the current device.

battery.getStatus(OBJECT)

Description

Obtains the battery level of the current device.

OBJECT parameters

Expand

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned by success

Expand

Parameter

Type

Description

charging

boolean

Indicates whether the battery is being charged.

level

float

Current battery level, ranging from 0.0 to 1.0.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. battery.getStatus({
  2. success:function(ret){console.log("handling success");}
  3. })

Demo

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <!-- Only one root node is allowed in template. -->
  3. <div class="container">
  4. <div class="info-container ">
  5. <div class="info-item">
  6. <text class="title">level</text>
  7. <text class="result">{{batterInfoGeted ? batteryNum +'%' : 'please get current level'}}</text>
  8. </div>
  9. <div class="info-item">
  10. <text class="title"> charging</text>
  11. <text class="result">{{batterInfoGeted ? isCharging : 'please get charging status'}}</text>
  12. </div>
  13. </div>
  14. <div class="mlr-container row-center mt-item">
  15. <input type="button" class="btn-blue" value="Get Battery Info" onclick="getBatteryInfo" />
  16. </div>
  17. </div>
  18. </template>
  19. <style lang="sass">
  20. .container {
  21. flex-direction: column;
  22. padding-bottom: 50px;
  23. }
  24. .mt-item {
  25. margin-top: 40px;
  26. }
  27. .info-container {
  28. margin-left: 32px;
  29. margin-right: 32px;
  30. padding-right: 20px;
  31. padding-left: 20px;
  32. background-color: #fff;
  33. border-radius: 32px;
  34. flex-direction: column;
  35. }
  36. .info-item {
  37. height: 120px;
  38. border-bottom-color: rgba(0, 0, 0, 0.05);;
  39. }
  40. .title {
  41. flex-shrink: 0;
  42. font-size: 32px;
  43. color: rgba(0, 0, 0, 0.9);
  44. }
  45. .result {
  46. width: 100%;
  47. justify-content: flex-end;
  48. text-align: right;
  49. color: #999;
  50. }
  51. .mlr-container {
  52. margin-right:32px;
  53. margin-left:32px;
  54. }
  55. .mt-btn {
  56. margin-top: 32px;
  57. }
  58. .btn-blue {
  59. background-color: #0faeff;
  60. width: 100%;
  61. height: 80px;
  62. border-radius: 40px;
  63. color: #fff;
  64. font-size: 32px;
  65. font-weight: 500;
  66. }
  67. </style>
  68. <script>
  69. import battery from '@system.battery';
  70. import prompt from '@system.prompt';
  71. module.exports = {
  72. public: {
  73. batteryNum: 0,
  74. isCharging: false,
  75. batterInfoGeted: false,
  76. },
  77. onInit: function () {
  78. this.$page.setTitleBar({ text: 'battery level'});
  79. },
  80. getBatteryInfo() {
  81. let that = this;
  82. battery.getStatus({
  83. success: function (ret) {
  84. that.batterInfoGeted = true;
  85. that.batteryNum = ret.level * 100;
  86. that.isCharging = ret.charging;
  87. },
  88. fail: function () {
  89. prompt.showToast({
  90. message: 'fail',
  91. duration: 2000,
  92. gravity: 'bottom'
  93. })
  94. }
  95. })
  96. }
  97. }
  98. </script>

The following figure shows the final effect.