【Android】为App所在的进程指定网络

最近开发一个项目,要求一个安卓设备同时接入两个无线网络。如果直接使用两块无线网卡,在驱动和应用层以及安卓框架中都会遇到问题,因此,选择了一款同时具备有线网卡和无线网卡的安卓开发板,然后再使用一块OpenWrt核心板将有线网络转换成无线网络,并实现透传。开发App的时候,发现安卓会为App选择一个默认优先级最高的网络,因此同时连接有线和无线时,应用的数据默认不走有线网口,下面介绍一种解决方案。

代码

if (Build.VERSION.SDK_INT >= 21) {   
    final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);   
NetworkRequest.Builder builder = new NetworkRequest.Builder();   

// 设置指定的网络传输类型(蜂窝传输) 等于手机网络
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);   

// 设置感兴趣的网络功能   
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);   

// 设置感兴趣的网络:计费网络   
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);   

NetworkRequest request = builder.build();  
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
    /**
     * Called when the framework connects and has declared a new network ready for use.       
     * This callback may be called more than once if the {@link Network} that is       
     * satisfying the request changes.       
     * 
     */      
    @TargetApi(Build.VERSION_CODES.M)     
    @Override      
    public void onAvailable(Network network) {         
      super.onAvailable(network);         
      Log.i("test", "已根据功能和传输类型找到合适的网络");         

      // 可以通过下面代码将app接下来的请求都绑定到这个网络下请求
      if (Build.VERSION.SDK_INT >= 23) {   
        connectivityManager.bindProcessToNetwork(network);
      } else {   
        // 23后这个方法舍弃了   
        ConnectivityManager.setProcessDefaultNetwork(network);
      }

      // 也可以在将来某个时间取消这个绑定网络的设置
      // if (Build.VERSION.SDK_INT >= 23) {
      //      onnectivityManager.bindProcessToNetwork(null);
      //} else {
      //     ConnectivityManager.setProcessDefaultNetwork(null);
      //}

      // 只要一找到符合条件的网络就注销本callback         
      // 你也可以自己进行定义注销的条件         
      connectivityManager.unregisterNetworkCallback(this);         
  };   
connectivityManager.requestNetwork(request, callback);}

TransportType

    * Indicates this network uses a Cellular transport.
     */
    public static final int TRANSPORT_CELLULAR = 0;

    /**
     * Indicates this network uses a Wi-Fi transport.
     */
    public static final int TRANSPORT_WIFI = 1;

    /**
     * Indicates this network uses a Bluetooth transport.
     */
    public static final int TRANSPORT_BLUETOOTH = 2;

    /**
     * Indicates this network uses an Ethernet transport.
     */
    public static final int TRANSPORT_ETHERNET = 3;

    /**
     * Indicates this network uses a VPN transport.
     */
    public static final int TRANSPORT_VPN = 4;

Capability

    public static final int NET_CAPABILITY_MMS            = 0;
    public static final int NET_CAPABILITY_SUPL           = 1;
    public static final int NET_CAPABILITY_DUN            = 2;
    public static final int NET_CAPABILITY_FOTA           = 3;
    public static final int NET_CAPABILITY_IMS            = 4;
    public static final int NET_CAPABILITY_CBS            = 5;
    public static final int NET_CAPABILITY_WIFI_P2P       = 6;
    public static final int NET_CAPABILITY_IA             = 7;
    public static final int NET_CAPABILITY_RCS            = 8;
    public static final int NET_CAPABILITY_XCAP           = 9;
    public static final int NET_CAPABILITY_EIMS           = 10;
    public static final int NET_CAPABILITY_NOT_METERED    = 11;

    /**
     * Indicates that this network should be able to reach the internet.
     */
    public static final int NET_CAPABILITY_INTERNET       = 12;
    public static final int NET_CAPABILITY_NOT_RESTRICTED = 13;
    public static final int NET_CAPABILITY_TRUSTED        = 14;
    public static final int NET_CAPABILITY_NOT_VPN        = 15;
    public static final int NET_CAPABILITY_VALIDATED      = 16;
    public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
    public static final int NET_CAPABILITY_FOREGROUND = 18;

你可能感兴趣的:(Android)