我有不同的活动,每个活动都有一些片段,主要活动除外。所有这些片段都包含一些操作一些电器的开关按钮,这是一个基于IOT的家庭自动化应用程序。主要活动有一个语音识别模块,用于监听语音命令,通过该语音命令,用户可以使用语音命令打开/关闭开关按钮,我希望从主活动中我可以控制位于不同活动片段上的所有开关按钮。我怎么能这样做,我可以将数据从主活动传递到其他活动的片段,以便我可以操作主要活动的切换按钮吗?任何人都可以帮我一个忙,并编写一个代码,以便我可以将主活动中的数据传递给名为“BedRoomFanFragment”的片段,该片段是名为“BedRoomActivity”的活动片段。请帮助我。谢谢 这是我想要发布数据的主要活动
switch (requestCode) {
case 10:
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txvResult.setText(result.get(0));
String dat;
dat = txvResult.getText().toString();
if(dat.equals("on")){
T.setText("ON");
// when user says "on" it posts data to BedRoomFanFragment
}
else if(dat.equals("off")){
T.setText("OFF");
}
else{}
}
break;
}
}
这是我想要接收数据以打开BedFan1开关按钮的片段
public class BedRoomFanFragment extends Fragment {
SwitchCompat BedFan1;
public static final String SHARED_PREFS = "sharedprefs";
public static final String BEDFAN1 = "bedfan1";
private boolean switchChange;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bed_room_fan, container, false);
BedFan1=(SwitchCompat) rootView.findViewById(R.id.bedFan1);
loadData();
updateViews();
BedFan1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(BEDFAN1,BedFan1.isChecked());
editor.apply();
if(!isChecked){
((BedRoomActivity)getActivity()).sendData("x");
}else if(isChecked){
((BedRoomActivity)getActivity()).sendData("y");
}
}
});
return rootView;
}
public void loadData(){
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
switchChange = sharedPreferences.getBoolean(BEDFAN1, false);
}
public void updateViews(){
BedFan1.setChecked(switchChange);
}
}
答案 0 :(得分:1)
创建interface
并将其添加到list
,并在发布数据时发送到interface
的所有实例。
虽然这是一个IOT/M2M
应用程序,但您应该专注于逻辑而不是将数据传递给视图。真棒libraries
(Otto,Eventbus)这使得它非常容易管理。
以下示例属于Otto。在fragment
subscribe
event
和您的post
event
。
public class BedRoomFanFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
bus.register(getActivity());
}
//this method will get called when you post an event from activity
@Subscribe public void homeApplianceEvent(int event) {
// TODO: React to the event somehow!
if(event == 0){
Log.d(“OFF”)
}
if(event == 1){
Log.d(“ON”)
}
}
Public class MainActivity extends Activity{
//this is an example but create a singleton
Bus bus = new Bus();
final int OFF =0;
final int ON = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
//post event like this
//this is to switch OFF
bus.post(OFF);
}
}
如果您想在event
中fragment
subscribe
发布Activity
,也适用