简体   繁体   English

当wifi或3g网络状态改变时,BroadcastReceiver

[英]BroadcastReceiver when wifi or 3g network state changed

I have an app which updates the database whenever the phone is connected to WiFi. 我有一个应用程序,只要手机连接到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 我已经实现了一个ServiceBroadcastReceiver来运行Service (它会告诉我正在使用什么网络),但问题是我不知道在manifest文件中添加什么来在网络状态发生变化时启动BroadcastReceiver它连接到某种网络

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. 广播<intent-action>表示已启用,禁用,启用,禁用或未知Wi-Fi。 One extra provides this state as an int. 一个额外提供此状态为int。 Another extra provides the previous state, if available. 如果可用,另一个额外提供先前的状态。

STATE_CHANGE STATE_CHANGE

Broadcast <intent-action> indicating that the state of Wi-Fi connectivity has changed. 广播<intent-action>表示Wi-Fi连接状态已更改。 One extra provides the new state in the form of a NetworkInfo object. 一个额外的以NetworkInfo对象的形式提供新状态。 If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. 如果新状态是CONNECTED,则额外的附加可以提供接入点的BSSID和WifiInfo。 as a String 作为一个字符串

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因为它会告诉您可用的连接类型。

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);

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 来源于

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? 这里同样的问题如何在整个应用程序中定期检查Internet连接?

To complement @Dantalian's answer. 补充@Dantalian的回答。 Starting with Android Nougat you should not declare your receiver on your Manifest because it will not receive the CONNECTIVITY_ACTION broadcast. 从Android Nougat开始,您不应在您的Manifest上声明您的接收器,因为它不会收到CONNECTIVITY_ACTION广播。 You should instead register your receiver using the Context.registerReceiver(Receiver, Intent) method. 您应该使用Context.registerReceiver(Receiver, Intent)方法注册接收Context.registerReceiver(Receiver, Intent)

Link to the source here 链接到这里的来源

Declare the intent filter to pass to the broadcastreceiver 声明意图过滤器以传递给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;
        }

    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM