应用关闭时广播接收器无法正常工作(来自世界各地)

时间:2017-10-26 13:21:13

标签: android broadcastreceiver intentfilter

我的应用不是 wifi状态更改(已连接或已断开连接)时接收广播。当app界面看不到

时,它正在工作

我的清单: enter image description here

我创建了BroadcastReceiver的子类并在manifest中声明,不需要注册BroadcastReceiver。

public class WifiReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {


    String action = intent.getAction();
        if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            NetworkInfo.State detailedState = networkInfo.getState();


            if (detailedState == NetworkInfo.State.CONNECTED && networkInfo.isConnected()) {
                   ---do something----
                }
                else if(detailedState == NetworkInfo.State.DISCONNECTED) {

                    ---do something----
                }

        }

    }

}

0 个答案:

没有答案