Android广播监听网络状态

第一步:检查网络状态的类型
需要设置权限–允许查看网络状态

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetUtil {

    //没有连接网络
    private static final int NETWORK_NONE = -1;
    //移动网络
    private static final int NETWORK_MOBILE = 0;
    //WiFi
    private static final int NETWORK_WIFI = 1;

    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 NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}
第二步:创建状态回调接口
public interface NetEvent {
    void onNetChange(int netMobile);
}
第三步:创建广播接收器
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetBroadcastReceiver extends BroadcastReceiver {
    private NetEvent netEvent;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            //检查网络状态的类型
            int netWrokState = NetUtil.getNetWorkState(context);
            if (netEvent != null)
                // 接口回传网络状态的类型
                netEvent.onNetChange(netWrokState);
        }
    }
    public void setNetEvent(NetEvent netEvent) {
        this.netEvent = netEvent;
    }
}

第四步:在Acitivity中动态注册广播,这里不能静态注册哦,原因你懂的

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements NetEvent {
    //网络状态
    private int netMobile;
    private TextView mtvNet;
    // 监控网络状态的广播
    private NetBroadcastReceiver netBroadcastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mtvNet = (TextView) findViewById(R.id.tv_net);
    }
    @Override
    public void onStart() {
        super.onStart();
        //注册广播
        if (netBroadcastReceiver == null) {
            netBroadcastReceiver = new NetBroadcastReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(netBroadcastReceiver, filter);
            //设置监听方法
            netBroadcastReceiver.setNetEvent(this);
        }
    }
    @Override
    public void onNetChange(int netMobile) {
        this.netMobile = netMobile;
        isNetConnect();
    }
    private void isNetConnect() {
        switch (netMobile) {
            case 1://wifi
               Toast.makeText(this, "当前网络类型:wifi", Toast.LENGTH_SHORT).show();
                break;
            case 0://移动数据
 
  
               Toast.makeText(this, "当前网络类型:移动数据", Toast.LENGTH_SHORT).show();
                break;
            case -1://没有网络
 
  
               Toast.makeText(this, "当前无网络", Toast.LENGTH_SHORT).show();
                break;
        }
    }
  @Override public void onDestroy() {
 
  
    super.onDestroy();
    if (netBroadcastReceiver != null) {
        //注销广播 
        unregisterReceiver(netBroadcastReceiver); 
	} 
}
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(Android广播监听网络状态)