I am viewing the source code for the HandlerThread class and noticed the class contains a data member mHandler. mHandler is an instance of type Handler.
private @Nullable Handler mHandler;
There is an accessor method for getting the instance of the private member defined in the HandlerThread class as well.
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}
I tried to get a reference to the mHandler member by calling the method on my HandlerThread reference. AndroidStudio does not allow a method call getThreadHandler on a HandlerThread instance. The method appears to be public just like the other public methods that can be called on a HandlerThread instance. Does anyone know why the public method getThreadHandler can not be called on a HandlerThread instance. Am I just missing something in the class/method/member definition? My code for instantiating a HandlerThread instance and calling methods is shown below.
...
import android.os.*
...
class SearchActivity : AppCompatActivity(), View.OnClickListener {
...
private val backgroundWork = HandlerThread("BackgroundApiCall")
private lateinit var backgroundHandler: Handler
...
override fun onCreate(savedInstanceState: Bundle?) {
...
backgroundWork.start()
backgroundHandler = = Handler(backgroundWork.looper)
// an attempt here to call getThreadHandler will not work
val handler = backgroundWork.threadHandler // or .getThreadHandler() for our Java friends
...
override fun onDestroy() {
super.onDestroy()
backgroundWork.quit()
}
override fun onClick(_searchBtn: View?) {
backgroundHandler.post(requestWork)
}
....
Note: I am only posting the code that is relative to the problem at hand and not the entire class.