public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, ”Active Network Type : “ + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, ”Mobile Network Type : “ + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
}
}
}
<!– Needed to check when the network connection changes –>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
<receiver android:name=”com.blackboard.androidtest.receiver.ConnectionChangeReceiver”
android:label=”NetworkConnection”>
<intent-filter>
<action android:name=”android.net.conn.CONNECTIVITY_CHANGE”/>
</intent-filter>
</receiver>
判断网络的状态断开与否
最新推荐文章于 2021-01-26 23:40:33 发布
本文介绍了一个用于监听Android设备网络连接状态变化的广播接收器示例。该接收器能够捕获到网络状态的变化,并通过Toast消息展示当前活动网络类型及移动网络类型。为了实现此功能,需要在AndroidManifest.xml中注册该接收器并申请ACCESS_NETWORK_STATE权限。
摘要由CSDN通过智能技术生成