美文网首页
[Sample]BasicNetWorking

[Sample]BasicNetWorking

作者: 河婆墟邓紫棋 | 来源:发表于2017-01-01 23:48 被阅读37次

2017年的开篇,我计划写一个系列文章,坚持下去让自己努力提高。这一系列的文件是运行Android SDK中的Sample,从例子中学习Android各项知识。
第一篇《BasicNetWorking》

效果图:


整体效果图
整体效果图

概括:
这个栗子是使用ConnectivityManager去判断是否有网络连接。如果有,连接的类型是什么。

需要的权限:

<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

从效果图中看出,顶部bar有两个按钮,我们找到menu中的代码,如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/test_action"
        android:showAsAction="ifRoom|withText"
        android:title="@string/test_text" />
    <item android:id="@+id/clear_action"
        android:showAsAction="ifRoom|withText"
        android:title="@string/clear_text" />
</menu>

在MainActivity中,可以看到主要的代码在checkNetworkConnection方法中,在这个方法中,只判断了是否有网络。如果有,是wifi还mobile。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // When the user clicks TEST, display the connection status.
        case R.id.test_action:
            checkNetworkConnection();
            return true;
        // Clear the log view fragment.
        case R.id.clear_action:
            mLogFragment.getLogView().setText("");
            return true;
    }
    return false;
}

/**
 * Check whether the device is connected, and if so, whether the connection
 * is wifi or mobile (it could be something else).
 */
private void checkNetworkConnection() {

  ConnectivityManager connMgr =
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
  if (activeInfo != null && activeInfo.isConnected()) {
      wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
      mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
      if(wifiConnected) {
          Log.i(TAG, getString(R.string.wifi_connection));
      } else if (mobileConnected){
          Log.i(TAG, getString(R.string.mobile_connection));
      }
  } else {
      Log.i(TAG, getString(R.string.no_wifi_or_mobile));
  }

}

分析代码:
通过getSystemService传入CONNECTIVITY_SERVICE得到ConnectivityManager,再通过getActiveNetworkInfo()获取当前网络连接的具体信息类。
在获取NetWorkManger的时候需要权限,返回也可能为null。
在初始化网络操作前都可以用isConnected()判断是否有网络连接。

/**
 * Returns details about the currently active default data network. When
 * connected, this network is the default route for outgoing connections.
 * You should always check {@link NetworkInfo#isConnected()} before initiating
 * network traffic. This may return {@code null} when there is no default
 * network.
 *
 * @return a {@link NetworkInfo} object for the current default network
 *        or {@code null} if no network default network is currently active
 *
 * <p>This method requires the call to hold the permission
 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
 */
public NetworkInfo getActiveNetworkInfo() {
    try {
        return mService.getActiveNetworkInfo();
    } catch (RemoteException e) {
        return null;
    }
}

源码阅读:
我们阅读下源码中定义的连接类型,归结来说一般只需要判断TYPE_MOBILE和TYPE_WIFI即可,源码如下(去除隐藏API):

/**
 * The Mobile data connection.  When active, all data traffic
 * will use this network type's interface by default
 * (it has a default route)
 */
public static final int TYPE_MOBILE      = 0;
/**
 * The WIFI data connection.  When active, all data traffic
 * will use this network type's interface by default
 * (it has a default route).
 */
public static final int TYPE_WIFI        = 1;
/**
 * An MMS-specific Mobile data connection.  This network type may use the
 * same network interface as {@link #TYPE_MOBILE} or it may use a different
 * one.  This is used by applications needing to talk to the carrier's
 * Multimedia Messaging Service servers.
 */
public static final int TYPE_MOBILE_MMS  = 2;
/**
 * A SUPL-specific Mobile data connection.  This network type may use the
 * same network interface as {@link #TYPE_MOBILE} or it may use a different
 * one.  This is used by applications needing to talk to the carrier's
 * Secure User Plane Location servers for help locating the device.
 */
public static final int TYPE_MOBILE_SUPL = 3;
/**
 * A DUN-specific Mobile data connection.  This network type may use the
 * same network interface as {@link #TYPE_MOBILE} or it may use a different
 * one.  This is sometimes by the system when setting up an upstream connection
 * for tethering so that the carrier is aware of DUN traffic.
 */
public static final int TYPE_MOBILE_DUN  = 4;
/**
 * A High Priority Mobile data connection.  This network type uses the
 * same network interface as {@link #TYPE_MOBILE} but the routing setup
 * is different.  Only requesting processes will have access to the
 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
 * will route over this interface if no default route exists.
 */
public static final int TYPE_MOBILE_HIPRI = 5;
/**
 * The WiMAX data connection.  When active, all data traffic
 * will use this network type's interface by default
 * (it has a default route).
 */
public static final int TYPE_WIMAX       = 6;

/**
 * The Bluetooth data connection.  When active, all data traffic
 * will use this network type's interface by default
 * (it has a default route).
 */
public static final int TYPE_BLUETOOTH   = 7;

/**
 * Dummy data connection.  This should not be used on shipping devices.
 */
public static final int TYPE_DUMMY       = 8;

/**
 * The Ethernet data connection.  When active, all data traffic
 * will use this network type's interface by default
 * (it has a default route).
 */
public static final int TYPE_ETHERNET    = 9;

相关文章

网友评论

      本文标题:[Sample]BasicNetWorking

      本文链接:https://www.haomeiwen.com/subject/lnzivttx.html