Java Code Examples for com.blankj.utilcode.util.NetworkUtils

The following examples show how to use com.blankj.utilcode.util.NetworkUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: YCAudioPlayer   Source File: BaseActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    if (BaseConfig.INSTANCE.isNight()) {
        setTheme(getDarkTheme());
    }
    super.onCreate(savedInstanceState);
    setContentView(getContentView());
    ButterKnife.bind(this);
    //避免切换横竖屏
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    StateAppBar.setStatusBarColor(this,R.color.redTab);
    if (mPresenter != null){
        mPresenter.subscribe();
    }
    initView();
    initListener();
    if(!NetworkUtils.isConnected()){
        ToastUtils.showShort("请检查网络是否连接");
    }
    initData();
}
 
Example 2
Source Project: YCAudioPlayer   Source File: OnLineMusicFragment.java    License: Apache License 2.0 6 votes vote down vote up
private void initRecyclerView() {
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));
    final RecycleViewItemLine line = new RecycleViewItemLine(activity, LinearLayout.HORIZONTAL,
            SizeUtils.dp2px(1), Color.parseColor("#f5f5f7"));
    recyclerView.addItemDecoration(line);
    adapter = new OnLineMusicAdapter(activity);
    recyclerView.setAdapter(adapter);
    recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if(NetworkUtils.isConnected()){
                onLazyLoad();
            }else {
                ToastUtils.showRoundRectToast("没有网络");
            }
        }
    });
}
 
Example 3
Source Project: YCAudioPlayer   Source File: WebViewActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 显示加载失败时自定义的网页
 */
protected void initErrorPage() {
    if (mErrorView == null) {
        mErrorView = View.inflate(this, R.layout.view_custom_data_error, null);
        LinearLayout ll_error_view = (LinearLayout) mErrorView.findViewById(R.id.ll_error_view);
        ll_error_view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (NetworkUtils.isConnected()) {
                    //如果有网络,则刷新页面
                    mWebView.reload();
                    recreate();
                } else {
                    //没有网络,不处理
                    ToastUtils.showRoundRectToast("请检查是否连上网络");
                }
            }
        });
        mErrorView.setOnClickListener(null);
    }
}
 
Example 4
Source Project: QPM   Source File: BasicInfoUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static String getNetworkType() {
    NetworkUtils.NetworkType networkType = NetworkUtils.getNetworkType();
    switch (networkType) {
        case NETWORK_2G:
            return "2G";
        case NETWORK_3G:
            return "3G";
        case NETWORK_4G:
            return "4G";
        case NETWORK_WIFI:
            return "WIFI";
        case NETWORK_NO:
            return "无";
        case NETWORK_UNKNOWN:
            return UNKNOW;
    }
    return UNKNOW;
}
 
Example 5
Source Project: YCAudioPlayer   Source File: InterceptorUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 网络缓存拦截器,网络连接时请求服务器,否则从本地缓存中获取
 * @return
 */
public static Interceptor addNetWorkInterceptor(){
    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!NetworkUtils.isConnected()) {
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
                LogUtils.d("addNetWorkInterceptor"+ "没有网络链接");
            }
            Response response = chain.proceed(request);
            if (NetworkUtils.isConnected()) {
                int maxAge = 0; // 有网络时 设置缓存超时时间0个小时
                LogUtils.d("addNetWorkInterceptor"+ "网络已连接,缓存时间为:" + maxAge);
                response = response.newBuilder()
                        .addHeader("Cache-Control", "public, max-age=" + maxAge)
                        .removeHeader("Pragma")// 清除头信息,因为服务器如果不支持,会返回一些干扰信息,不清除下面无法生效
                        .build();
            } else {
                int maxStale = Constant.TIME_CACHE;
                LogUtils.d("addNetWorkInterceptor"+ "网络未连接,缓存时间为:" + maxStale);
                response = response.newBuilder()
                        .addHeader("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .removeHeader("Pragma")
                        .build();
            }
            String cookeHeader = response.header("Set-Cookie", "");
            LogUtils.e("cookeHeader1-----------"+cookeHeader);
            return response;
        }
    };
    return interceptor;
}
 
Example 6
Source Project: YCAudioPlayer   Source File: LocalMusicFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void initRecyclerView() {
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));
    adapter = new LocalMusicAdapter(activity);
    recyclerView.setAdapter(adapter);
    recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if(NetworkUtils.isConnected()){
                onLazyLoad();
            }else {
                com.pedaily.yc.ycdialoglib.toast.ToastUtils.showRoundRectToast("没有网络");
            }
        }
    });
}
 
Example 7
Source Project: DoraemonKit   Source File: DbDebugFragment.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 网络变化时调用
 *
 * @param networkType
 */
public void networkChanged(NetworkUtils.NetworkType networkType) {
    if (tvIp == null) {
        return;
    }
    if (networkType == NetworkUtils.NetworkType.NETWORK_NO) {
        tvIp.setText("please check network is connected");
    } else {
        tvIp.setText("" + DebugDB.getAddressLog().replace("Open ", "").replace("in your browser", ""));
    }
}
 
Example 8
Source Project: DanDanPlayForAndroid   Source File: NetworkConsumer.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(Disposable disposable) {
    if (NetworkUtils.isAvailableByPing()) {
        if (display != null) {
            display.normalNetwork();
        }
    } else {
        if (display != null) {
            display.noNetwork();
        }
        LogUtils.i("no network");
    }
}
 
Example 9
Source Project: Android-UtilCode   Source File: NetworkActivity.java    License: Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 10
Source Project: Android-UtilCode   Source File: NetworkActivity.java    License: Apache License 2.0 5 votes vote down vote up
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")
    );
}
 
Example 11
Source Project: QPM   Source File: BasicInfoUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static List<InfoItem> getBaseInfo(Context context) {
    List<InfoItem> result = new ArrayList<>();
    // 基本信息
    result.add(new InfoItem("基本信息"));
    result.add(new InfoItem("是否ROOT", DeviceUtils.isDeviceRooted() ? "是" : "否"));
    result.add(new InfoItem("SDK版本", String.valueOf(DeviceUtils.getSDKVersionName())));
    result.add(new InfoItem("发布版本", Build.VERSION.RELEASE));
    result.add(new InfoItem("AndroidId", DeviceUtils.getAndroidID()));
    result.add(new InfoItem("设备厂商", DeviceUtils.getManufacturer()));
    result.add(new InfoItem("设备类型", DeviceUtils.getModel()));
    result.add(new InfoItem("产品型号", Build.PRODUCT));
    result.add(new InfoItem("主板型号", Build.BOARD));
    result.add(new InfoItem("显示型号", Build.DISPLAY));
    result.add(new InfoItem("序列号", Build.SERIAL));
    try{
        result.add(new InfoItem("IMEI", PhoneUtils.getIMEI()));
        result.add(new InfoItem("IMSI", PhoneUtils.getIMSI()));
    } catch (SecurityException e){
        //用户可能手动拒绝权限
    }
    result.add(new InfoItem("OCCID", getOCCID()));
    result.add(new InfoItem("SIM卡", PhoneUtils.isSimCardReady() ? "有" : "无"));

    // 网络相关
    result.add(new InfoItem("网络信息"));
    result.add(new InfoItem("Wifi名称", getWifiName(context)));
    result.add(new InfoItem("IP地址", NetworkUtils.getIPAddress(true)));
    result.add(new InfoItem("Mac地址", DeviceUtils.getMacAddress()));
    result.add(new InfoItem("运营商", NetworkUtils.getNetworkOperatorName()));
    result.add(new InfoItem("网络状态", getNetworkType()));
    result.add(new InfoItem("系统UA", System.getProperty("http.agent")));
    result.add(new InfoItem("聚美UA", getJMUserAgent(context)));

    //屏幕信息
    result.add(new InfoItem("屏幕信息"));
    result.add(new InfoItem("分辨率", getScreenSize(context)));
    result.add(new InfoItem("真实分辨率", ScreenUtils.getScreenWidth() + "x" + ScreenUtils.getScreenHeight()));
    result.add(new InfoItem("像素密度", String.valueOf(ScreenUtils.getScreenDensity())));
    result.add(new InfoItem("像素密度dp", String.valueOf(ScreenUtils.getScreenDensityDpi())));
    result.add(new InfoItem("屏幕尺寸", String.valueOf(getScreenInch(context) + "英寸")));

    // 硬件信息
    result.add(new InfoItem("硬件信息"));
    result.add(new InfoItem("CPU架构", getCPUType()));
    result.add(new InfoItem("CPU核数", String.valueOf(Runtime.getRuntime().availableProcessors())));
    result.add(new InfoItem("总内存", transferByte2MB(Runtime.getRuntime().totalMemory())));
    result.add(new InfoItem("最大内存", transferByte2MB(Runtime.getRuntime().maxMemory())));
    result.add(new InfoItem("剩余内存", transferByte2MB(Runtime.getRuntime().freeMemory())));
    result.add(new InfoItem("硬件信息", Build.HARDWARE));

    return result;
}