检查当前手机网络的Android代码

package com.util;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
 * 判断当前手机联网渠道的工具
 * 
 * @author Administrator
 */
public class NetUilt {
	/** 检查当前手机网络 */
	
	
    
	public static boolean checkNet(Context context) {
	
			// 判断连接方式
		boolean wifiConnected = isWIFIConnected(context);
		boolean mobileConnected = isMOBILEConnected(context);
	if (wifiConnected == false && mobileConnected == false) {
			// 如果都没有连接返回false,提示用户当前没有网络
			return false;
		}
		return true;


		
	}


	// 判断手机使用是wifi还是mobile
	/**
	 * 判断手机是否采用wifi连接
	 */
	public static boolean isWIFIConnected(Context context) {
		// Context.CONNECTIVITY_SERVICE).


		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}


	/** 判断mobile连接 */
	public static boolean isMOBILEConnected(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}
获取网络信息状态的函数,选择是否设置网络
boolean isNetwork = NetUilt.checkNet(getActivity());// 获取网络信息
if (!isNetwork){


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setTitle(R.string.netstate);
    builder.setMessage(R.string.setnetwork);
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {

            Intent mIntent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); 
            startActivity(mIntent);
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.create();
    builder.show();

}


新版本这样子获取网络状态信息


public class NetUtility {
   //得到网络状态信息
    public static boolean checkNetworkConnection(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activityNetwork = cm.getActiveNetworkInfo();
        return activityNetwork != null && activityNetwork.isConnected();
    }
    //设置网络(弹出对话框去打开网络)
    public static void noNetworkAlert(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(R.string.netstate);
        builder.setMessage(R.string.setnetwork);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent mIntent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                context.startActivity(mIntent);
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.create();
        builder.show();
    }
}



 
 

你可能感兴趣的:(Android成长日记)