<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(HomeActivity.this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 200);
} else {
// 如果有权限做些什么
ThreadPoolManager.getInstance().execute(() -> LogUtils.i("-----", NetworkUtils.isAvailable(), NetworkUtils.isConnected()));
}
}
public void setEthernet2(int type) {
Log.i("调试信息", "setEthernet2: " + type);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder;
if (type == 0) {// 设置网络类型为wifi
builder = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
} else if (type == 1) {// 设置网络类型为有线
builder = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET);
} else {// 设置网络类型为移动数据网络
builder = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
}
connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.bindProcessToNetwork(network);
} else {
ConnectivityManager.setProcessDefaultNetwork(network);
}
}
});
}
Android 设置期望网络选择 wifi 移动流量 有线
最新推荐文章于 2024-08-14 20:01:35 发布