Android determines whether phonegap is connected to the Internet and loads the super. loadUrl URL

Source: Internet
Author: User

Copy codeThe Code is as follows: public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Super. setIntegerProperty ("splashscreen", R. drawable. splash );
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo info = cwjManager. getActiveNetworkInfo ();
If (info! = Null & info. isAvailable ()){
Super. loadUrl ("http://www.xxx.com", 4500 );
}
Else
{
Super. loadUrl ("file: // android_asset/www/error.html", 4500 );
}
}

The fourth line shows the startup screen.
Row 7 determines whether the network is connected. If the network is loaded, the remote address is loaded. If the network is not connected, the local address is loaded.
Do not forget network Permissions
Error.html error interface code
Main js CodeCopy codeThe Code is as follows: document. addEventListener ("deviceready", onDeviceReady, false );
Function onDeviceReady (){
CheckConnection ();
Document. addEventListener ("backbutton", eventBackButton, false); // return key
// Document. addEventListener ("menubutton", eventMenuButton, false); // menu
}
Function checkConnection (){
Var networkState = navigator. network. connection. type;
If (networkState = Connection. NONE ){
Navigator. notification. confirm ('make sure the network connection is enabled ', showAlert, 'hs',' OK ');
Return false;
}
}
Function showAlert (button ){
If (button = 1 ){
Navigator. app. exitApp ();
}
Return false;
}
Function eventBackButton (){
Navigator. notification. confirm ('Are you sure you want to exit? ', ShowConfirm, 'Exit the software', 'OK, cancel ');
}
Function showConfirm (button ){
If (button = 1 ){
Document. removeEventListener ("backbutton", eventBackButton, false); // cancel the return key
Navigator. app. exitApp ();
}
}

Some people once wondered why they used java to determine whether to connect to the Internet rather than directly determining the main reason in js:
1. Do not want to show him that the www.xxxxx.com website cannot be accessed
2. Avoid direct access to the actual website
3. Skip local judgment and jump to the blank page in the middle of the remote address

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.