56.7 Implementing a Thread Handler
Thread handlers are implemented in the main thread of an application and are primarily used to make updates to the user interface in response to messages sent by other threads running within the application’s process.
Handlers are subclassed from the Android Handler class and can be used either by specifying a Runnable to be executed when required by the thread, or by overriding the handleMessage() callback method within the Handler subclass which will be called when messages are sent to the handler by a thread.
For the purposes of this example, a handler will be implemented to update the user interface from within the previously created thread. Load the MainActivity.java file into the Android Studio editor and modify the code to add a Handler instance to the activity:
.
.
import android.os.Handler;
import android.os.Message;
import android.os.Looper;
public class MainActivity extends AppCompatActivity {
.
.
...