[Android] The use of LiveData in broadcasting

preamble

Use Livedata in broadcasting, so that when broadcasting messages, using Livedata to observe and obtain messages does not need to register broadcasting in the Activity, which is especially suitable for listening to a broadcasting requirement in multiple Activities.

project requirements

Now there is a watch connection broadcast. When the watch connection status changes, multiple activities need to obtain the status of the watch change, and then perform corresponding processing according to the status.

function realization

first Applicationinside

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

We create WatchConnectLiveDataa class that inherits from LiveData

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

In this way, when the program starts, we create a global observer. When the broadcast data changes, we can obtain the state change through livedata.

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

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131582994