Android网络状态监听,wifi强度监听

1、网络状态判断

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络工具
 * @author fishpan
 */
public class NetWorkUtils {
	public Context context;
	
	public NetWorkUtils(Context context) {
		super();
		this.context = context;
	}

	/**
	 * 获取是否可用
	 * @param context
	 * @return
	 */
	public boolean available(){
		boolean flag = false;
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if(networkInfo != null && networkInfo.isConnected()){ 
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 获取当前网络类型名称
	 * @return	mobile 手机网络,wifi wifi网络
	 */
	public String netowrkTypeName(){
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if(networkInfo != null){
			return networkInfo.getTypeName();
		}
		return null;
	}
	
	/**
	 * 手机网络是否可用
	 * @return
	 */
	public boolean mobileAvailable(){
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo networkinfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
        if (networkinfo != null) {  
            return networkinfo.isAvailable();  
        }  
        return false;
	}
	
	/**
	 * wifi是否可用
	 * @return
	 */
	public boolean wifiAvailable(){
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo networkinfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
        if (networkinfo != null) {  
            return networkinfo.isAvailable();  
        }  
        return false;
	}
}

2、网络状态监听

      a、自定义broadcastreceive

public class NetworkStateBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) { 
		if(intent.getAction().equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)){
			wifiStateChange(context, intent);
		}else{
			networkChange(context);
		}
	}
	
	public void wifiStateChange(Context context, Intent intent){
		int wifi_state = intent.getIntExtra("wifi_state", 0);   
        int level = Math.abs(((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getRssi()); 
        Log.e("network", "wifi状态:" + wifi_state + "; wifi强度:" + level);
	}
	
	public void networkChange(Context context){
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
		if(networkInfo != null){
			int type = networkInfo.getType();
			switch (type) {
				case ConnectivityManager.TYPE_MOBILE:
					Log.e("network", "手机网络");
				break; 
				case ConnectivityManager.TYPE_WIFI:
					Log.e("network", "wifi网络");
				break;
			}
		}else{
			Log.e("network", "没有网络");
		}
	}
}
b、注册广播

NetworkStateBroadcastReceiver broadcastReceiver = new NetworkStateBroadcastReceiver();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, filter);

c、权限设置






你可能感兴趣的:(Android网络状态监听,wifi强度监听)