判断网络是否可以用

膜拜大神亮之—- 判断网络是否可以用

package com.zsz.develop.registerutils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

/**
 * @author LigthWang
 *
 *         描述:判断网络的类型的封装
 */
public class CheckNetwork {
private Context context;

public CheckNetwork(Context context) {
    this.context = context;
}

public static boolean IsWifiAvailable(Context context) {
    boolean flag = false;
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeInfo = manager.getActiveNetworkInfo();
    if (activeInfo != null && activeInfo.isConnected()) {
        flag = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        // mobileConnected = activeInfo.getType() ==
        // ConnectivityManager.TYPE_MOBILE;
    }
    return flag;
}

public static boolean IsNetworkAvailable(Context context) {
    boolean flag = false;
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager.getActiveNetworkInfo() != null) {
        flag = manager.getActiveNetworkInfo().isAvailable();
    }

    return flag;
}

public boolean CheckNetworkAvailable() {
    boolean flag = false;
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager.getActiveNetworkInfo() != null) {
        flag = manager.getActiveNetworkInfo().isAvailable();
    }
    if (!flag) {
        // ToastUtil.show(context, "连接不可用,请确认您的网络通畅,稍后再试!");
    }
    return flag;
}

public boolean CheckNetworkAndSet() {
    boolean flag = false;
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager.getActiveNetworkInfo() != null) {
        flag = manager.getActiveNetworkInfo().isAvailable();
    }
    if (!flag) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle("网络状态 ");
        builder.setMessage("当前网络不可用,是否设置网络?");
        builder.setPositiveButton("设置",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        context.startActivity(new Intent(
                                Settings.ACTION_SETTINGS)); // ACTION_WIRELESS_SETTINGS
                                                            // 网络设
                    }
                });
        builder.setNegativeButton("离线浏览",
                new DialogInterface.OnClickListener() {

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

    return flag;

}
}