2012-09-26 124 views
0

我試圖讓廣播接收機聽互聯網連接,然後連接時做一些任務。廣播接收機監控互聯網連接不工作

當我在我的真實設備上禁用或啓用我的WIFI或禁用/啓用仿真器中的數據訪問時,我沒有收到任何通知。操作CONNECTIVITY_CHANGE不再受支持。

public class InternetConnectivityReceiver extends BroadcastReceiver { 
Context context; 
@Override 
public void onReceive(Context context, Intent intent) { 
    this.context = context; 

    Log.i(TAG, "Internet Conenction State Changed"); 
} 
} 

清單

<application 
    android:icon="@android:drawable/arrow_down_float" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 



     <receiver android:name=".InternetConnectivityReceiver"> 
      <intent-filter> 
       <action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"> 

       </action> 
      </intent-filter> 
     </receiver> 
...... 
</application> 
+0

可能的重複:http://stackoverflow.com/questions/3307237/how-can-i-monitor-the-network-connection-status-in-android –

+0

它不是重複的先生。 – MSaudi

+0

你有互聯網權限嗎? – Lucifer

回答

0

Developer documentation for ConnectivityManager說:

BACKGROUND_DATA_SETTING_CHANGED

這個常數已被棄用。從ICE_CREAM_SANDWICH開始, 背景數據的可用性取決於幾個組合因子,並且此 廣播不再發送。

嘗試傾聽"android.net.conn.CONNECTIVITY_CHANGE"代替。

+0

哦,我想我錯了,或者寫錯了一些地方。我認爲android.net.conn.CONNECTIVITY_CHANGE是不合時宜的,因爲我在自動完成中找不到它。非常感謝,這工作。 – MSaudi