如果您只想使用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);
}
});
}
答案 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是否有数据
@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);
}
});
}