0
飛行機モードのON/OFF時には、スイッチボタンの状態を変更する必要があります。 ブロードキャストレシーバのMainActivity(レイアウト)で宣言されているスイッチボタン
は、私は私がスイッチボタンを宣言している主な活動をしていると私はレシーバー
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>
私はどのようにメソッドをonMessageEventないことを1つの事を理解していませんでした私は仕事を完了したthoと呼ばれる。 –
複数のイベントをどのように処理できますか?感謝 –
'isAirplaneModeOn'が主なアクティビティに達していて(かつ 'onStart()'を越えていて)、( 'onStop()'に達していない場合、 'onMessageEvent(SwitchEvent event)'が呼び出されます。したがって、 'onMessageEvent(SwitchEvent event)'は、アクティビティが「生きている」場合にのみ呼び出されます。 複数のイベントを処理するために、この例で見たように、必要な数だけイベントクラスを作成して使用できます。私が使用するイベントクラスの数を減らすために、イベントクラスのコンストラクタにパラメータを追加し、suscribersメソッドではこれらのパラメータに従って条件を設定します。 –