How to get switch value in Android?

后端 未结 3 1581
傲寒
傲寒 2021-02-07 09:24

I have placed a switch widget in Main Activity, I also have a second activity that extends BroadcastReceiver. I want to get the boolean state of switch widget in second activity

3条回答
  •  攒了一身酷
    2021-02-07 09:42

    To access the value of the switch, you need to do the following:

    ((Switch) findViewById(R.id.switch_id)).isChecked();
    

    BUT in a context of a BroadcastReceiver, you don't have really access to a layout, therefore you can't access the switch. You have to perform this ONLY within the Activity that inflates the layout that has the Switch.

    You may have a BroadcastReceiver registered programmatically within an Activity, this is the only way I see this mixture of concepts working.

提交回复
热议问题