Android网络判断是否连接和网络类型

来源:互联网 发布:初中生在哪学编程 编辑:程序博客网 时间:2024/06/18 18:50
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.telephony.TelephonyManager;import static android.provider.Settings.ACTION_WIRELESS_SETTINGS;/** * Created by dell on 2017/11/5. */public class IsConnection {    /**     * 判断网络是否连接     */    public static boolean getIsConnection(Context context) {        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo info = manager.getActiveNetworkInfo();        if (info != null && info.isAvailable()) {            return true;        } else {            return false;        }    }    /**     * 判断网络类型的方法     */    public static String  getNetWorkType(final Context context){        /**        *result:只能返回一种结果,网络类型在同一时刻只能有一个类型        */        String result="";        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo info = manager.getActiveNetworkInfo();        if (info != null && info.isConnected()){            if (info.getType() == manager.TYPE_WIFI){                /**                *返回WIFI网络                */                result = "WIFI";            }else if(info.getType() == manager.TYPE_MOBILE){                /**                *返回的是移动流量                */                String name = info.getSubtypeName();                /**                 * 使用的是移动流量判断是那种类型                 */                int type = info.getSubtype();                switch (type){                    case TelephonyManager.NETWORK_TYPE_LTE:                        result = "4G";                        break;                    case TelephonyManager.NETWORK_TYPE_GPRS:                    case TelephonyManager.NETWORK_TYPE_EDGE:                    case TelephonyManager.NETWORK_TYPE_CDMA:                    case TelephonyManager.NETWORK_TYPE_1xRTT:                    case TelephonyManager.NETWORK_TYPE_IDEN:                        result = "2G";                        break;                    case TelephonyManager.NETWORK_TYPE_UMTS:                    case TelephonyManager.NETWORK_TYPE_EVDO_0:                    case TelephonyManager.NETWORK_TYPE_EVDO_A:                    case TelephonyManager.NETWORK_TYPE_HSDPA:                    case TelephonyManager.NETWORK_TYPE_HSUPA:                    case TelephonyManager.NETWORK_TYPE_HSPA:                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12                    case TelephonyManager.NETWORK_TYPE_HSPAP:                        result ="3G";                        break;                    default:                        if (name.equalsIgnoreCase("TD-SCDMA") || name.equalsIgnoreCase("WCDMA") || name.equalsIgnoreCase("CDMA2000")){                            result = "3G";                        }else{                            result = name;                        }                        break;                }            }        }else{            /**            *如果没有联网就要让用户决定是否去打开网络            */            AlertDialog.Builder builder = new AlertDialog.Builder(context);            builder.setTitle("设置网络");            builder.setMessage("是否去打开网络?");            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialogInterface, int i) {                    /**                     * 全部网络设置(ACTION_WIRELESS_SETTINGS);                     * WIFI设置(ACTION_WIFI_SETTINGS);                     * 3G流量设置: (ACTION_DATA_ROAMING_SETTINGS).                     */                    Intent intent = new Intent(ACTION_WIRELESS_SETTINGS);                    context.startActivity(intent);                }            });            builder.setNegativeButton("NO",null);            builder.create();            builder.show();        }        return result;    }}
原创粉丝点击