来源:http://gundumw100.iteye.com/blog/1051235
1 /** 判断是否有网络 */ 2 private boolean checkNetwork() { 3 boolean flag = false; 4 ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 5 if (cwjManager.getActiveNetworkInfo() != null) 6 flag = cwjManager.getActiveNetworkInfo().isAvailable(); 7 if (!flag) { 8 Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接"); 9 b.setPositiveButton("确定", new DialogInterface.OnClickListener() { 10 public void onClick(DialogInterface dialog, int whichButton) { 11 Intent mIntent = new Intent("/"); 12 ComponentName comp = new ComponentName("com.android.settings", 13 "com.android.settings.WirelessSettings"); 14 mIntent.setComponent(comp); 15 mIntent.setAction("<span class="hilite">android</span>.intent.action.VIEW"); 16 startActivity(mIntent); 17 } 18 }).setNeutralButton("取消", new DialogInterface.OnClickListener() { 19 public void onClick(DialogInterface dialog, int whichButton) { 20 dialog.cancel(); 21 } 22 }).create(); 23 b.show(); 24 } 25 26 return flag; 27 }
1 Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面 2 http://www.cnblogs.com/mainroadlee/archive/2011/01/11/Android_Network_State_Checking_And_Setting.html 3 4 Java代码 5 /** 6 * 检测是否开启wify或gprs 7 * @param context 8 * @return 9 */ 10 public boolean checkNetwork(final Context context){ 11 ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 12 13 //mobile 3G Data Network 14 State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); 15 //wifi 16 State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); 17 18 //如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接 19 if(mobile==State.CONNECTED||mobile==State.CONNECTING) 20 return true; 21 if(wifi==State.CONNECTED||wifi==State.CONNECTING) 22 return true; 23 24 25 Builder b = new android.app.AlertDialog.Builder(context).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接"); 26 b.setPositiveButton("设置网络", new DialogInterface.OnClickListener() { 27 public void onClick(DialogInterface dialog, int whichButton) { 28 context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面 29 // context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面 30 } 31 }).setNeutralButton("取消", new DialogInterface.OnClickListener() { 32 public void onClick(DialogInterface dialog, int whichButton) { 33 dialog.cancel(); 34 } 35 }).create(); 36 b.show(); 37 return false; 38 39 }