android determines if phonegap is connected to the Internet and loads the super.loadUrl url

  • 2020-05-09 19:17:00
  • OfStack

 
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); 
} 
} 

Explain that line 4 shows the splash screen
Line 7 determines whether the network is connected if the remote address is loaded on the network if the local address is not loaded on the network
Don't forget your network privileges
error.html error interface code
Main js code
 
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(' Please make sure the network connection is enabled ', showAlert , ' prompt ', ' determine '); 
return false; 
} 
} 
function showAlert(button) { 
if( button==1 ) { 
navigator.app.exitApp(); 
} 
return false; 
} 
function eventBackButton(){ 
navigator.notification.confirm(' Confirm exit? ', showConfirm, ' Exit the software ', ' determine , cancel '); 
} 
function showConfirm(button) { 
if( button==1 ) { 
document.removeEventListener("backbutton", eventBackButton, false); // Logout return key  
navigator.app.exitApp(); 
} 
} 

Someone once wondered why java should be used to judge whether a network is connected or not, instead of js directly to judge the main reason:
1, do not want him to show www.xxxxx.com url is not accessible
2. Avoid direct access to the actual website
3, and skip the local judge remote address middle blank page

Related articles: