How to access Switch button that is declared in MainActivity(layout) in BroadcastReceivers(如何访问在 BroadcastReceivers 的 MainActivity(layout) 中声明的 Switch 按钮)
问题描述
我有一个任务,我必须在飞行模式打开/关闭时更改 Switch 按钮的状态.
I have a task in which i have to change state of Switch button when airplane mode is ON/OFF.
我有一个主要活动,我在其中声明了 Switch Button,我想从 BroadcastReceiver 类更改 Switch 的开/关状态
I have a main activity in which i declared Switch Button and i want to Change the state on/off of Switch from BroadcastReceiver Class
接收者
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
What Should i do ?
}
}
}
layout_main_activity
layout_main_activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.broadcastthroughmanifest.MainActivity">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="127dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
推荐答案
尝试使用这样的事件总线 one 将事件发布到活动.从活动中,当您收到事件时,您会更改开关的状态.像这样的东西:- 你的广播课:
Try using an event bus like this one to post the event to the activity. From the activity, when you receive the event, you change the state of the switch. Something like this: - Your broadcast class:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
EventBus.getDefault().post(new SwitchEvent());
}
}
}
活动的班级:
public class YourActivity{
//declare your switch
@Override
public void onCreate() {
setContentView(R.layout.layout_main_activity);
//initialize your switch
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(SwitchEvent event) {
//update your switch element
}
}
事件的类别:
Public class SwitchEvent{
public SwitchEvent(){
//empty
}
}
这篇关于如何访问在 BroadcastReceivers 的 MainActivity(layout) 中声明的 Switch 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何访问在 BroadcastReceivers 的 MainActivity(layout) 中声明的 Switch 按钮


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01