phonegap判断设备是否在线(android)

document.addEventListener("deviceready", onDeviceReady, false); 

    function onDeviceReady() {
        checkConnection();
        document.addEventListener("backbutton", eventBackButton, false); //返回键
        //document.addEventListener("menubutton", eventMenuButton, false);  //menu
    }
    function checkConnection() {
      var networkState = navigator.network.connection.type;
        if( networkState == Connection.NONE ) {
            navigator.notification.confirm('请确认网络连接已经开启', showAlert , '提示', '确定');
            return false;
        } 
  }
  function showAlert(button) {
          if( button==1 ) {
              navigator.app.exitApp();
          }
          return false;
  }
    function eventBackButton(){
          
        navigator.notification.confirm('确认退出?', showConfirm, '退出软件', '确定,取消'); 
         
    }
    function showConfirm(button) {
        
        if( button==1 ) {
            document.removeEventListener("backbutton", eventBackButton, false); //注销返回键
            navigator.app.exitApp();
        }
    }
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);
         }
        }

曾经有人很奇怪为什么要用java判断是否联网而不直接在js里面判断主要原因:

1,不愿意让他显示www.xx.com网址不能访问情况

2,避免直接访问实际网址

3,而且免去本地判断跳转远程地址中间空白页面

转载:http://www.cnblogs.com/coderV/archive/2013/02/23/2923301.html

相关推荐