2

如果您只想使用 Wifi,请使用此代码。请确保 android 版本应大于 21。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void stayOnWifi(Context context) {
    final ConnectivityManager connection_manager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

        @Override
        public void onAvailable(Network network) {
            ConnectivityManager.setProcessDefaultNetwork(network);
        }
    });
}
4

2 回答 2

4

如果 wifi 没有数据,当您想从 WIFI 发送请求时使用。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void stayOnWifi(Context context) {
    final ConnectivityManager connection_manager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

        @Override
        public void onAvailable(Network network) {
            ConnectivityManager.setProcessDefaultNetwork(network);
        }
    });
}

如果 wifi 没有任何数据,当您想从 wifi 发送请求时使用此方法。上面代码的问题是当你强制设置数据使用优先级时。如果我们设置 wifi 优先应用程序将无法使用移动数据,如果为移动数据数据设置优先级,那么无论 wifi 是否有数据,应用程序都无法使用 wifi .


问题的解决方案我们应该使用广播,意味着当我们想从没有数据的wifi发送请求时使用上述方法,并且在完成使用广播后,它设置可以是移动或wifi的可用数据。意味着限制将被取消。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

public static void stayOnMobileData(Context context) {
    final ConnectivityManager connection_manager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            ConnectivityManager.setProcessDefaultNetwork(network);
        }
    });
}

播送

public class NetworkStateChangeBroadcast extends BroadcastReceiver {
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override
    public void onReceive(Context context, Intent intent) {
        if (in.bets.bettywifi.ui.common.BaseActivity.checkNetworkStatus(context)) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            final android.net.NetworkInfo mobile = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifi.isConnected()) {

                BaseActivity.stayOnWifi(context);
            } else if (mobile.isConnected()) {

                BaseActivity.stayOnMobileData(context);

            }
        }
    }
}
于 2017-11-08T05:55:39.590 回答
0
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void stayOnWifi(Context context) {
    final ConnectivityManager connection_manager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

        @Override
        public void onAvailable(Network network) {
            ConnectivityManager.setProcessDefaultNetwork(network);
        }
    });
}
于 2017-11-08T05:18:56.693 回答