ConnectivityManager判断是否有网络
2013-10-27 15:10
399 查看
![](http://gundumw100.iteye.com/images/icon_star.png)
/** 判断是否有网络 */
private boolean checkNetwork() {
boolean flag = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null)
flag = cwjManager.getActiveNetworkInfo().isAvailable();
if (!flag) {
Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("<span class=\"hilite\">android</span>.intent.action.VIEW");
startActivity(mIntent);
}
}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();
b.show();
}
return flag;
}
Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
http://www.cnblogs.com/mainroadlee/archive/2011/01/11/Android_Network_State_Checking_And_Setting.html
Java代码
![](http://gundumw100.iteye.com/images/icon_star.png)
/**
* 检测是否开启wify或gprs
* @param context
* @return
*/
public boolean checkNetwork(final Context context){
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile 3G Data Network
State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//wifi
State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
if(mobile==State.CONNECTED||mobile==State.CONNECTING)
return true;
if(wifi==State.CONNECTED||wifi==State.CONNECTING)
return true;
Builder b = new android.app.AlertDialog.Builder(context).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
b.setPositiveButton("设置网络", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
// context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面
}
}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();
b.show();
return false;
}
public static boolean checkNet(Context context) { try { // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if( connectivity!= null ) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if( info != null && info.isConnected()) { // 判断当前网络是否已经连接 if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } }catch(Exception e) { return false; } return false; }
App自动更新之通知栏下载
http://www.cnblogs.com/qianxudetianxia/archive/2011/04/12/2010919.html
相关文章推荐
- 判断android手机当前是否接入网络
- 网络应用:生成随机验证图片并在输入验证码后判断验证码是否正确
- Android网络状态的监听+判断是否前台进程+ListView的滚动底部
- 在android的开发中,判断是否连接网络
- ionic ngCordova判断是否有网络以及监控网络变化
- Android判断网络是否可用并且开启网络
- 『转』android 程序中判断当前是否连接网络,网络是否可用
- android 开发中判断网络是否连接的代码
- android项目判断是否有可用网络
- 判断网络是否为真正的公网IP
- 判断网络是否连通
- 判断设备是否有网络连接
- jquery 判断网络资源,网络文件是否存在
- android 程序中判断当前是否连接网络,网络是否可用
- Android判断网络是否连接以及连接类型
- 判断GPS、网络是否开启
- Broadcast监听网络状态,ping判断网络是否可用
- 使用API判断网络是否连通(InternetGetConnectedState / IsNetworkAlive)
- Android中判断网络连接是否可用
- Android判断当前网络是否可用