Android wifi 或 3g 网络状态改变时的 BroadcastReceiver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10733121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BroadcastReceiver when wifi or 3g network state changed
提问by J1and1
I have an app which updates the database whenever the phone is connected to WiFi. I have implemented a Service
and BroadcastReceiver
which will run the Service
(it will tell me what network is in use), but the problem is I don't know what to add in the manifest
file to start BroadcastReceiver
when the network state changes or when it connects to some kind of network
我有一个应用程序,只要手机连接到 WiFi,它就会更新数据库。我实现了一个Service
和BroadcastReceiver
这将运行Service
(它会告诉我是什么用网络),但问题是我不知道是什么的在添加manifest
文件开始BroadcastReceiver
当它连接到某种当网络状态发生变化或网络的
回答by nullpotent
You need
你需要
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
In your receiver
tag.
在你的receiver
标签中。
Or if you want more control over it, before registering BroadcastReceiver
set these up:
或者,如果您想对其进行更多控制,请在注册之前进行BroadcastReceiver
设置:
final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(yourReceiver, filters);
WIFI_STATE_CHANGED
WIFI_STATE_CHANGED
Broadcast <intent-action>
indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.
<intent-action>
指示 Wi-Fi 已启用、禁用、启用、禁用或未知的广播。一个额外的提供这个状态作为一个int。如果可用,另一个额外的提供以前的状态。
STATE_CHANGE
STATE_CHANGE
Broadcast <intent-action>
indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String
<intent-action>
指示 Wi-Fi 连接状态已更改的广播。一个额外的以 NetworkInfo 对象的形式提供新状态。如果新状态是 CONNECTED,额外的附加项可以提供接入点的 BSSID 和 WifiInfo。作为字符串
Also, you'll need to specify the right permissions inside manifest
tag:
此外,您需要在manifest
标签内指定正确的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
To check connectivity, you can use ConnectivityManager
as it tells you what type of connection is available.
要检查连接,您可以使用ConnectivityManager
as 它告诉您可用的连接类型。
ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
回答by Dantalian
This is what I do to get notified when connection has changed.
You define a BroadCastReceiver
to receive the broadcast.
这就是我在连接发生变化时收到通知的方法。您定义一个BroadCastReceiver
来接收广播。
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = wifi != null && wifi.isConnectedOrConnecting() ||
mobile != null && mobile.isConnectedOrConnecting();
if (isConnected) {
Log.d("Network Available ", "YES");
} else {
Log.d("Network Available ", "NO");
}
}
}
You also have to define that BroadcastReceiver
in your manifest file and filter by ConnectivityChange
.
您还必须BroadcastReceiver
在清单文件中定义它并按ConnectivityChange
.
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Source here
来源在这里
回答by Nicolás Carrasco
To complement @Dantalian's answer. Starting with Android Nougat you should not declare your receiver on your Manifest because it will not receive the CONNECTIVITY_ACTION
broadcast. You should instead register your receiver using the Context.registerReceiver(Receiver, Intent)
method.
补充@Dantalian 的回答。从 Android Nougat 开始,您不应在 Manifest 中声明您的接收器,因为它不会接收CONNECTIVITY_ACTION
广播。您应该改为使用该Context.registerReceiver(Receiver, Intent)
方法注册您的接收器。
Link to the source here
链接到这里的源
回答by Parag Chauhan
You should make an BroadcastReceiver
that will be triggered when the connectivity status has changed :
您应该BroadcastReceiver
在连接状态发生变化时触发:
Here Same question How to check the Internet Connection periodically in whole application?
Here 同样的问题如何在整个应用程序中定期检查 Internet 连接?
回答by N. Kyalo
Declare the intent filter to pass to the broadcastreceiver
声明要传递给广播接收器的意图过滤器
IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);
In the broadcast receiver, check for EXTRA_WIFI_STATE;
在广播接收器中,检查 EXTRA_WIFI_STATE;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action)
{
case WifiManager.WIFI_STATE_CHANGED_ACTION:
int extra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,0);
if(extra==WifiManager.WIFI_STATE_ENABLED)
{
}//...else WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLING
break;
}
}
};