Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.alarm"} Add the following configuration to the script element on the page where the API will be called:
import alarm from '@system.alarm'
Or
var alarm = require("@system.alarm") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, and Vision products |
Applicable regions | Global |
API | Description |
|---|---|
Obtains information about a service provider. If the alarm function is not supported, an empty string is returned. | |
Sets an alarm. A pop-up will be displayed, asking the user for confirmation. This API is called only when the user agrees to set the alarm. |
Description
Obtains information about a service provider. If no provider is available, am empty string is returned.
Sample code
var provider = alarm.getProvider(); // huawei
Description
Sets an alarm. A pop-up will be displayed, asking the user for confirmation. This API is called only when the user agrees to set the alarm.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
hour | number | Yes | Alarm time (hours). The value ranges from 0 to 23. |
minute | number | Yes | Alarm time (minutes). The value ranges from 0 to 59. |
message | string | No | Alarm name, which can contain up to 200 characters. |
vibrate | boolean | No | Indicates whether to enable vibration. The default value is true. |
days | array<number> | No | Repeat cycle of the alarm. The alarm does not repeat by default.
|
ringtone | string | No | Ringtone. The default ringtone varies with the system. The file path is that of a data file path or an in-app resource. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion, regardless of whether the API call is successful or fails. |
Sample code
alarm.setAlarm({
hour: 14,
minute: 0,
message:'Huawei test alarm',
vibrate: false,
days: [0,1,2,3,4,5],
ringtone: '/Common/audio/20181211_155437.m4a',
success: function (res) {
console.log('handling success')
},
fail: function (erromsg, errocode) {
console.log('handling failed');
prompt.showToast({
message: 'set failed,msg:' + erromsg + ', code = ' + errocode
})
},
complete: function() {
console.log('handling complete' )
}
}) <template>
<div class="doc-page">
<div class="item-container">
<div class="item-content line">
<text class="title">service provider: </text>
<text>{{provider}}</text>
</div>
<div class="item-content">
<text class="title">Time: {{hour}} hours</text>
<slider class="slider" min="0" max="23" onchange="setAlarmHour"></slider>
</div>
<div class="item-content">
<text class="title">Time: {{minute}} minute</text>
<slider class="slider" min="0" max="59" onchange="setAlarmMinute"></slider>
</div>
<div class="item-content">
<text class="title">Alarm name</text>
<input class="message" onchange="setAlarmMsg" value="{{message}}"></input>
</div>
<div class="item-content line">
<text class="title">vibrate</text>
<switch class="switch" checked="true" onchange="switchAlarmVibrate"></switch>
</div>
<div class="item-content line">
<text class="title">days </text>
<switch class="switch" checked="false" onchange="switchAlarmRepeat"></switch>
</div>
<div class="item-container">
<input type="button" class="btn-blue" onclick="pickVideo" value="select a Ringtone"/>
</div>
<div class="item-container">
<input type="button" class="btn-blue" onclick="setAlarm" value="Set an Alarm"/>
</div>
</div>
</div>
</template>
<style>
.doc-page{
flex: 1;
flex-direction: column;
}
.item-container {
flex-direction: column;
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
}
.item-content {
flex-direction: column;
width: 100%;
padding: 10px;
}
.item-container .line {
flex-direction: row;
}
.item-content .title {
margin: 30px 0;
height: 30px;
}
.message {
width: 100%;
height: 60px;
border: 1px solid #3ec26a;
border-left-width: 0;
border-right-width: 0;
}
.switch {
margin-right: 30px;
}
.btn-blue {
background-color: #0faeff;
width: 100%;
height: 80px;
border-radius: 40px;
color: #fff;
font-size: 32px;
font-weight: 500;
}
</style>
<script>
import alarm from '@system.alarm'
import media from '@system.media'
import prompt from '@system.prompt'
export default {
private: {
componentName: 'Alarm',
hour: 0,
minute: 0,
message: 'quickapp alarm',
vibrate: true,
repeat: false,
days: [],
provider: '',
ringtone: ''
},
onInit () {
this.$page.setTitleBar({text: 'alarm'})
this.provider = alarm.getProvider()
},
setAlarm () {
const day = [new Date().getDay() - 1]
this.days = this.repeat ? [0, 1, 2, 3, 4, 5, 6] : day
alarm.setAlarm({
hour: this.hour,
minute: this.minute,
message: this.message,
vibrate: this.vibrate,
ringtone: this.ringtone,
days: this.days,
success() {
prompt.showToast({
message: 'set alarm success!'
})
},
fail(data, code) {
console.log('set alarm failed: code' + code.code)
},
})
},
pickVideo () {
const _this = this
media.pickFile({
success(data) {
prompt.showToast({
message: 'data.uri:' + data.uri
})
_this.ringtone = data.uri
},
fail(data, code) {
prompt.showToast({
message: 'select ringtone:' + code
})
}
})
},
setAlarmHour (e) {
this.hour = e.progress
},
setAlarmMinute (e) {
this.minute = e.progress
},
setAlarmMsg (e) {
this.message = e.value
},
switchAlarmVibrate (e) {
this.vibrate = e.checked
},
switchAlarmRepeat (e) {
this.repeat = e.checked
}
}
</script> The following figure shows the final effect.
