你的第一个问题的答案:你的广播接收器被调用两次是因为
你已经添加了两个
<intent-filter>
- 网络连接更改:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
- WiFi状态更改:
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
只需使用一个:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />。
它将仅响应一个动作,而不是两个。有关更多信息,请参见此处。
回答第二个问题(如果互联网连接可用,你希望接收方只打一次电话):
你的代码是完美的;你仅在互联网可用时通知。
更新
如果只想检查移动设备是否已与Internet连接,则可以使用此方法检查连接。
public boolean isonline(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); //should check null because in airplane mode it will be null return (netInfo != null && netInfo.isConnected());}


