BroadcastReceiver将收到" android.net.wifi.STATE_CHANGE"当应用程序启动时

时间:2018-03-22 08:58:13

标签: android android-broadcastreceiver

" android.net.wifi.STATE_CHANGE",我认为此意图操作将在WiFi关闭/断开/连接时广播。

当我启动我的应用程序时,接收器将收到" android.net.wifi.STATE_CHANGE"这个动作,但我的设备仍然连接WiFi。 WiFi状态没有变化。

我使用adb logcat filiter" android.net.wifi.STATE_CHANGE",我没有看到任何系统广播,但启动我的应用程序将显示已收到。

我在Android 5.1 / 6.0 / 7.0上测试

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WifiBroadcastReceiver wifiBroadcastReceiver = new WifiBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        filter.addAction("android.net.wifi.STATE_CHANGE");
        registerReceiver(wifiBroadcastReceiver, filter);
    }
}

class WifiBroadcastReceiver extends BroadcastReceiver {
    private String TAG = "WifiBroadcastReceiver";
    @Override
    public void onReceive(Context paramContext, Intent paramIntent) {
        //When Start App, it always received "android.net.wifi.WIFI_STATE_CHANGED" and "android.net.wifi.STATE_CHANGE".
        if ("android.net.wifi.STATE_CHANGE".equals(paramIntent.getAction())) {
            Log.e(TAG, paramIntent.getAction());
            WifiInfo wifiInfo = paramIntent.getParcelableExtra("wifiInfo");
            //It will print the current connected SSID
            Log.e(TAG, wifiInfo.getSSID());
        }
    }
}

0 个答案:

没有答案