2012-03-20 49 views

回答

2

您可以列出特定的意圖。定義一個接收器這樣,

<receiver android:name=".NetworkReceiver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter> 
    </receiver> 

,並在您接收,檢查網絡狀態一樣,

@Override 
public void onReceive(Context ctx, Intent intent) { 
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 
     NetworkInfo info = intent 
       .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
     String typeName = info.getTypeName(); 
     String subtypeName = info.getSubtypeName(); 
        boolean available = info.isAvailable(); 
        // etc 
     } 
} 
+0

感謝....這項工作給我。 – 2012-03-20 19:12:32

0
public boolean online() { 
    ConnectivityManager manager = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo info = manager.getActiveNetworkInfo(); 
    return info != null && info.isConnectedOrConnecting(); 
}