每次调用时,都会从BroadcastReceiver接收MainActivity中的数据

时间:2014-05-15 11:12:35

标签: java android

我开发了一款使用BroadcastReceiver类来检测Wi-Fi网络更改的Android应用。在这个接收器中,我得到了网络的ssid。现在我希望接收器获得的这个ssid应该在每次调用接收器时传递给MainActivity,以便可以在MainActivity中完成一些ssid比较。 怎么办呢?

1 个答案:

答案 0 :(得分:0)

覆盖onReceive()方法

final NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                super.onReceive(context, intent);
                         // put your code here to check wi-fi and to get ssid value
            }
        };
        final IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        wifibox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                if (isChecked) {
                    registerReceiver(networkChangeReceiver, filter);
                } else {
                    unregisterReceiver(networkChangeReceiver);
                }

            }
        });