PhoneGap, "Building cross-platform APP:PHONEGAP mobile Applications" serial four (using program load events)

Source: Internet
Author: User

After you understand what's going on in PhoneGap, this section will begin to describe in detail the usage of these events. This sectionto describes the program load events, which are the 3 events of Deviceready, pause, and resume.

"Example 4-2 use of program load events"

01<! DOCTYPE html> 02

After the program is run, the system automatically loads the script in the PhoneGap and then pops up the interface shown in 4-7 to indicate that the device is loaded. And when the user clicks the "Back" button or "HOME" button, will also pop up the corresponding dialog box, 4-8, but not equal to the author's response to click on the "OK" button, the program has been placed in the background.

Tsinghua University Press launched the "build cross-platform App:phonegap mobile app" is an app book, contains more than 20 app instances and 3 app projects, but also combined with HTML5 mobile development. This book is for mobile development beginners, low threshold, shallow reading, the most suitable for mobile app startup small white. This release of the 3rd to 5th chapter as the basis for readers to learn, interested in the Welcome to buy this book!

Figure 4-7 Popup dialog after device loading is complete

Figure 4-8 The same popup dialog box prompts when the program is paused

As a matter of fact, if you run the program at this point, it will also pop up the corresponding dialog box, but the truth is that when you run the program again, there is no dialog pop-up prompt "program is restored." This is not due to the wrong code, but rather because of some specific invocation relationships of phonegap, and in order to prove this, there are now some modifications to the 16th to 29th line of the example. In this modification, the use of the Alert popup dialog is discarded, but the Console.log method is used to output the information on Eclipse's Logcat panel when the corresponding function is executed.

The function Ondeviceready () {console.log ("The device is loaded!) is executed when the device has finished loading. ");} Executes the function OnPause () {Console.log ("The program is paused!) when the program is paused. ");} When the program is resumed from a paused state, the function functions Onresume () {console.log ("The device is restored! ");}

Then run the program, as shown in the output information 4-9 on the Logcat panel.

Figure 4-9 Information in Logcat

It turns out that when a program resumes from a paused state, calling some DOM operations may not get the expected results, which is related to factors such as compatibility and operational efficiency of the PHONEGAP under the Android platform, which need to be paid special attention in the actual development.

The following example illustrates the use of events in PhoneGap, as shown in the examples of lines 10th, 12, and 14, where you want to operate on an event in PhoneGap only by Document.addeventlistener (" EventName ", function, false); Such a format can be defined. Where EventName is the name of the event that needs to be defined, and function is a custom function that is responsible for responding to the event.

Tip: An interesting question to look at in the example is that the declaration of pause and resume two events is done after the device has been loaded, which is a good habit, and every phonegap developer tries to adapt.

Take this opportunity to introduce a bit of extra knowledge, which is the way to debug in PhoneGap. There are often unexpected errors in program development, and in general you can verify where the program went wrong by outputting some data at a specific location. At this point, you need to consider what method to use to get the data.

For developers accustomed to web development, using the alert method to eject data in a dialog box is a very convenient option, but in practice this method is not very convenient, so the Console.log method is often used to output information to facilitate debugging.

In addition to using the Console.log method, there are many people who like to use JavaScript DOM operations to output the results directly in the page, in some cases, such as the need to count a variable over a period of time changes, this method is also very useful.

Tsinghua University Press launched the "build cross-platform App:phonegap mobile app" is an app book, contains more than 20 app instances and 3 app projects, but also combined with HTML5 mobile development. This book is for mobile development beginners, low threshold, shallow reading, the most suitable for mobile app startup small white. This release of the 3rd to 5th chapter as the basis for readers to learn, interested in the Welcome to buy this book! Welcome Dabigatran:348632872, direct questions to the author of the Exchange!



PhoneGap, "Building cross-platform APP:PHONEGAP mobile Applications" serial four (using program load events)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.