刚刚没看清, 如果只是让自己的 APP 可以上外网的话可以试试这个
final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
// 设置感兴趣的网络功能
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
//强制使用蜂窝数据网络-移动数据
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
NetworkRequest request =
builder.build();
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
@
Override public void onAvailable(Network network) {
Log.d(TAG, "onAvailable: network" + network);
if (Build.VERSION.SDK_INT >= 23) {
connectivityManager.bindProcessToNetwork(network);
} else {
ConnectivityManager.setProcessDefaultNetwork(network);
}
}
};
connectivityManager.requestNetwork(request, callback);