在android中进行网络连接检查

时间:2012-06-15 08:44:50

标签: android android-wifi

我正在尝试检查网络是否可用。
但在我的模拟器中它总是显示connected(在断开互联网之后)

我的代码中是否有任何错误。我正在使用 wifi 进行连接。

代码:

public class AndroidConnectivityActivity extends Activity
{
Button checkBtn;

/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
      checkBtn = (Button) findViewById(R.id.button1);

      checkBtn.setOnClickListener(new View.OnClickListener()
      {
       public void onClick(View v)
       {
        if(isInternetOn())
        {
         Toast.makeText(getBaseContext(), "Connected",
           Toast.LENGTH_SHORT).show();
        }
        else
        {
         Toast.makeText(getBaseContext(), "Not connected",
           Toast.LENGTH_SHORT).show();
        }
       }});
     }

     public final boolean isInternetOn()
     {
      ConnectivityManager connec = (ConnectivityManager) getSystemService
      (Context.CONNECTIVITY_SERVICE);

      if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
        ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
        ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
        ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
      {
       return true;
      }

      else if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
        || (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
      {
       return false;
      }

      return false;
     }
    }

3 个答案:

答案 0 :(得分:2)

使用下面的代码

public static boolean isInternetAvailable(Context context)
    {
        boolean isInternetAvailable = false;

        try
        {  
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if(networkInfo != null && (networkInfo.isConnected()))
            {
                isInternetAvailable  = true;
            }    
        }
        catch(Exception exception)
        {
            // Do Nothing
        }

        return isInternetAvailable;
    }

并尝试使用真实设备

使用Activity onCreate Method中的代码,如

if(isInternetAvailable(this))
{
Toast toast = Toast.makeText(this, "internet available", Toast.LENGTH_SHORT);
        toast.show();
}
else
{
Toast toast = Toast.makeText(this, "internet not available", Toast.LENGTH_SHORT);
        toast.show();
}

必需的清单权限

<uses-permission
        android:name="android.permission.INTERNET" />

    <uses-permission
        android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 1 :(得分:2)

使用下面显示的方法

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

并在清单集权限中

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

答案 2 :(得分:0)

使用它:

private boolean getOnlineState() {
        ConnectivityManager myConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnectivityManager
                .getActiveNetworkInfo();
        if (myNetworkInfo != null
                && myNetworkInfo.isConnectedOrConnecting())
            return true;
        return false;

    }

和btw:

if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
    ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
  {
   return true;
  }

  else if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
    || (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
  {
   return false;
  }

  return false;
 }

与:

相同
    if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
    ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
         return true;
         return false;

Yous应该在真实手机而不是模拟器上尝试这样的硬件操作。