• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Development Guidelines on Clock Apps<a name="EN-US_TOPIC_0000001115417926"></a>
2
3-   [Overview](#section11522349121115)
4-   [Preparations](#section6592121861218)
5-   [How to Develop](#section19901741111312)
6-   [Signing and Packaging](#section10601181101516)
7-   [Running on the Real Device](#section092721731511)
8-   [FAQs](#section1122413460153)
9    -   [hdc\_std Fails to Connect to a Device](#section1922725151614)
10    -   [hdc\_std Fails to Run](#section15657547131615)
11
12
13## Overview<a name="section11522349121115"></a>
14
15This document describes how to quickly set up a development environment \(using the Hi3516D V300 development board\) and develop a clock app running on OpenHarmony. You can click  [here](https://gitee.com/openharmony/app_samples/tree/master/common/Clock)  to obtain the sample code.
16
17The clock app displays the current time, as shown in the following figure.
18
19**Figure  1**  Clock display effect<a name="fig7763172132019"></a>
20
21
22![](figure/clock.png)
23
24## Preparations<a name="section6592121861218"></a>
25
26Download and install DevEco Studio. For details, see the  [HUAWEI DevEco Studio User Guide](../../application-dev/quick-start/deveco-studio-(openharmony)-user-guide.md).
27
28## How to Develop<a name="section19901741111312"></a>
29
30The Clock app displays the current time through a clock face and numbers.
31
32As shown in  [Figure 1 Clock display effect](#fig7763172132019), the UI consists of two parts:
33
34-   Clock face area: displays a dynamic analog clock whose hands rotate accurately.
35-   Digital time area: displays the current time in numerals.
36
37To build such an app, we can create a page that has a flexible layout with two rows vertically arranged. The development procedure is as follows:
38
391.  Add a root component  **<div\>**  to the  **.hml**  file. Note that each  **.hml**  file can contain only one root component. The sample code is as follows:
40
41    ```
42    <div class="container">
43    </div>
44    ```
45
46    **class="container"**  indicates the style used by the component. The  **container**  is a style class defined in the  **index.css**  file.
47
48    ```
49    .container {
50        flex-direction: column;
51        justify-content: center;
52        align-items: center;
53        width: 100%;
54        height: 100%;
55    }
56    ```
57
58    The height and width of the root component  **<div\>**  are set in the style class. Note that the height and width must be explicitly specified \(except for some components, such as  **<text\>**\). Otherwise, the component may fail to display. In the  **container**  style class, the  **flex-direction**  attribute is set to  **column**, which means that child components of  **<div\>**  are vertically arranged from top to bottom for implementing the flexible page layout.
59
602.  Implement clock hand rotation using the  **<stack\>**  component. The  **<stack\>**  component provides a stack container where child components are successively stacked and the latter one overwrites the previous one.
61
62    Add a stack container to the root component. The sample code is as follows:
63
64    ```
65    <div class="container">
66        <stack class="stack">
67            <image src="/common/clock_bg.png" class="clock-bg"></image> <!--Set the clock face image.-->
68            <image src="/common/hour_hand.png" class="clock-hand"
69                   style="transform : rotate({{ hour * 30 + minute / 2 }}deg);"></image> <!--Set the hour hand image. (hour * 30) indicates that the hour hand rotates 30 degrees every hour.  (minute / 2) indicates the rotation degrees per minute.-->
70            <image src="/common/minute_hand.png" class="clock-hand"
71                   style="transform : rotate({{ minute * 6 + second / 10 }}deg);"></image> <!--Set the minute hand image. (minute * 6) indicates that the minute hand rotates 6 degrees every minute. (second / 10) indicates the rotation degrees per second.-->
72            <image src="/common/second_hand.png" class="clock-hand"
73                   style="transform : rotate({{ second * 6 }}deg);"></image> <!--Set the second hand image. (second * 6) indicates that the second hand rotates 6 degrees per second.-->
74       </stack>
75    </div>
76    ```
77
78    **style="transform: rotate\(\{\{ second \* 6 \}\}deg\)**  sets the rotation event of a component.  **transform**  translates, rotates, or scales an image.  **rotate**  rotates an image. You can set an image to rotate around its x-axis or y-axis.
79
80    Set attributes, such as the height, width, and position, of the stack component in the CSS file. The sample code is as follows:
81
82    ```
83    .stack {
84        flex-direction: column;
85        justify-content: center;
86        align-items: center;
87        width: 100%;
88        height: 50%;
89    }
90    ```
91
92    Set attributes, such as the height and width, of the clock-bg component in the CSS file. The sample code is as follows:
93
94    ```
95    .clock-bg {
96        width: 80%;
97        height: 80%;
98        object-fit: scale-down;
99    }
100    ```
101
102    Set attributes, such as the height and width of the hour, minute, and second hands, of the clock-hand component in the CSS file. The sample code is as follows:
103
104    ```
105    .clock-hand {
106        width: 25%;
107        height: 65%;
108        object-fit: contain;
109    }
110    ```
111
112    Add a timer in the  **index.js**  file to update the hour, minute, and second variables in real time so that the time can be automatically updated on the app UI. The sample code is as follows:
113
114    ```
115    export default {
116        timer: undefined,
117        // Define parameters.
118        data: {
119          hour: 0,   // Define hours.
120          minute: 0, // Define minutes.
121          second: 0  // Define seconds.
122        },
123        onInit () {
124            this.updateTime();
125            this.timer = setInterval(this.updateTime, 1000)// Set the timer to 1 second.
126        },
127        updateTime: function () {
128            var nowTime = new Date()
129            this.hour = nowTime.getHours()
130            this.minute = nowTime.getMinutes()
131            this.second = nowTime.getSeconds()
132            if (this.hour < 10) {
133                this.hour = '0' + this.hour
134            }
135            if (this.minute < 10) {
136                this.minute = '0' + this.minute
137            }
138            if (this.second < 10) {
139                this.second = '0' + this.second
140            }
141        },
142    }
143    ```
144
1453.  Display the current time in numerals under the analog clock. Add the text component at the end of the root layout. The following example shows the UI structure:
146
147    ```
148    <text class="digit-clock"> {{ hour }}:{{ minute }}:{{ second }}</text>
149    ```
150
151    class=**"digit-clock"**  sets the height, width, and font size of the component. The sample code is as follows:
152
153    ```
154    .digit-clock {
155        font-size: 58px;
156        width: 100%;
157        margin-top: 0px;
158        text-align: center;
159    }
160    ```
161
1624.  Set the style, animation effect, and dynamic data binding for all components. The complete sample code is as follows:
163    -   **index.hml**
164
165        ```
166        <div class="container">
167            <stack class="stack">
168                <image src="/common/clock_bg.png" class="clock-bg"></image>
169                <image src="/common/hour_hand.png" class="clock-hand"
170                       style="transform : rotate({{ hour * 30 + minute / 2 }}deg);"></image>
171                <image src="/common/minute_hand.png" class="clock-hand"
172                       style="transform : rotate({{ minute * 6 + second / 10 }}deg);"></image>
173                <image src="/common/second_hand.png" class="clock-hand"
174                       style="transform : rotate({{ second * 6 }}deg);"></image>
175            </stack>
176            <text class="digit-clock">{{ hour }}:{{ minute }}:{{ second }}</text>
177        </div>
178        ```
179
180    -   **index.css**
181
182        ```
183        .container {
184            flex-direction: column;
185            justify-content: center;
186            align-items: center;
187            width: 100%;
188            height: 100%;
189        }
190
191        .stack {
192            flex-direction: column;
193            justify-content: center;
194            align-items: center;
195            width: 100%;
196            height: 50%;
197        }
198
199        .digit-clock {
200            font-size: 58px;
201            width: 100%;
202            margin-top: 0px;
203            text-align: center;
204        }
205
206        .clock-bg {
207            width: 80%;
208            height: 80%;
209            object-fit: scale-down;
210        }
211
212        .clock-hand {
213            width: 25%;
214            height: 65%;
215            object-fit: contain;
216        }
217        ```
218
219    -   **index.js**
220
221        A  **.js**  file is used to implement logic interactions of the clock app. The following  **.js**  file implements the function of periodically obtaining the system time.
222
223        ```
224        export default {
225            timer: undefined,
226            data: {
227                hour: 0,
228                minute: 0,
229                second: 0
230            },
231            onInit() {
232                this.updateTime()
233                this.timer = setInterval(this.updateTime, 1000)
234            },
235            updateTime: function () {
236                var nowTime = new Date()
237                this.hour = nowTime.getHours()
238                this.minute = nowTime.getMinutes()
239                this.second = nowTime.getSeconds()
240                if (this.hour < 10) {
241                    this.hour = '0' + this.hour
242                }
243                if (this.minute < 10) {
244                    this.minute = '0' + this.minute
245                }
246                if (this.second < 10) {
247                    this.second = '0' + this.second
248                }
249            },
250            onDestroy() {
251                clearInterval(this.timer);
252            }
253        }
254        ```
255
256
257
258## Signing and Packaging<a name="section10601181101516"></a>
259
260After finishing writing the app code, you need to sign and package the app before running it on a real device. For details, see  [Signing and Packaging Guide](../../application-dev/quick-start/configuring-the-openharmony-app-signature.md).
261
262## Running on the Real Device<a name="section092721731511"></a>
263
264Before you install the app and run it on the development board, install the DevEco Device Tool by following operations provided in  [HUAWEI DevEco Device Tool User Guide](https://device.harmonyos.com/en/docs/ide/user-guides/service_introduction-0000001050166905). Burn OpenHarmony into the development board, and run it on the board. For details about how to build, burn, and run an image, see . After the image is running normally and the system is started properly, perform the following steps to install or uninstall the app:
265
2661.  Obtain the HDC client from the following path:
267
268    ```
269    developtools/hdc_standard/prebuilt/windows/hdc_std.exe
270    ```
271
272    Change the HDC client name to  **hdc.exe**  and add the path above to the system environment variable  **path**.
273
2742.  Open the  **cmd**  window, and run the following commands to push the HAP file to the device directory, and install it:
275
276    ```
277    hdc smode
278    hdc target mount
279    hdc file send clock.hap /data/clock.hap
280    hdc shell chmod 666 /data/clock.hap
281    hdc shell bm install -p /data/clock.hap
282    ```
283
2843.  Run the following command to start the app.  **ohos.samples.clock**  indicates the app package name, and  **MainAbility**  indicates the ability started by the app.
285
286    ```
287    hdc shell aa start -d 123 -a ohos.samples.clock.MainAbility -b ohos.samples.clock
288    ```
289
2904.  \(Optional\) Run the following command to uninstall the app.  **ohos.samples.clock**  indicates the app package name.
291
292    ```
293    hdc shell bm uninstall -n ohos.samples.clock
294    ```
295
296
297## FAQs<a name="section1122413460153"></a>
298
299### hdc\_std Fails to Connect to a Device<a name="section1922725151614"></a>
300
301-   **Symptom**
302
303    **\[Empty\]**  is displayed in the output after the  **hdc\_std list targets**  command is run.
304
305-   **Possible Causes and Solutions**
306    1.  The device fails to be identified.
307
308        Check whether  **HDC Device**  exists in the universal serial bus device of the device manager. If  **HDC Device**  does not exist, the device cannot be connected. In this case, remove and then insert the device or burn the latest image for the device.
309
310    2.  hdc\_std works improperly.
311
312        Run the  **hdc kill**  or  **hdc start -r**  command to kill or restart the HDC service, and then run the  **hdc list targets**  command to check whether device information is obtained.
313
314    3.  hdc\_std does not match the device.
315
316        If the latest image is burnt for the device, hdc\_std must also be of the latest version. As hdc\_std is updated continuously, obtain hdc\_std of the latest version from the  **developtools\_hdc\_standard**  repository in the  **prebuilt**  directory.
317
318
319
320### hdc\_std Fails to Run<a name="section15657547131615"></a>
321
322-   **Symptom**
323
324    The  **hdc\_std.exe**  file does not run after being clicked.
325
326-   **Possible Causes and Solutions**
327
328    **hdc\_std.exe**  requires no installation and can be directly used on a disk. It can also be added to environment variables. Open the  **cmd**  window and run the  **hdc\_std**  command to use  **hdc\_std.exe**.
329
330
331