2010-02-19 41 views
38

我需要接受網絡操作,如連接,斷開等我使用用於此目的的廣播接收器的網絡廣播。誰能告訴我,我需要它的意圖行動,以捕獲網絡事件,現在按我搜索互聯網上我使用android.net.ConnectivityManager.CONNECTIVITY_ACTION意向行動

這裏是我的廣播接收器類:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class NetworkStateReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 


    if (intent.getAction().equals(
      android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { 

     // do something.. 
    } 
} 
} 

,我還增加了允許訪問網絡狀態:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

這裏是如何我已經宣佈這個類清單文件

<receiver class=".NetworkStateReceiver" android:name=".NetworkStateReceiver"> 
    <intent-filter> 
      <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> 
    </intent-filter> 
</receiver> 

請建議我在正確的意圖行動,如果我錯了,或有任何其他方式捕捉網絡事件。

回答

51

這裏有一個工作示例:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

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

public class ConnectivityReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(ConnectivityReceiver.class.getSimpleName(), "action: " 
       + intent.getAction()); 
    } 

} 
+0

根據我的測試,我不需要任何許可接收廣播,無論wlan現在是上/下還是3g現在上/下。我有點困惑,什麼是android.net.conn.CONNECTIVITY_CHANGE然後好? – 2011-05-11 18:08:59

+0

如果你再看看,你會發現CONNECTIVITY_CHANGE是不是權限,它的註冊意向接收器(您可以在代碼或者做)。 – Hamid 2012-04-12 12:25:53

+0

答案看起來與問題相同。那麼爲什麼它在答案之前不工作? – 2012-07-20 21:19:54

5

yanchenko的答案是非常有用的,我只是簡化了一點點獲得連接狀態,請如下修改的onReceive:

public class ConnectivityReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(ConnectivityReceiver.class.getSimpleName(), "action: " 
       + intent.getAction()); 
     MyConstants.IS_NETWORK_AVAILABLE = haveNetworkConnection(context); 
     //IS_NETWORK_AVAILABLE this variable in your activities to check networkavailability. 

    } 


    private boolean haveNetworkConnection(Context context) { 
     boolean haveConnectedWifi = false; 
     boolean haveConnectedMobile = false; 

     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
     for (NetworkInfo ni : netInfo) { 
      if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
       if (ni.isConnected()) 
        haveConnectedWifi = true; 
      if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
       if (ni.isConnected()) 
        haveConnectedMobile = true; 
     } 
     return haveConnectedWifi || haveConnectedMobile;  
    } 
} 
+0

,你知道android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY?自API級別1以來,這是額外的Intent中的一個額外布爾值,它告訴您是否存在連接。 – 2012-12-15 17:01:23

+1

關於昨天的評論,我注意到它並不精確。看起來EXTRA_NO_CONNECTIVITY,至少在某些情況下,只有在沒有連接時纔會添加到Intent extras中。因此,與getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,FALSE)訪問它應該產生一個布爾值,如果沒有連接可言,和假,如果有任何連接這是真的。此外,這個答案中的代碼是**反模式**,因爲分析是有限的。它不會檢測藍牙共享,USB共享或LAN連接。不要這樣做。 – 2012-12-17 09:40:38

+2

歸因於該代碼,看到拉維忘了:http://stackoverflow.com/a/4239410/182653 – paulw1128 2013-05-22 17:48:26