Android手機WiFi下強制使用4G數據流量上網

行之有效的方案

其他方案

private void doTest()
{
    display("Requesting CELLULAR network connectivity...");
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

    NetworkRequest request = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();

    connectivityManager.requestNetwork(request, 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.
         *
         * This method will be called on non-UI thread, so beware not to use any UI updates directly.
         *
         * @param network The {@link Network} of the satisfying network.
         */
        @Override
        public void onAvailable(final Network network)
        {
            display("Got available network: " + network.toString());

            try
            {
                final InetAddress address = network.getByName("navalclash.com");
                display("Resolved host2ip: " + address.getHostName() + " -> " +  address.getHostAddress());
            }
            catch (UnknownHostException e)
            {
                e.printStackTrace();
            }

            display("Do request test page from remote http server...");

            if(okHttpClient == null)
            {
                okHttpClient = new OkHttpClient.Builder().socketFactory(network.getSocketFactory()).build();
            }

            Request request = new Request.Builder()
                    .url("http://navalclash.com")
                    .build();
            try (Response response = okHttpClient.newCall(request).execute())
            {
                display("RESULT:\n" + response.body().string());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });
}
mSocket = new Socket();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Network network = null;
    try {
        //獲取WIFI的網口
        Method getNetworkMethod = WifiManager.class.getMethod("getCurrentNetwork");
        getNetworkMethod.setAccessible(true);
        network = (Network) getNetworkMethod.invoke(mWifiManager);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    if (network != null) {
        network.bindSocket(mSocket);
        }
    }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    Log.d("NetworkDns", "Requesting CELLULAR network connectivity...");

    NetworkRequest request = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();

    connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback()
    {
        @Override
        public void onAvailable(final Network network)
        {
            Log.d("NetworkDns", "Got available network: " + network.toString());

            try
            {
                final InetAddress address = network.getByName("www.website.com");
                Log.d("NetworkDns", "Resolved host2ip: " + address.getHostName() + " -> " +  address.getHostAddress());
            }
            catch (UnknownHostException e)
            {
                e.printStackTrace();
            }

            Log.d("NetworkDns", "Do request test page from remote http server...");

            OkHttpClient okHttpClient = null;

            if(okHttpClient == null)
            {
                okHttpClient = new OkHttpClient.Builder()
                        .socketFactory(network.getSocketFactory())
                        .dns(new Dns() {
                            @Override
                            public List<InetAddress> lookup(String hostname) throws UnknownHostException {
                                if (network != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    List<InetAddress> addresses = Arrays.asList(network.getAllByName(hostname));
                                    Log.d("NetworkDns", "List : " + addresses);
                                    return addresses;
                                }
                                return SYSTEM.lookup(hostname);
                            }
                        }).build();
            }

            Request request = new Request.Builder()
                    .url("http://www.website.com")
                    .build();
            try (Response response = okHttpClient.newCall(request).execute())
            {
                Log.d("NetworkDns", "RESULT:\n" + response.body().string());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    });
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章