android判斷phonegap是否聯網動態加載 super.loadUrl網址


 

 1 public void onCreate(Bundle savedInstanceState) {
 2 
 3  super.onCreate(savedInstanceState);
 4          super.setIntegerProperty("splashscreen", R.drawable.splash);
 5          ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
 6          NetworkInfo info = cwjManager.getActiveNetworkInfo();
 7          if (info != null && info.isAvailable()){
 8              super.loadUrl("http://www.xxx.com",4500);
 9          }
10          else
11          {
12              super.loadUrl("file:///android_asset/www/error.html", 4500);
13          }
14         }

解釋下  第四行 顯示啟動畫面

第七行判斷是否聯網 如果聯網加載遠程地址 如果不聯網加載本地地址

別忘網絡權限

error.html錯誤界面代碼

主要js代碼

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

曾經有人很奇怪為什么要用java判斷是否聯網而不直接在js里面判斷主要原因:

1,不願意讓他顯示www.xx.com網址不能訪問情況

2,避免直接訪問實際網址

3,而且免去本地判斷跳轉遠程地址中間空白頁面

原創轉載請標注:http://www.cnblogs.com/coderV/archive/2013/02/23/2923301.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM