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