com.socks.jiandan.utils.NetWorkUtil Java Examples

The following examples show how to use com.socks.jiandan.utils.NetWorkUtil. 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 File: MainActivity.java    From JianDan with Apache License 2.0 6 votes vote down vote up
@Override
protected void initData() {

    netStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (NetWorkUtil.isNetWorkConnected(MainActivity.this)) {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.AVAILABLE));
                } else {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.UNAVAILABLE));
                }
            }
        }
    };

    registerReceiver(netStateReceiver, new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));
}
 
Example #2
Source File: MainActivity.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
@Override
protected void initData() {

    netStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (NetWorkUtil.isNetWorkConnected(MainActivity.this)) {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.AVAILABLE));
                } else {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.UNAVAILABLE));
                }
            }
        }
    };

    registerReceiver(netStateReceiver, new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));
}
 
Example #3
Source File: PictureFragment.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
public void onEventMainThread(NetWorkEvent event) {

        if (event.getType() == NetWorkEvent.AVAILABLE) {
            if (NetWorkUtil.isWifiConnected(getActivity())) {
                mAdapter.setIsWifi(true);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为WIFI模式,自动加载GIF图片");
                    lastShowTime = System.currentTimeMillis();
                }
            } else {
                mAdapter.setIsWifi(false);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为省流量模式,只加载GIF缩略图");
                    lastShowTime = System.currentTimeMillis();
                }
            }
            isFirstChange = false;
        }
    }
 
Example #4
Source File: PictureFragment.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
@Override
public void onStart() {
    super.onStart();

    Subscription subscribe = RxNetWorkEvent.toObserverable().subscribe(netWorkEvent -> {
        if (netWorkEvent.getType() == NetWorkEvent.AVAILABLE) {
            if (NetWorkUtil.isWifiConnected(getActivity())) {
                mAdapter.setIsWifi(true);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ToastHelper.Short(R.string.load_mode_wifi);
                    lastShowTime = System.currentTimeMillis();
                }
            } else {
                mAdapter.setIsWifi(false);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ToastHelper.Short(R.string.load_mode_3g);
                    lastShowTime = System.currentTimeMillis();
                }
            }
            isFirstChange = false;
        }
    });

    mRxBusComposite.add(subscribe);
}
 
Example #5
Source File: PictureFragment.java    From JianDan with Apache License 2.0 6 votes vote down vote up
public void onEventMainThread(NetWorkEvent event) {

        if (event.getType() == NetWorkEvent.AVAILABLE) {
            if (NetWorkUtil.isWifiConnected(getActivity())) {
                mAdapter.setIsWifi(true);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为WIFI模式,自动加载GIF图片");
                    lastShowTime = System.currentTimeMillis();
                }
            } else {
                mAdapter.setIsWifi(false);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为省流量模式,只加载GIF缩略图");
                    lastShowTime = System.currentTimeMillis();
                }
            }
            isFirstChange = false;
        }
    }
 
Example #6
Source File: MainActivity.java    From JianDan_OkHttpWithVolley with Apache License 2.0 6 votes vote down vote up
@Override
protected void initData() {

    netStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (NetWorkUtil.isNetWorkConnected(MainActivity.this)) {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.AVAILABLE));
                } else {
                    EventBus.getDefault().post(new NetWorkEvent(NetWorkEvent.UNAVAILABLE));
                }
            }
        }
    };

    registerReceiver(netStateReceiver, new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));
}
 
Example #7
Source File: PictureFragment.java    From JianDan_OkHttpWithVolley with Apache License 2.0 6 votes vote down vote up
public void onEventMainThread(NetWorkEvent event) {

        if (event.getType() == NetWorkEvent.AVAILABLE) {
            if (NetWorkUtil.isWifiConnected(getActivity())) {
                mAdapter.setIsWifi(true);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为WIFI模式,自动加载GIF图片");
                    lastShowTime = System.currentTimeMillis();
                }
            } else {
                mAdapter.setIsWifi(false);
                if (!isFirstChange && (System.currentTimeMillis() - lastShowTime) > 3000) {
                    ShowToast.Short("已切换为省流量模式,只加载GIF缩略图");
                    lastShowTime = System.currentTimeMillis();
                }
            }
            isFirstChange = false;
        }
    }
 
Example #8
Source File: PictureAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public PictureAdapter(BaseActivity activity, LoadResultCallBack loadResultCallBack, LoadFinishCallBack<Object> loadFinisCallBack, int type) {
    mActivity = activity;
    mType = type;
    mLoadFinisCallBack = loadFinisCallBack;
    mLoadResultCallBack = loadResultCallBack;
    mPictures = new ArrayList<>();
    isWifiConnected = NetWorkUtil.isWifiConnected(mActivity);
}
 
Example #9
Source File: PictureAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
public PictureAdapter(Activity activity, LoadResultCallBack loadResultCallBack, LoadFinishCallBack loadFinisCallBack, Picture.PictureType type) {
    mActivity = activity;
    mType = type;
    mLoadFinisCallBack = loadFinisCallBack;
    mLoadResultCallBack = loadResultCallBack;
    pictures = new ArrayList<>();
    isWifiConnected = NetWorkUtil.isWifiConnected(mActivity);
}
 
Example #10
Source File: PictureAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #11
Source File: VideoAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #12
Source File: PictureAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #13
Source File: PictureAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
public PictureAdapter(Activity activity, LoadResultCallBack loadResultCallBack, LoadFinishCallBack loadFinisCallBack, Picture.PictureType type) {
    mActivity = activity;
    mType = type;
    mLoadFinisCallBack = loadFinisCallBack;
    mLoadResultCallBack = loadResultCallBack;
    pictures = new ArrayList<>();
    isWifiConnected = NetWorkUtil.isWifiConnected(mActivity);
}
 
Example #14
Source File: JokeAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #15
Source File: VideoAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #16
Source File: FreshNewsAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadDate();
        } else {
            loadFromCache();
        }
    }
 
Example #17
Source File: PictureAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #18
Source File: JokeAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #19
Source File: NetStateReceiver.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(
            ConnectivityManager.CONNECTIVITY_ACTION)) {
        if (NetWorkUtil.isNetWorkConnected(context)) {
            RxNetWorkEvent.send(new NetWorkEvent(NetWorkEvent.AVAILABLE));
        } else {
            RxNetWorkEvent.send(new NetWorkEvent(NetWorkEvent.UNAVAILABLE));
        }
    }
}
 
Example #20
Source File: JokeAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #21
Source File: VideoAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #22
Source File: VideoAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {
    if (NetWorkUtil.isNetWorkConnected(mActivity)) {
        loadData();
    } else {
        loadCache();
    }
}
 
Example #23
Source File: PictureAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #24
Source File: PictureAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
public PictureAdapter(Activity activity, LoadResultCallBack loadResultCallBack, LoadFinishCallBack loadFinisCallBack, Picture.PictureType type) {
    mActivity = activity;
    mType = type;
    mLoadFinisCallBack = loadFinisCallBack;
    mLoadResultCallBack = loadResultCallBack;
    pictures = new ArrayList<>();
    isWifiConnected = NetWorkUtil.isWifiConnected(mActivity);
}
 
Example #25
Source File: JokeAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            loadData();
        } else {
            loadCache();
        }

    }
 
Example #26
Source File: FreshNewsAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            RequestManager.addRequest(new Request4FreshNews(FreshNews.getUrlFreshNews(page),
                    new Response.Listener<ArrayList<FreshNews>>() {
                        @Override
                        public void onResponse(ArrayList<FreshNews> response) {

                            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                            mLoadFinisCallBack.loadFinish(null);

                            if (page == 1) {
                                mFreshNews.clear();
                                FreshNewsCache.getInstance(mActivity).clearAllCache();
                            }

                            mFreshNews.addAll(response);
                            notifyDataSetChanged();

                            FreshNewsCache.getInstance(mActivity).addResultCache(JSONParser.toString(response),
                                    page);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                    mLoadFinisCallBack.loadFinish(null);
                }
            }), mActivity);
        } else {
            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            mLoadFinisCallBack.loadFinish(null);

            if (page == 1) {
                mFreshNews.clear();
                ShowToast.Short(ConstantString.LOAD_NO_NETWORK);
            }

            mFreshNews.addAll(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            notifyDataSetChanged();
        }

    }
 
Example #27
Source File: FreshNewsAdapter.java    From JianDan with Apache License 2.0 4 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            RequestManager.addRequest(new Request4FreshNews(FreshNews.getUrlFreshNews(page),
                    new Response.Listener<ArrayList<FreshNews>>() {
                        @Override
                        public void onResponse(ArrayList<FreshNews> response) {

                            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                            mLoadFinisCallBack.loadFinish(null);

                            if (page == 1) {
                                mFreshNews.clear();
                                FreshNewsCache.getInstance(mActivity).clearAllCache();
                            }

                            mFreshNews.addAll(response);
                            notifyDataSetChanged();

                            FreshNewsCache.getInstance(mActivity).addResultCache(JSONParser.toString(response),
                                    page);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                    mLoadFinisCallBack.loadFinish(null);
                }
            }), mActivity);
        } else {
            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            mLoadFinisCallBack.loadFinish(null);

            if (page == 1) {
                mFreshNews.clear();
                ShowToast.Short(ConstantString.LOAD_NO_NETWORK);
            }

            mFreshNews.addAll(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            notifyDataSetChanged();
        }

    }