package com.example.administrator.zhapp.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/9/10.
 */
public class NetWordStatus extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null || networkInfo.isAvailable()) {
            NetworkInfo.State mob = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();//手机
            NetworkInfo.State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();//wifi

            if (mob == NetworkInfo.State.CONNECTED || mob == NetworkInfo.State.CONNECTING) {
                Toast.makeText(context, "手机流量", Toast.LENGTH_SHORT).show();
            }
            if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTED) {
                Toast.makeText(context, "wifi", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
