如果移动数据打开,wifi也打开但没有数据,那么如何通过WIFI发送请求

时间:2017-11-08 05:18:13

标签: android android-connectivitymanager

如果您只想使用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);
        }
    });
}

2 个答案:

答案 0 :(得分:1)

当你想要从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);
        }
    });
}

广播

公共类NetworkStateChangeBroadcast扩展了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);

        }
    }
}

}

答案 1 :(得分:0)

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void stayOnWifi(Context context){     最后的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);
    }
});

}