萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android 判斷網絡狀態

Android 判斷網絡狀態

 Android 判斷網絡狀態這一應用技巧在實際應中是比較重要的。那麼,在Android操作系統中,如何能夠正確的判斷我們所連接的網絡是否斷開恩?今天我們就針對這一應用技巧進行一個詳細的分析。

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判斷網絡狀態的具體應用方法就為大家介紹到這裡。

copyright © 萬盛學電腦網 all rights reserved