android检测网络连接状态示例讲解
程序员文章站
2022-06-10 19:12:38
android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(androidmanifest.xml):复制代码 代码如下:
<uses-permission android:name="android.permission.access_network_state"/>
<uses-permission android:name="android.permission.internet"/>
/**
* 对网络连接状态进行判断
* @return true, 可用; false, 不可用
*/
private boolean isopennetwork() {
connectivitymanager connmanager = (connectivitymanager)getsystemservice(context.connectivity_service);
if(connmanager.getactivenetworkinfo() != null) {
return connmanager.getactivenetworkinfo().isavailable();
}
return false;
}
/**
* 访问百度主页,网络不可用则需设置
*/
private void initmoregames() {
string url_moregames = "http://www.baidu.com";
mwebview = (webview) findviewbyid(r.id.view_gamesort);
if (mwebview != null) {
mwebview.requestfocus();
websettings websettings = mwebview.getsettings();
if (websettings != null) {
websettings.setjavascriptenabled(true);
websettings.setcachemode(mode_private);
websettings.setdefaulttextencodingname("utf-8");
}
// 判断网络是否可用
if(isopennetwork() == true) {
mwebview.loadurl(url_moregames);
} else {
alertdialog.builder builder = new alertdialog.builder(moregamesactivity.this);
builder.settitle("没有可用的网络").setmessage("是否对网络进行设置?");
builder.setpositivebutton("是", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
intent intent = null;
try {
string sdkversion = android.os.build.version.sdk;
if(integer.valueof(sdkversion) > 10) {
intent = new intent(android.provider.settings.action_wireless_settings);
}else {
intent = new intent();
componentname comp = new componentname("com.android.settings", "com.android.settings.wirelesssettings");
intent.setcomponent(comp);
intent.setaction("android.intent.action.view");
}
moregamesactivity.this.startactivity(intent);
} catch (exception e) {
log.w(tag, "open network settings failed, please check...");
e.printstacktrace();
}
}
}).setnegativebutton("否", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
dialog.cancel();
finish();
}
}).show();
}
} else {
log.w(tag, "mwebview is null, please check...");
}
}
android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(androidmanifest.xml):
复制代码 代码如下:
<uses-permission android:name="android.permission.access_network_state"/>
<uses-permission android:name="android.permission.internet"/>
然后,检测网络状态是否可用
复制代码 代码如下:
/**
* 对网络连接状态进行判断
* @return true, 可用; false, 不可用
*/
private boolean isopennetwork() {
connectivitymanager connmanager = (connectivitymanager)getsystemservice(context.connectivity_service);
if(connmanager.getactivenetworkinfo() != null) {
return connmanager.getactivenetworkinfo().isavailable();
}
return false;
}
最后,不可用则打开网络设置
复制代码 代码如下:
/**
* 访问百度主页,网络不可用则需设置
*/
private void initmoregames() {
string url_moregames = "http://www.baidu.com";
mwebview = (webview) findviewbyid(r.id.view_gamesort);
if (mwebview != null) {
mwebview.requestfocus();
websettings websettings = mwebview.getsettings();
if (websettings != null) {
websettings.setjavascriptenabled(true);
websettings.setcachemode(mode_private);
websettings.setdefaulttextencodingname("utf-8");
}
// 判断网络是否可用
if(isopennetwork() == true) {
mwebview.loadurl(url_moregames);
} else {
alertdialog.builder builder = new alertdialog.builder(moregamesactivity.this);
builder.settitle("没有可用的网络").setmessage("是否对网络进行设置?");
builder.setpositivebutton("是", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
intent intent = null;
try {
string sdkversion = android.os.build.version.sdk;
if(integer.valueof(sdkversion) > 10) {
intent = new intent(android.provider.settings.action_wireless_settings);
}else {
intent = new intent();
componentname comp = new componentname("com.android.settings", "com.android.settings.wirelesssettings");
intent.setcomponent(comp);
intent.setaction("android.intent.action.view");
}
moregamesactivity.this.startactivity(intent);
} catch (exception e) {
log.w(tag, "open network settings failed, please check...");
e.printstacktrace();
}
}
}).setnegativebutton("否", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
dialog.cancel();
finish();
}
}).show();
}
} else {
log.w(tag, "mwebview is null, please check...");
}
}
推荐阅读
-
Android 实时监测(监听)网络连接状态变化
-
Android判断设备网络连接状态及判断连接方式的方法
-
Android中检查网络连接状态的变化无网络时跳转到设置界面
-
Android编程监听网络连接状态改变的方法
-
android检测网络连接状态示例讲解
-
Android开发实现实时检测蓝牙连接状态的方法【附源码下载】
-
Android源码设计模式之状态模式实例讲解
-
android 状态栏提示 (Notification、NotificationManager) 示例
-
Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面 (转载)
-
Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面 (转载)