Android原生的代码中是可以将手机在wifi的情况下强制使用移动网络,
Android中原生代码:
private void forceSendRequestByMobileData() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NET_CAPABILITY_INTERNET);
//强制使用蜂窝数据网络-移动数据
builder.addTransportType(TRANSPORT_CELLULAR);
NetworkRequest build = builder.build();
connectivityManager.requestNetwork(build, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
try {
URL url = new URL("");
HttpURLConnection connection = (HttpURLConnection)network.openConnection(url);
/省略参数配置/
connection.connect();
/数据流处理/
} catch (Exception e) {
}
}
});
}> >
在mui中应该如何写呢?ios又该怎么处理?
var MainActivity = plus.android.runtimeMainActivity();
var Context = plus.android.importClass('android.content.Context');
var ConnectivityManager = plus.android.importClass("android.net.ConnectivityManager");
var NetworkRequest = plus.android.importClass("android.net.NetworkRequest");
var NetWork = plus.android.importClass('android.net.Network');
var connectivityManager = MainActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
var NetworkCapabilities = plus.android.importClass("android.net.NetworkCapabilities");
var builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
//强制使用蜂窝数据网络-移动数据
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
var networkRequest = builder.build();
var myCallback = plus.android.newObject("android.net.ConnectivityManager$NetworkCallback");
console.log(JSON.stringify(myCallback));
myCallback.onAvailable = function(network) {
console.log("onAvailable: ");
// connectivityManager.bindProcessToNetwork(network);
}
回调方法我就不知道怎么写了?主要是那个network对象拿不到?怎么处理呢
0 个回复