how to broadcast Receiver and MVVM?

后端 未结 2 1908
星月不相逢
星月不相逢 2021-01-27 19:23

my manifest


            
                

        
相关标签:
2条回答
  • 2021-01-27 19:40
    1. define base class for LiveData of BroadcastReceiver
    public class ReceiverLiveData<T> extends LiveData<T> {
    
        private final Context context;
        private final IntentFilter filter;
        private final BiFunction<Context, Intent, T> mapFunc;
    
        public ReceiverLiveData(Context context, IntentFilter filter, BiFunction<Context, Intent, T> mapFunc) {
            this.context = context;
            this.filter = filter;
            this.mapFunc = mapFunc;
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            context.unregisterReceiver(mBroadcastReceiver);
        }
    
        @Override
        protected void onActive() {
            super.onActive();
            setValue(mapFunc.apply(context, new Intent()));
            context.registerReceiver(mBroadcastReceiver, filter);
        }
    
        private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                setValue(mapFunc.apply(context, intent));
            }
        };
    }
    
    1. declare your variable in ViewModel, for example battery change intent
        public static class ViewModel extends AndroidViewModel {
            public final LiveData<Intent> batteryIntentLiveData = new ReceiverLiveData<>(getApplication(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED), (context, intent) -> intent);
            public final LiveData<NetworkInfo> activeNetworkInfoLiveData = new ReceiverLiveData<>(getApplication(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION), ((context, intent) -> ((ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo()));
    
            public ViewModel(@NonNull Application application) {
                super(application);
            }
        }
    
    1. initialize ViewModel in Activity::onCreate
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
            binding.setViewModel(new ViewModelProvider(this).get(ViewModel.class));
            binding.setLifecycleOwner(this);
            setContentView(binding.getRoot());
        }
    
    1. use batteryIntentLiveData & activeNetworkInfoLiveData in your DataBinding layout
    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    
        <data>
    
            <variable
                name="viewModel"
                type="com.xxx.receiverbindingtest.MainActivity.ViewModel" />
    
            <import type="android.os.BatteryManager" />
    
            <import type="android.view.View" />
        </data>
    
        <LinearLayout 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"
            android:orientation="vertical"
            tools:context=".MainActivity">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{String.valueOf(viewModel.batteryIntentLiveData.getIntExtra(BatteryManager.EXTRA_LEVEL, 0))}" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text='@{viewModel.batteryIntentLiveData.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0 ? "Charging" : "Discharging"}' />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text='@{String.valueOf(viewModel.activeNetworkInfoLiveData.state)}'
                android:visibility="@{viewModel.activeNetworkInfoLiveData.connectedOrConnecting ? View.VISIBLE : View.GONE}" />
    
        </LinearLayout>
    </layout>
    
    0 讨论(0)
  • 2021-01-27 19:40

    Use your Application class. either have a LiveData in it or a reference to a shared ViewModel. You can access Application using context.getApplicationContext() from NetworkChangeReceiver

    0 讨论(0)
提交回复
热议问题