WEB开发网
开发学院手机开发Android 开发 android判断网络的状态断开与否 阅读

android判断网络的状态断开与否

 2010-09-14 23:53:00 来源:WEB开发网   
核心提示:public class ConnectionChangeReceiver extends BroadcastReceiver{@Overridepublic void onReceive( Context context, Intent intent ){ConnectivityManager connectivit

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>

Tags:android 判断 网络

编辑录入:coldstar [复制链接] [打 印]
赞助商链接