本文整理汇总了Java中com.blankj.utilcode.util.NetworkUtils类的典型用法代码示例。如果您正苦于以下问题:Java NetworkUtils类的具体用法?Java NetworkUtils怎么用?Java NetworkUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetworkUtils类属于com.blankj.utilcode.util包,在下文中一共展示了NetworkUtils类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lazyLoad
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
/**
* 懒加载
*/
protected void lazyLoad() {
if (!NetworkUtils.isConnected()) {
ToastUtils.showShort(R.string.no_internet);
return;
}
if (!isPrepared || isLoadData) {
return;
}
if (Key.getSetting().PREF_PRELOAD || !(getActivity() instanceof MainActivity)) {
isLoadData = true;
initData();
return;
}
if (!isVisible) {
return;
}
isLoadData = true;
initData();
}
示例2: onWidgetClick
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
@Override
public void onWidgetClick(View view) {
switch (view.getId()) {
case R.id.btn_open_wireless_settings:
NetworkUtils.openWirelessSettings();
break;
case R.id.btn_set_data_enabled:
NetworkUtils.setDataEnabled(!NetworkUtils.getDataEnabled());
break;
case R.id.btn_set_wifi_enabled:
NetworkUtils.setWifiEnabled(!NetworkUtils.getWifiEnabled());
break;
}
setAboutNetwork();
}
示例3: setAboutNetwork
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
private void setAboutNetwork() {
tvAboutNetwork.setText("isConnected: " + NetworkUtils.isConnected()
+ "\ngetDataEnabled: " + NetworkUtils.getDataEnabled()
+ "\nis4G: " + NetworkUtils.is4G()
+ "\ngetWifiEnabled: " + NetworkUtils.getWifiEnabled()
+ "\nisWifiConnected: " + NetworkUtils.isWifiConnected()
+ "\nisWifiAvailable: " + NetworkUtils.isWifiAvailable()
+ "\nisAvailableByPing: " + NetworkUtils.isAvailableByPing()
+ "\ngetNetworkOperatorName: " + NetworkUtils.getNetworkOperatorName()
+ "\ngetNetworkTypeName: " + NetworkUtils.getNetworkType()
+ "\ngetIPAddress: " + NetworkUtils.getIPAddress(true)
// + "\ngetDomainAddress: " + NetworkUtils.getDomainAddress("baidu.com")
);
}
示例4: setAboutNetwork
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
private void setAboutNetwork() {
tvAboutNetwork.setText("isConnected: " + NetworkUtils.isConnected()
+ "\nisAvailableByPing: " + NetworkUtils.isAvailableByPing()
+ "\ngetDataEnabled: " + NetworkUtils.getDataEnabled()
+ "\nis4G: " + NetworkUtils.is4G()
+ "\ngetWifiEnabled: " + NetworkUtils.getWifiEnabled()
+ "\nisWifiConnected: " + NetworkUtils.isWifiConnected()
+ "\nisWifiAvailable: " + NetworkUtils.isWifiAvailable()
+ "\nisAvailableByPing: " + NetworkUtils.isAvailableByPing()
+ "\ngetNetworkOperatorName: " + NetworkUtils.getNetworkOperatorName()
+ "\ngetNetworkTypeName: " + NetworkUtils.getNetworkType()
+ "\ngetIPAddress: " + NetworkUtils.getIPAddress(true)
+ "\ngetDomainAddress: " + NetworkUtils.getDomainAddress("baidu.com")
);
}
示例5: getData
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
@Override
public void getData() {
Key.getSetting();
LCache.init();
ApiManager.getDefault();
Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
if (true) {
e.onNext(true);
return;
}
String ip = NetworkUtils.getDomainAddress(ApiManager.domain);
if (ip == null) {
e.onNext(false);
return;
}
e.onNext(NetworkUtils.isAvailableByPing(ip));
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean connected) throws Exception {
if (connected) {
ActivityUtils.startActivity(MainActivity.class);
} else {
ToastUtils.showShort("服务端连接失败.");
}
finish();
}
});
}
示例6: onSubscribe
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
@Override
public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
disposable = d;
if (!NetworkUtils.isConnected()) {
// LogUtils.e(TAG, "网络不可用");
ToastUtils.showShort(R.string.toast_network_error);
} else {
// LogUtils.e(TAG, "网络可用");
}
}
示例7: onWidgetClick
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
@Override
public void onWidgetClick(View view) {
switch (view.getId()) {
case R.id.btn_open_wireless_settings:
NetworkUtils.openWirelessSettings();
break;
case R.id.btn_set_data_enabled:
NetworkUtils.setMobileDataEnabled(!NetworkUtils.getMobileDataEnabled());
break;
case R.id.btn_set_wifi_enabled:
NetworkUtils.setWifiEnabled(!NetworkUtils.getWifiEnabled());
break;
}
setAboutNetwork();
}
示例8: setAboutNetwork
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
private void setAboutNetwork() {
tvAboutNetwork.setText(new SpanUtils()
.appendLine("isConnected: " + NetworkUtils.isConnected())
.appendLine("getMobileDataEnabled: " + NetworkUtils.getMobileDataEnabled())
.appendLine("isMobileData: " + NetworkUtils.isMobileData())
.appendLine("is4G: " + NetworkUtils.is4G())
.appendLine("getWifiEnabled: " + NetworkUtils.getWifiEnabled())
.appendLine("isWifiConnected: " + NetworkUtils.isWifiConnected())
.appendLine("isWifiAvailable: " + NetworkUtils.isWifiAvailable())
.appendLine("getNetworkOperatorName: " + NetworkUtils.getNetworkOperatorName())
.appendLine("getNetworkTypeName: " + NetworkUtils.getNetworkType())
.append("getIPAddress: " + NetworkUtils.getIPAddress(true))
.create()
);
}
示例9: intercept
import com.blankj.utilcode.util.NetworkUtils; //导入依赖的package包/类
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!NetworkUtils.isConnected()) {//没网强制从缓存读取(必须得写,不然断网状态下,退出应用,或者等待一分钟后,就获取不到缓存)
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
Response response = chain.proceed(request);
Response responseLatest;
if (NetworkUtils.isConnected()) {
int maxAge = 60; //有网失效一分钟
responseLatest = response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 3; // 离线缓存时间为 3 天
responseLatest = response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return responseLatest;
}