Ex­plor­ing Phone­gap APIS

OpenSource For You - - DEVELOPERS OVERVIEW -

As I have al­ready given you the list of APIs in­cluded in 3KRnHGDS, OHW's HxSORUH WKHP WR sHH wKDW EHnH­fiW FDn EH ob­tained from each and how they can be in­cluded in your ap­pli­ca­tion to make it fea­ture rich. Th­ese APIs give you the ad­van­tage of ac­cess­ing na­tive de­vice fea­tures like the ac­celerom­e­ter, the com­pass, con­tacts, etc. Let’s start play­ing around with th­ese APIs.

Ac­celerom­e­ter: This is a mo­tion sen­sor that can de­tect a change in move­ment with re­spect to the de­vice’s ori­en­ta­tion in all the three axes. It al­lows you to change the po­si­tion of an ob­ject in your ap­pli­ca­tion with ev­ery move of your de­vice. Us­age: This can be im­ple­mented in rac­ing games. The meth­ods for us­ing the ac­celerom­e­ter are: getCur­ren­tAc­cel­er­a­tion()— Used to get the cur­rent ac­cel­er­a­tion along three axes x,y,z. watchAc­cel­er­a­tion() - Used to get the ac­cel­er­a­tion with some reg­u­lar time in­ter­val. clearWatch() - Used to stop the watch that you started with watchAc­cel­er­a­tion() The ar­gu­ments to pass through th­ese meth­ods are: 1. ac­celerom­e­terSuc­cess() - This method will be called when we get suc­cess­ful ac­cel­er­a­tion from the de­vice. ac­celerom­e­terEr­ror() - This method will be called when we fail to re­ceive ac­cel­er­a­tion from the de­vice. ac­celerom­e­terOp­tions – This will let you set the fre­quency for the time in­ter­val used in watchAc­cel­er­a­tion() So let’s try to put th­ese things up in the code to see it in ac­tion.

Re­place in­dex.html with ac­celerom­e­ter.html

1.

2.

3.

2.

3. The out­put of the above code is shown in Fig­ure 4. Ex­pla­na­tion: The code above shows the ac­cel­er­a­tion with an in­creas­ing time­stamp.

Ref­er­ence 1 in the code shows the doc­u­ment. ad­dEven­tLis­tener func­tion, which de­picts the wait for Phone­Gap to load. Ref­er­ences 2 and 3 de­pict that when Phone­Gap is loaded, the de­viceReady method will call start­watch(), i.e., the user de­fined method. Ref­er­ence 4 shows that in­side the start­watch(), there is a vari­able called the op­tions ID, de­pict­ing the fre­quency for the time­stamp. Ref­er­ence 5 shows that the method watchAc­cel­er­a­tion() fetches the cur­rent ac­cel­er­a­tion along the x,y and z axes. The ar­gu­ments passed will be called at the time of suc­cess or fail­ure. Ref­er­ences 6 and T in­di­cate that in case of suc­cess, the body el­e­ment 'Wait­ing for ac­cel­er­a­tion' will be re­placed by ‘ac­cel­er­a­tion’ with re­spect to x, y and z. Ref­er­ence 8 shows that in case of an er­ror, an alert will be shown with an er­ror mes­sage. Ref­er­ences 9 and 10 show that in the stop­Watch() method, if the watch is not null, it means the watchAc­cel­er­a­tion() method has been called be­fore; so use clearwatch() to stop watch­ing the ac­celerom­e­ter.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.