[Android] ブロードキャストでの LiveData の使用

前文

ブロードキャストで Livedata を使用すると、メッセージをブロードキャストするときに Livedata を使用してメッセージを監視および取得するときに、アクティビティにブロードキャストを登録する必要がなくなります。これは、複数のアクティビティでブロードキャスト要件をリッスンするのに特に適しています。

プロジェクトの要件

監視接続ブロードキャストがあり、監視接続ステータスが変化すると、複数のアクティビティが監視変更のステータスを取得し、ステータスに応じて対応する処理を実行する必要があります。

機能実現

まずApplication内側

    override fun onCreate() {
    
    
        super.onCreate()
        instance = this
        br = WatchConnectLiveData.getInstance()
    }


    override fun onTerminate() {
    
    
        br.unRegisterReceiver()
        super.onTerminate()
    }

    @SuppressLint("StaticFieldLeak")
    companion object {
    
    

        @SuppressLint("StaticFieldLeak")
        private lateinit var instance: MyApplication
        fun getInstance(): MyApplication {
    
    
            return instance
        }

        private lateinit var br: WatchConnectLiveData

        fun getBroadcastLiveData(): WatchConnectLiveData {
    
    
            return br
        }
    }

WatchConnectLiveDataLiveDataを継承するクラスを作成します。

public class WatchConnectLiveData extends LiveData<Integer> {
    
    

    private static WatchConnectLiveData instance;
    private BroadcastReceiver broadcastReceiver;

    public synchronized static WatchConnectLiveData getInstance() {
    
    
        if (instance == null) {
    
    
            instance = new WatchConnectLiveData();
        }
        return instance;
    }

    private WatchConnectLiveData() {
    
    
        broadcastReceiver = new BroadcastReceiver() {
    
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
    
                if (intent != null) {
    
    
                    int state = intent.getIntExtra("state", -1);
                    Log.e("手表连接状态", "此时状态为:" + state);
                    postValue(state);
                }
            }
        };
        // 注册广播接收器
        registerReceiver();
    }

    @Override
    protected void onActive() {
    
    
        Log.e("TAG", "当有观察者时调用此方法");
        if (broadcastReceiver == null) {
    
    
            registerReceiver();
        }
    }

    @Override
    protected void onInactive() {
    
    
        Log.e("TAG", "观察者没了");
    }

    private void registerReceiver() {
    
    
        // 注册广播接收器
        IntentFilter intentFilter = new IntentFilter("com.xxxxx.xxxxx.action.CONNECTION_STATE_CHANGED");
        MyApplication.Companion.getInstance().registerReceiver(broadcastReceiver, intentFilter);
    }

    public void unRegisterReceiver() {
    
    
        if (broadcastReceiver != null) {
    
    
            MyApplication.Companion.getInstance().unregisterReceiver(broadcastReceiver);
            broadcastReceiver = null;
        }
    }
}

このようにして、プログラム開始時にグローバルオブザーバーを作成し、ブロードキャストデータが変化した際に、ライブデータを通じて状態変化を取得することができます。

MyApplication.getBroadcastLiveData().observe(viewLifecycleOwner) {
    
    
    Log.e(TAG, "状态===>>> $it <<<===")
    //根据相应的状态进行数据处理和操作
    
}

おすすめ

転載: blog.csdn.net/qq_43358469/article/details/131582994