android 获取宽带类型,Android 网络类型判断

在Android中经常用到网络状态和网络类型,不如网络为WIFI是做某一操作,网络为数据流量时做另一个操作。

以下是获取网络类型的示例代码:

private void checkNetworkConnection() {

String TAG = "Basic Network Demo";

// Whether there is a Wi-Fi connection.

boolean wifiConnected = false;

// Whether there is a mobile connection.

boolean mobileConnected = false;

// BEGIN_INCLUDE(connect)

ConnectivityManager connMgr =

(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();

if (activeInfo != null && activeInfo.isConnected()) {

wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;

mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;

if (wifiConnected) {

Log.i(TAG, "The active connection is wifi.");

} else if (mobileConnected) {

Log.i(TAG, "The active connection is mobile.");

}

} else {

Log.i(TAG, "No wireless or mobile connection.");

}

// END_INCLUDE(connect)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值