4

我正在尝试检查网络是否可用。
但在我的模拟器中它总是显示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;
     }
    }
4

3 回答 3

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 方法中使用代码,例如

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" />
于 2012-06-15T08:47:55.020 回答
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"/>
于 2012-06-15T08:50:18.243 回答
0

用这个:

private boolean getOnlineState() {
        ConnectivityManager myConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnectivityManager
                .getActiveNetworkInfo();
        if (myNetworkInfo != null
                && myNetworkInfo.isConnectedOrConnecting())
            return true;
        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;
  }

  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;

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

于 2012-06-15T08:55:21.467 回答