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>
====================================================================================================================
Android判断网络状态是否断开
Android 判断网络状态是否断开,不多说了,看代码吧!
/**
* 判断网络状态是否可用
* @return true: 网络可用 ; false: 网络不可用
*/
public boolean isConnectInternet() {
ConnectivityManager conManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null ){ // 注意,这个判断一定要的哦,要不然会出错
return networkInfo.isAvailable();
}
return false ;
}
最后别忘要在加上
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
这个权限哦!