您的位置:首页 > 理论基础 > 计算机网络

Android网络状态监控

2017-03-11 20:51 246 查看

网络状态判断类

/**
* Created by Notzuonotdied on 2016/10/31.
* 网络状态判断
*/
public class NetWorkUtils {
private static final int NET_TYPE_WIFI = 1;
private static final int NET_TYPE_MOBILE = 0;
public static final int NET_TYPE_NO_NETWORK = -1;

public Context context = null;

public NetWorkUtils() {

}

public NetWorkUtils(Context context) {
this.context = context;
}

public static MyApplication getApplication() {
return MyApplication.getInstance();
}

/**
* 判断是否联网
*/
public static boolean isConnectNET(final Context context) {
final ConnectivityManager conManage = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = conManage.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
return true;
} else {
Toast.makeText(context, "断网了,请检查网络~", Toast.LENGTH_SHORT).show();
return false;
}
}

/**
* 判断是否是WIFI连接
*/
public static boolean isConnectWIFI() {
final ConnectivityManager conManage = (ConnectivityManager)
getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conManage.getActiveNetworkInfo();
int netType = networkInfo != null ? networkInfo.getType() : -1;
return netType == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected();
}

public static int getNetWorkState(Context context) {
// 得到连接管理器对象
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
return NET_TYPE_WIFI;
} else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
return NET_TYPE_MOBILE;
}
}
return NET_TYPE_NO_NETWORK;
}

public static boolean isNetworkAviable(Context context) {
int state = getNetWorkState(context);
return state == 0 || state == 1;
}

public static void whenNetworkError() {
Intent intent = new Intent();
intent.setClass(getApplication(), NetWorkErrorActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
}


NetBroadcastReceiver

需要添加网络权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


注册

<receiver
android:name=".Service.NetBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>


在需要监听网络的类中

implements NetBroadcastReceiver.netEventHandler


在Acitivity中的onCreate(),在Fragment中的onCreateView()中

// 注册
NetBroadcastReceiver.mListeners.add(this);


public class NetBroadcastReceiver extends BroadcastReceiver {

public static ArrayList<netEventHandler> mListeners = new ArrayList<>();

@Override
public void onReceive(Context context, Intent intent) {
// 如果相等的话说明网络发生了变化
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
MyApplication.mNetWorkState = NetWorkUtils.getNetWorkState(context);
if (mListeners.size() > 0)// 通知接口完成加载
for (netEventHandler handler : mListeners) {
handler.onNetChange();
}
}
}

public interface netEventHandler {
void onNetChange();
}
}


MyApplication

在自定义的Application中保存网络状态

/**
* Created by Notzuonotdied on 2016/8/6.
* 全局变量类
*/
public class MyApplication extends Application {
public static int mNetWorkState;
private static MyApplication myApplication;

public static MyApplication getInstance() {
return myApplication;
}

@Override
public void onCreate() {
super.onCreate();
initNETService();
}

public void initNETService() {
mNetWorkState = NetWorkUtils.getNetWorkState(MyApplication.this);
}
}


在类中的定义方法

/**
* Created by Notzuonotdied on 2017/3/11.
*/

public class TestActivity extends Activity implements NetBroadcastReceiver.netEventHandler {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注册
NetBroadcastReceiver.mListeners.add(this);
}

@Override
public void onNetChange() {
// 当由无网络变为有网络的时候执行该方法
if (MyApplication.mNetWorkState != NetWorkUtils.NET_TYPE_NO_NETWORK) {
// ...
}
}
}


/**
* Created by Notzuonotdied on 2017/3/11.
*/
public class TestFragment extends Fragment implements NetBroadcastReceiver.netEventHandler {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
// 注册
NetBroadcastReceiver.mListeners.add(this);
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onNetChange() {
// 当由无网络变为有网络的时候执行该方法
if (MyApplication.mNetWorkState != NetWorkUtils.NET_TYPE_NO_NETWORK) {
// ...
}
}
}


附录

Android实时监听网络状态(1)

-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 网络