I applied observable to this method, but after calling this method, it is saying too much work on the main thread. Any help is appreciated
fun isBatteryHealthGood(): Observable<Boolean> {
var count = 0
intent = context.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
while (batteryStatus == null && count < maxCount) {
Thread.sleep(1000)
count++
}
return Observable.just(batteryStatus == BatteryManager.BATTERY_HEALTH_GOOD)
}
My solution is to avoid usage of Thread.sleep() by using interval operator I think you can ommit observable in isBatteryHealthGood().
Just return boolean like this:
//a simple function works here, no need
fun isBatteryHealthGood(): Boolean {
return batteryStatus == BatteryManager.BATTERY_HEALTH_GOOD
}
and finally subscribe like this:
Observable.
interval(1000, TimeUnit.MILLISECONDS)
take(maxCount) //place max count here
.map { _ -> isBatteryHealthGood() }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.maintThread())
.subscribe {
batterystat ->
//do what you need
}
PS: you should register receiver only once
intent = context.registerReceiver(broadCastReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))