Phonegap network connection HTML5

Source: Internet
Author: User

Phonegap Network connection judgment official website provides Network APIs

Cordova 3.4 APIs determine the network connection status

Determine if the network is 4G 3G 2g wifi none

This article provides Demo download at the end

Supported systems:

Amazon Fire OSAndroidBlackBerry 10 iOSWindows Phone 7 and 8 TizenWindows 8

Start with creating a project:

<1> Create a phonegap project command on the console:
 phonegap create my-app cd my-app phonegap run android

<2> Go To The plugins folder under the project directory from the command line.

cd my-appcd plugins

<3> download the plug-in now

cordova plugin add org.apache.cordova.network-information


Download complete:


<4> Add an android platform project (replace "android" With ios)

cordova platform add android

Added:

<5> compile the android Project

cordova build

Compilation completed:

Import the project to eclipse

The place in the circle is the code generated by the system.

Copy the following statement to www/index.html under the assets Directory;

Example:

            
         
         
         
         
         Hello World             

Network Connection of Steamed Stuffed bun





Test Apache Cordova

Connecting to Device

Device is Ready

<Script type = "text/javascript" src = "cordova. js "> </script> <script type =" text/javascript "src =" js/index. js "> </script> <script type =" text/javascript "> app. initialize (); </script> <script type = "text/javascript"> function aaa () {alert (111 );}; // here is the method for determining the network connection status function checkConnection () {var networkState = navigator. connection. type; var states ={}; states [Connection. UNKNOWN] = 'unknown connection'; states [connection. ETHERNET] = 'ethernet connection'; states [connection. WIFI] = 'wifi connection'; states [connection. CELL_2G] = 'cell 2G connection'; states [connection. CELL_3G] = 'cell 3G connection '; states [Connection. CELL_4G] = 'cell 4G connection '; states [Connection. CELL] = 'cell generic connection'; states [connection. NONE] = 'no network connection'; alert ('Connection type: '+ states [networkState]);} </script>


Code details:

                var networkState = navigator.connection.type;                var states = {};                states[Connection.UNKNOWN]  = 'Unknown connection';                states[Connection.ETHERNET] = 'Ethernet connection';                states[Connection.WIFI]     = 'WiFi connection';                states[Connection.CELL_2G]  = 'Cell 2G connection';                states[Connection.CELL_3G]  = 'Cell 3G connection';                states[Connection.CELL_4G]  = 'Cell 4G connection';                states[Connection.CELL]     = 'Cell generic connection';                states[Connection.NONE]     = 'No network connection';


Navigator. connection. type to get the current network status

The states array indicates the output status. You can modify the output parameters as needed.

Download the project and import the platforms of phonegap to eclipse.

If clear is reported, check whether the exported lib Package reports an error.

If there is still an error, you just need to change the google API to the latest android API.

If you encounter any problems with the import project, refer to my article.

Blog: http://blog.csdn.net/aaawqqq/article/details/20463183

Phonegap solution Error: Error initializing Cordova: Class not found:

Http://blog.csdn.net/aaawqqq/article/details/21243869

Demo:

Http://download.csdn.net/detail/aaawqqq/7111039

Related Article

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.