NetworkInfo对象的isConnected()与isAvailable()区别

来源:互联网 发布:xampp默认数据库密码 编辑:程序博客网 时间:2024/05/06 10:43

public class MainActivity extends Activity  {      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);            this.registerReceiver(mBroadcastReceiver, new IntentFilter(                  ConnectivityManager.CONNECTIVITY_ACTION));      }        private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()      {          @Override          public void onReceive(Context context, Intent intent)          {              Bundle bundle = intent.getExtras();              NetworkInfo aNetworkInfo = (NetworkInfo) bundle                      .get(ConnectivityManager.EXTRA_NETWORK_INFO);                if (aNetworkInfo.isConnected())              {                  System.out.println("connecte");              } else              {                  System.out.println("not connect");              }                if (aNetworkInfo.isAvailable())              {                  System.out.println("available");              } else              {                  System.out.println("not available");              }              System.out.println("--------------------");          }      };  }  

总结有以下5种状态:

在WLAN设置界面

1,显示连接已保存,但标题栏没有,即没有实质连接上,输出为:not connect, available

2,显示连接已保存,标题栏也有已连接上的图标,            输出为:connect, available

3,选择不保存后                                                                        输出为:not connect, available

4,选择连接,在正在获取IP地址时                                        输出为:not connect, not available

5,连接上后                                                                                输出为:connect, available

<span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">本文转自 </span><a target=_blank href="http://blog.csdn.net/santicom/article/details/6932421" target="_blank" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">http://blog.csdn.net/santicom/article/details/6932421</a>(地址已不可用)

0 0
原创粉丝点击