现在的位置: 首页 > 综合 > 正文

Android中判断网络

2013年08月24日 ⁄ 综合 ⁄ 共 612字 ⁄ 字号 评论关闭

 

直接给代码好啦,程序员都懂得!

if (!isConnectInternet(getContext())) {
         Toast.makeText(getContext(), "暂时没有可用的网络,请检查网络", Toast.LENGTH_SHORT).show();
     }else{
      Toast.makeText(getContext(), R.string.userorpass_wrong, Toast.LENGTH_LONG).show();

     }  

public boolean isConnectInternet(Context context)  {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.isAvailable()) {
              return true;
        }else{

              return false;
       }

}

抱歉!评论已关闭.