Android中判断网络是否可用的代码分享
程序员文章站
2022-03-23 14:04:43
package cn.hackcoder.beautyreader.broadcast;
import android.content.broadcastre...
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 { /** * 判断是否有网络连接 * @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网络是否可用 * @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网络是否可用 * @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; } /** * 获取当前网络连接的类型信息 * @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; } /** * 获取当前的网络状态 :没有网络0:wifi网络1:3g网络2:2g网络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; } }
注册:
<receiver android:name=".broadcast.networkstatusreceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.net.conn.connectivity_change" /> </intent-filter> </receiver>