Android中判断详细的网络

package com.example.networkjudgewangluopanduan;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkMobileNet();
    }

    public void checkMobileNet() {
        // 1.检测手机是否处于联网状态

        // 1.得到系统服务ConnectivityManager
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        // 2.得到网络信息类对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        // 3.进行判断
        if (networkInfo != null && networkInfo.isConnected()) {
            Toast.makeText(this, "已连接互联网!",Toast.LENGTH_LONG).show();

            // 检测手机的网络类型
            int type = networkInfo.getType();// 得到网络类型
            switch (type) {
                case ConnectivityManager.TYPE_WIFI:// wifi网络
                    Toast.makeText(this, "wifi!",Toast.LENGTH_LONG).show();
                    break;
                case ConnectivityManager.TYPE_MOBILE:// 移动数据
                    Toast.makeText(this, "移动数据!",Toast.LENGTH_LONG).show();
                    // 2g 3g 4g
                    getDetailMobileNetType(networkInfo);
                    break;
            }

        } else {
            Toast.makeText(this, "未连接互联网!" ,Toast.LENGTH_LONG).show();

            AlertDialog.Builder buider=new AlertDialog.Builder(this);

            buider.setTitle("网络连接设置");
            buider.setMessage("未连接网络,是否要进行设置?");

            buider.setPositiveButton("设置", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    //跳转到系统设置网络的界面
                    Intent intent=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                    startActivity(intent);

                }

            });
            buider.setNegativeButton("取消",  new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                }

            });
            buider.create().show();
        }

    }

    // 得到详细的网络类型 wap 2G/3G/4G网络
    public void getDetailMobileNetType(NetworkInfo netInfo) {

        String strNetworkType = "";
        // 得到整数类型
        int subtype = netInfo.getSubtype();
        // 得到名子
        String _strSubTypeName = netInfo.getSubtypeName();

        switch (subtype) {
            case TelephonyManager.NETWORK_TYPE_GPRS:// 联通2G
            case TelephonyManager.NETWORK_TYPE_EDGE:// 移动2G
            case TelephonyManager.NETWORK_TYPE_CDMA:// 电信2G
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN: // api<8 : replace by 11
                strNetworkType = "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: // api<13 : replace by 15
                strNetworkType = "3G";
                break;
            case TelephonyManager.NETWORK_TYPE_LTE: // api<11 : replace by 13
                strNetworkType = "4G";
                break;
            default:
                // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA")
                        || _strSubTypeName.equalsIgnoreCase("WCDMA")
                        || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                    strNetworkType = "3G";
                } else {
                    strNetworkType = _strSubTypeName;
                }

                break;

        }
        Log.d("zzz", "网络类型是:" + strNetworkType);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值