StupidBeauty
Read times:1032Posted at:Mon Jan 6 23:31:34 2014
- no title specified

转载:[Android实例] android 检查网络状态

可使用ConnectivityManager来检查手机当前是否已经连接到互联网。

http://www.eoeandroid.com/thread-158025-1-1.html

亮点:

public class CheckNet {
public static boolean checkNet(Context context){
//获得手机所有连接管理对象(包括对wi-fi等连接的管理)
try{
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;
}
}

在相应的Activity中 直接使用即可

private void checkNetAll(){
if(CheckNet.checkNet(MainActivity.this)==false){
Toast.makeText(MainActivity.this, R.string.chackNetError, Toast.LENGTH_LONG).show(); //这里我只做了一个提示,可根据情况设置成其它形式。
}

}

未知美人

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands