android 网络可用否,以及类型
来源:互联网 发布:linux 用web查看流量 编辑:程序博客网 时间:2024/05/21 15:50
在Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况。下面的代码,作为例子,详细说明了对于当前网络情况的判断。 先看一个自己定义的应用类。 view plaincopy to clipboardprint? 01.public class NetworkDetector { 02. 03. public static boolean detect(Activity act) { 04. 05. ConnectivityManager manager = (ConnectivityManager) act 06. .getApplicationContext().getSystemService( 07. Context.CONNECTIVITY_SERVICE); 08. 09. if (manager == null) { 10. return false; 11. } 12. 13. NetworkInfo networkinfo = manager.getActiveNetworkInfo(); 14. 15. if (networkinfo == null || !networkinfo.isAvailable()) { 16. return false; 17. } 18. 19. return true; 20. } 21.} public class NetworkDetector { public static boolean detect(Activity act) { ConnectivityManager manager = (ConnectivityManager) act .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); if (networkinfo == null || !networkinfo.isAvailable()) { return false; } return true; } } 这个类只有一个静态方法,用来检测当前系统的网络是否可用。如果可用,返回true。 还要进一步说明的是,NetworkInfo类中有一个方法getType(),这个方法可以用来判断当前可用的网络是wifi,还是mobile等等。 再来看使用的例子。 view plaincopy to clipboardprint? 01.boolean networkState = NetworkDetector.detect(XXXActivity.this); 02.if (!networkState) { 03. DialogUtil.openMsgDialog(XXXActivity.this, 04. android.R.drawable.ic_dialog_info, 05. “网络不可用,是否现在设置网络?”, android.R.string.ok, 06. android.R.string.cancel, 07. new DialogInterface.OnClickListener() { 08. @Override 09. public void onClick(DialogInterface dialog, int which) { 10. startActivityForResult(new Intent( 11. ACTION_WIRELESS_SETTINGS), 0); 12. } 13. }, new DialogInterface.OnClickListener() { 14. @Override 15. public void onClick(DialogInterface dialog, int which) { 16. dialog.cancel(); 17. } 18. }).show(); 19.} boolean networkState = NetworkDetector.detect(XXXActivity.this); if (!networkState) { DialogUtil.openMsgDialog(XXXActivity.this, android.R.drawable.ic_dialog_info, “网络不可用,是否现在设置网络?”, android.R.string.ok, android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startActivityForResult(new Intent( ACTION_WIRELESS_SETTINGS), 0); } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).show(); } 其中XXXActivity是我自己的Activity类,DialogUtil是我自己定义的Dialog应用类。这里的代码片段,是在判断没有网络的情况下,弹出dialog,提示用户是否进入系统的网络设置界面。 因为以上代码用到了权限,需要在AndroidManifest文件中加入如下权限代码:
- android 网络可用否,以及类型
- android 检测网络是否可用,以及网络类型
- 判断网络是否可用以及当前手机的网络类型
- Android 网络是否可用 及 获得运营商类型
- android 检查可用网络
- Android网络是否可用
- 检测网络是否可用及网络类型
- 检测网络是否可用 网络类型
- js控制控件可用否?
- android 判断当前环境网络是否可用及获取当前网络类型
- Android判断网络是否连接以及连接类型
- 一起学android之如何获取网络类型并判断是否可用(20)
- 网络是否可用判断以及Fragment
- [Android] 检查网络是否可用
- [Android]检查网络是否可用
- android 判断网络是否可用
- Android-验证网络是否可用
- android adb 网络不可用。
- Android: 主动抛出异常调试
- 多线程分享
- 有符号数和无符号数
- SQLServer数据库查询,更新一例
- 好书集锦
- android 网络可用否,以及类型
- AT命令收集
- pb解决数据窗列比较少,而选中状态比较宽现象
- Undo Tablespace 切换注意事项
- ITPUB知识索引贴--RAC
- VC窗口类注册与诞生
- 为什么下载的视频都打不开呢
- 为什么下载的视频都打不开呢
- Oracle 11g Active Data Guard