判断网络连接状况:
activity中的代码 :ConnectivityManager 网络连接管理类 BroadcastReceiver 抽象类,实现其中的抽象方法。Service也是抽象类 记得实现其中的抽象方法
package com.lantu.tuhuadmin.netbroadcast;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import de.greenrobot.event.EventBus;
/**
* 网络监听广播
*/
public class MainActivity extends AppCompatActivity {
private NetReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
mReceiver = new NetReceiver();
//注册一个广播接收者
registerReceiver(mReceiver, filter);
//虽然他注册了广播接收者,但是他并没有进行处理
EventBus.getDefault().register(this);
}
public void onEvent(NetEvent netEvent) {
int state = netEvent.getState();
if (state == 1) {
//说明有网络连接
Toast.makeText(MainActivity.this, "网络连接正常", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消
unregisterReceiver(mReceiver);
}
}
自定义广播接收者里面的代码:
package com.lantu.tuhuadmin.netbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import de.greenrobot.event.EventBus;
/**
* Created on 2016/7/13.
* Author:XXX
* Description:网络广播监听
*/
public class NetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
//获取网络联系信息
NetworkInfo mobWorkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); //手机流量连接
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); //Wifi连接
if ((mobWorkInfo != null && mobWorkInfo.isConnected()) || (wifiNetInfo != null && wifiNetInfo.isConnected())) {
//说明有网络连接 1表示有网络连接 2 表示没有网络连接
int state = (mobWorkInfo != null && mobWorkInfo.isConnected()) || (wifiNetInfo != null && wifiNetInfo.isConnected()) ? 1 : 2;
EventBus.getDefault().post(new NetEvent(state, "网络已连接"));
} else {
//没有网络不进行处理
}
}
}
}
package com.lantu.tuhuadmin.netbroadcast;
/**
* Created on 2016/7/13.
* Author:XXX
* Description:XXX
*/
public class NetEvent {
private int state; // 1 网络可用 2不可用
private String msg;
public NetEvent(int state, String msg) {
this.state = state;
this.msg = msg;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
————————————————————————————————————————————>
其实不用使用EventBus,使用内部类实现更为简单,多一个jar会导致apk文件过大的。
代码如下:直接以内部类的形式实现,第一次见广播的这种写法
package com.lantu.tuhuadmin.netbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
/**
* 网络监听广播
*/
public class MainActivity extends AppCompatActivity {
private NetReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
mReceiver = new NetReceiver();
//注册一个广播接收者
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消
unregisterReceiver(mReceiver);
}
class NetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
//获取网络联系信息
NetworkInfo mobWorkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); //手机流量连接
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); //Wifi连接
if ((mobWorkInfo != null && mobWorkInfo.isConnected()) || (wifiNetInfo != null && wifiNetInfo.isConnected())) {
//说明有网络连接 1表示有网络连接 2 表示没有网络连接
// int state = (mobWorkInfo != null && mobWorkInfo.isConnected()) || (wifiNetInfo != null && wifiNetInfo.isConnected()) ? 1 : 2;
Toast.makeText(MainActivity.this, "网络连接正常", Toast.LENGTH_LONG).show();
} else {
//没有网络不进行处理
}
}
}
}
}