0

I have a task in which i have to change state of Switch button when airplane mode is ON/OFF.

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

Receiver

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

<?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>
Omar Saleem
  • 137
  • 1
  • 3
  • 11

1 Answers1

0

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());
        }
    }
}

The activty's class:

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
    }
}

The event's class:

Public class SwitchEvent{
    public SwitchEvent(){
        //empty
    }
}
Maxime Claude
  • 965
  • 12
  • 27
  • i did not understand 1 thing that how does onMessageEvent method called tho i have completed task. – Omar Saleem Dec 11 '17 at 19:59
  • 'onMessageEvent(SwitchEvent event)' will be called when 'isAirplaneModeOn' if the main activity is has reached (and crossed 'onStart()' ), and hasn't reached ( 'onStop()' ). So 'onMessageEvent(SwitchEvent event)' will be called only if the activity is "alive". Now to handle multiple events, you can create as many event classes as you wish and just used them as you saw in this example. What I do to decrease the number of Event classes that I use, I add parameters in the constructors of my event classes, and in the suscribers methods I set conditions according to these parameters. – Maxime Claude Dec 11 '17 at 20:21
  • Thank you very much for saving my day :) – Omar Saleem Dec 11 '17 at 21:08