简体   繁体   English

android:如何知道设备中是否有互联网连接?

[英]android: how to know internet connection is available or not in device?

I want to check the Internet connection available or not in device so how can i got this..please give me code for this.. 我想检查设备中的互联网连接是否可用,所以我该如何获取..请为此提供代码。

thank a lot In advance 非常感谢

hey buddy...apply this code..it might be helpful to u.thanks in advance.... 嘿,伙计...应用此代码..可能对提前感谢您很有帮助....

boolean connected; 布尔连接;

    private boolean checkInternetConnection()
    {

        ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

        // ARE WE CONNECTED TO THE NET

        if (conMgr.getActiveNetworkInfo() != null

                && conMgr.getActiveNetworkInfo().isAvailable()

                && conMgr.getActiveNetworkInfo().isConnected()) 
        {

        return true;

        }
        else 
        {
        return false;

        }
    }
        ConnectivityManager connectionService =
            (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) {
            // Device is online
        }
// Network is connected or not 
public boolean isConnected()
{
    ConnectivityManager connectivity = (ConnectivityManager) context.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)
                  {
                      Log.d("LOG","Network is Available");
                      return true;
                  }

      }
      return false;
}

Use the following function inorder to check the internet connction: 请使用以下功能为了检查互联网连接:

public boolean isOnline()
    {

         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo ni = cm.getActiveNetworkInfo();
         boolean result = false;
         if(ni != null )
         {
             if(  ni.getState() == NetworkInfo.State.CONNECTED )
             {
                 result = true;
             }
         }

         return result;

        } 

check what isOnline() returns .If true then Internet is connected else Inernet is not connected . 检查isOnline()返回的结果。如果为true,则说明已连接Internet,否则未连接Inernet Hope this will help you..:) 希望这个能对您有所帮助..:)

Use this Answer It`s help. 使用此Answer It的帮助。

public boolean isOnline() {
  ConnectivityManager cm =(ConnectivityManager)      
  getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo netInfo = cm.getActiveNetworkInfo();
  if (netInfo != null && netInfo.isConnectedOrConnecting()) {
     return true;
  }
  return false;
}

if(temp==true){
    genHelper.showToast("Net Connection");              
}else{              
    genHelper.showToast(" Not  Connected");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定,如果互联网连接当前可用且在Android设备上有效? - how to determine, if an internet connection is currently available and active on an android device? 如何知道设备上运行的当前互联网连接? - how to know current internet connection run on device? 如何在android中检查当前的互联网连接是否可用 - How to check currently internet connection is available or not in android Android:如果没有可用的互联网连接,如何关闭应用程序? - Android: how to close app if no Internet connection available? Android-如何检查设备是否可以连接互联网? - Android - How to check, device has Internet connection? 如何正确检查Android设备上的互联网连接? - How to properly check internet connection on android device? 如何知道互联网连接是否存在? - How to know that internet connection is there or not? 如何使用 android 中的互联网连接检查手机检查服务器连接是否可用? - How to check server connection is available or not with internet connection check mobile in android? 当Android应用中无法使用互联网连接时,如何暂停当前活动 - How to pause the current activity when internet connection is not available in android app 如何在使用服务的 android 中互联网连接不可用时显示对话框 - How to display dialog when internet connection not available in android using services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM