2012-09-26 203 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