Android判断当前网络是否可用--示例代码
在Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况。下面的代码,作为例子,详细说明了对于当前网络情况的判断。
先看一个自己定义的应用类。
- 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等等。
再来看使用的例子。
- 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文件中加入如下权限代码:
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
---------------------------------------------------------------------------
GL(arui319)
http://blog.csdn.net/arui319
<本文可以转载,但是请保留以上作者信息。谢谢。>
Android判断当前网络是否可用--示例代码的更多相关文章
- android——判断当前网络是否可用
http://www.cnblogs.com/codeworker/archive/2012/04/23/2467180.html //判断当前是否有网络连接 private boolean isCo ...
- Android检测网络状态,判断当前网络是否可用
用户手机当前网络可用:WIFI.2G/3G网络,用户打开与不打开网络,和是否可以用是两码事.可以使用指的是:用户打开网络了并且可以连上互联网进行上网. 检测当前网络是否可用,代码如下: /** * 检 ...
- Android中判断当前网络是否可用
转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...
- c#判断网络连接状态示例代码
使用c#判断网络连接状态的代码. 代码: public partial class Form1 : Form { [DllImport() == true) { label1.Text = " ...
- Android 检查手机网络是否可用
添加网络状态权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 代 ...
- Android程序检测网络是否可用
在做Android应用程序中,连接网络的时候,常常要用到检测网络状态是否可用,在这里分享一个比较好用的方法. 本人参考:http://blog.csdn.net/sunboy_2050/article ...
- android检测当前网络是否可用
在android程序中运行第一步就是检测当前有无可用网络 如果没有网络可用就退出程序 if (isConnect(this)==false) { ...
- Android 判断当前网络连接类型
实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...
- Android 用ping的方法判断当前网络是否可用
判断网络的情况中,有个比较麻烦的情况就是连上了某个网络,但是那个网络无法上网 ,,, = = 想到了用ping指令来判断,经测试,可行~ ~ ~ private static final boolea ...
随机推荐
- spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案
spring cloud 升级到最新版 后,报错: org.springframework.beans.factory.BeanCreationException: Error creating be ...
- 使用SqlServer中的float类型时发现的问题
在做项目中,使用了float类型来定义一些列,如:Price,但是发现了很多问题1.当值的位数大于6位是float型再转varchar型的时候会变为科学技术法显示 此时只好将float型转换成n ...
- j2ee model1模型完成分页逻辑的实现 详解!
在显示用户全部信息的页面,在显示全部数据的时候,长长的滚动条,像是没有边界的天空一样, 让用户查看数据很不方便. 于是, 我们要把这些数据分页显示, 就像office的word一样,每页显示一定数量的 ...
- dxg:TreeListView.RowDecorationTemplate
<dxg:TreeListView.RowDecorationTemplate> <ControlTemplate TargetType="ContentControl&q ...
- [Node.js]33. Level 7: Persisting Questions
Let's go back to our live-moderation app and add some persistence, first to the questions people ask ...
- Oracle查询client编码集
Oracle查询client编码集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ---------------- ...
- Python编程-基础知识-List
Negative Indexes(负索引) >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[-1] 'e ...
- Android 之开发积累
1.后台设置ImageView的src属性 有三种方式:img = (ImageView)this.findViewById(R.id.img_result_analyze); [1]setImage ...
- PHP $_POST
$_POST 变量用于收集来自 method="post" 的表单中的值. $_POST 变量 $_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值. ...
- 1z0-052 q209_5
5: Your database is open and the LISTENER listener is running. The new DBA of the system stops the l ...