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
相关推荐
86417413 2020-11-25
83206733 2020-11-19
86276537 2020-11-19
83266337 2020-11-19
86256434 2020-11-17
zhouboxiao 2020-11-16
rise 2020-11-22
sssdssxss 2020-11-20
windle 2020-11-10
孙雪峰 2020-10-30
xfcyhades 2020-11-20
liuxudong00 2020-11-19
cheidou 2020-11-19
gunhunti 2020-09-25
csdnYF 2020-11-15
达观数据 2020-11-11
playlinuxxx 2020-11-11