且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查在Android互联网连接

更新时间:2023-01-01 18:53:21

有时候主动连接不是第一个在列表中,或者处于非活动状态或处于错误状态。这是我会怎么做:

Sometimes the active connection is not first in the list, or is inactive or in an error state. This is how I would do it:

  NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;

请不要忘记添加此权限的应用程序清单:

Don't forget to add this permission in the application manifest:

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

这是否帮助你?

Does this help you?

灵光