Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

android / dayu

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • dayu
  • ..
  • utils
  • NetworkConnectChangedReceiver.java
Find file
Normal viewHistoryPermalink
NetworkConnectChangedReceiver.java 3.59 KB
Newer Older
罗翻's avatar
去除代码中的中文字
b61ecd3a
 
罗翻 committed 7 years ago
1 2 3
package com.dayu.utils; /**
 * Created by yu
 * on 2017/10/24.
yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
4 5 6 7 8 9 10 11 12 13 14
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

罗翻's avatar
去除代码中的中文字
b61ecd3a
 
罗翻 committed 7 years ago
15 16 17
import com.dayu.baselibrary.R;
import com.dayu.common.BaseApplication;

yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
18 19 20 21 22 23 24 25 26 27 28 29
/**
 * 网络改变监控广播
 * <p>
 * 监听网络的改变状态,只有在用户操作网络连接开关(wifi,mobile)的时候接受广播,
 * 然后对相应的界面进行相应的操作,并将 状态 保存在我们的APP里面
 * <p>
 * <p>
 */
public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
罗翻's avatar
去除代码中的中文字
b61ecd3a
 
罗翻 committed 7 years ago
30
            connType = BaseApplication.getContext().getString(R.string.net_3G);
yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
31
        } else if (type == ConnectivityManager.TYPE_WIFI) {
罗翻's avatar
去除代码中的中文字
b61ecd3a
 
罗翻 committed 7 years ago
32
            connType = BaseApplication.getContext().getString(R.string.net_wifi);
yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        }
        return connType;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 监听wifi的打开与关闭,与wifi的连接无关
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e("TAG", "wifiState:" + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
            }
        }
        // 监听wifi的连接状态即是否连上了一个有效无线路由
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                // 获取联网状态的NetWorkInfo对象
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                //获取的State对象则代表着连接成功与否等状态
                NetworkInfo.State state = networkInfo.getState();
                //判断网络是否已经连接
                boolean isConnected = state == NetworkInfo.State.CONNECTED;
                Log.e("TAG", "isConnected:" + isConnected);
                if (isConnected) {
                } else {

                }
            }
        }
        // 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            //获取联网状态的NetworkInfo对象
            NetworkInfo info = intent
                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                //如果当前的网络连接成功并且网络连接可用
                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI
                            || info.getType() == ConnectivityManager.TYPE_MOBILE) {
罗翻's avatar
增加jswebview
d822891d
 
罗翻 committed 7 years ago
76 77
//                        EventBus.getDefault().post(new RefreshTab(0));
//                        RxBus.getDefault().post(new RefreshTab(0) );
yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
78 79 80
                    }
                } else {

mReturn's avatar
red packet
020e3ff9
 
mReturn committed 4 years ago
81
//                    ToastUtils.showShortToast(BaseApplication.getContext().getString(R.string.net_not_connect));
yu's avatar
2017-10-24
d425bd15
 
yu committed 7 years ago
82 83 84 85 86
                }
            }
        }
    }
}