Android中判斷網(wǎng)絡(luò)是否可用的代碼分享
package cn.hackcoder.beautyreader.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.util.Log; import android.widget.Toast; import cn.hackcoder.beautyreader.activity.base.BaseActivity; import cn.hackcoder.beautyreader.utils.NetWorkUtils; public class NetWorkStatusReceiver extends BroadcastReceiver { public NetWorkStatusReceiver() { } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Toast.makeText(context, "network changed", Toast.LENGTH_LONG).show(); BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)>0; } } }
package cn.hackcoder.beautyreader.utils; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.telephony.TelephonyManager; /** * Created by hackcoder on 15-1-25. */ public class NetWorkUtils { /** * 判斷是否有網(wǎng)絡(luò)連接 * @param context * @return */ public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /** * 判斷WIFI網(wǎng)絡(luò)是否可用 * @param context * @return */ public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } /** * 判斷MOBILE網(wǎng)絡(luò)是否可用 * @param context * @return */ public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } /** * 獲取當(dāng)前網(wǎng)絡(luò)連接的類型信息 * @param context * @return */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } /** * 獲取當(dāng)前的網(wǎng)絡(luò)狀態(tài) :沒有網(wǎng)絡(luò)0:WIFI網(wǎng)絡(luò)1:3G網(wǎng)絡(luò)2:2G網(wǎng)絡(luò)3 * * @param context * @return */ public static int getAPNType(Context context) { int netType = 0; ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_WIFI) { netType = 1;// wifi } else if (nType == ConnectivityManager.TYPE_MOBILE) { int nSubType = networkInfo.getSubtype(); TelephonyManager mTelephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { netType = 2;// 3G } else { netType = 3;// 2G } } return netType; } }
注冊(cè):
<receiver android:name=".broadcast.NetWorkStatusReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
- Android 判斷網(wǎng)絡(luò)狀態(tài)實(shí)例詳解
- Android中判斷網(wǎng)絡(luò)是否連接實(shí)例詳解
- Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問(wèn)題的解決方法
- Android編程判斷網(wǎng)絡(luò)是否可用及調(diào)用系統(tǒng)設(shè)置項(xiàng)的方法
- Android中判斷網(wǎng)絡(luò)連接狀態(tài)的方法
- Android判斷網(wǎng)絡(luò)類型的方法(2g,3g還是wifi)
- Android編程判斷網(wǎng)絡(luò)連接是否可用的方法
- Android中監(jiān)聽判斷網(wǎng)絡(luò)連接狀態(tài)的方法
- Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài)
- Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
相關(guān)文章
Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
這篇文章主要介紹了Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式,需要的朋友可以參考下2020-03-03Android 后臺(tái)生成長(zhǎng)圖并分享示例(非長(zhǎng)截圖)
這篇文章主要介紹了Android 后臺(tái)生成長(zhǎng)圖并分享示例(非長(zhǎng)截圖),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android AndBase框架使用封裝好的函數(shù)完成Http請(qǐng)求(三)
這篇文章主要介紹了Android AndBase框架使用封裝好的函數(shù)完成Http請(qǐng)求的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-03Android項(xiàng)目開發(fā) 教你實(shí)現(xiàn)Periscope點(diǎn)贊效果
這篇文章主要為大家分享了Android項(xiàng)目開發(fā),一步一步教你實(shí)現(xiàn)Periscope點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Android Studio 通過(guò)登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程
SQLite是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫(kù)中。這篇文章主要介紹了Android Studio 通過(guò)登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程,需要的朋友可以參考下2018-09-09Android 實(shí)現(xiàn)界面刷新的幾種方法
這篇文章主要介紹了Android 實(shí)現(xiàn)界面刷新的相關(guān)資料,這里提供了幾種方法及實(shí)例代碼,具有一定的參考價(jià)值,需要的朋友可以參考下2016-11-11關(guān)于Android中drawable必知的一些規(guī)則
drawable這個(gè)東西相信大家天天都在使用,每個(gè)Android開發(fā)者都再熟悉不過(guò)了,但可能還有一些你所不知道的規(guī)則,那今天我們就來(lái)一起探究一下這些規(guī)則。2016-08-08