Phonegap学习点滴(2) -- 网络状态检测

Phonegap学习点滴(2) -- 网络状态检测  http://blog.csdn.net/x251808026/article/details/16992943

方法一:在MainActivity中检查然后传递参数到html

public class MainActivity extends DroidGap  {



	@Override 

    public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

         NetworkInfo info = cm.getActiveNetworkInfo();

         if (info != null){

        	 if (info.isAvailable()){

        		 super.loadUrl("http://www.baidu.com",6000);

        	 }else{

            	 super.loadUrl(Config.getStartUrl()); 

             }       	 

         }else{

        	 super.loadUrl(Config.getStartUrl()); 

         }

    }

	

}

 方法二:直接在html中检测

function checkConnection() {

            var networkState = navigator.network.connection.type;	

            var states = {};

            states[Connection.UNKNOWN]  = 1;

            states[Connection.ETHERNET] = 2;

            states[Connection.WIFI]     = 3;

            states[Connection.CELL_2G]  = 4;

            states[Connection.CELL_3G]  = 5;

            states[Connection.CELL_4G]  = 6;

            states[Connection.NONE]     = 0;



            alert('Connection type: ' + states[networkState]);

        }

 

你可能感兴趣的:(PhoneGap)