Android判断当前网络是否可用
来源:互联网 发布:java自学视频教程 编辑:程序博客网 时间:2024/05/16 06:36
在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" />
实现的网络infor类:
import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;public class ConnectionDetetor {private Context mContext; public ConnectionDetetor(Context context){ this.mContext = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } public boolean isConnecting(){ ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if(connectivity!= null){ NetworkInfo[] info = connectivity.getAllNetworkInfo(); for(NetworkInfo in:info){ if(in.getState()== NetworkInfo.State.CONNECTED){ return true; } } } return false; }}
0 0
- Android判断当前网络是否可用
- Android判断当前网络是否可用
- Android中判断当前网络是否可用
- Android中判断当前网络是否可用
- android 判断当前网络是否可用
- Android中判断当前网络是否可用
- Android中判断当前网络是否可用
- Android中判断当前网络是否可用
- 判断当前网络是否可用
- 判断当前网络是否可用
- 判断当前网络是否可用
- 判断当前网络是否可用
- 判断当前网络是否可用
- 判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- 黑马程序员-类加载器的小总结
- TCP协议疑难杂症全景解析
- HashMap学习笔记:get方法
- C++ 线程安全的单例模式
- 隐马尔可夫模型(HMM)攻略
- Android判断当前网络是否可用
- linux:Superblock mount time is in the future
- 推荐引擎初探
- grep,fgrep,egrep
- C++与java多态的区别(转)
- 调整系统的inode数量
- j2me,我不准备深入了
- 你可能不知道的5个功能强大的 HTML5 API
- Asp.net MVC3部署问题-不能加载System.Web.Helpers的解决方案