Why use Handler in Android?
First: let us know what a stream is:
- Multitasking topics help
- Themes can be taught as a mini-process running in the main process.
- Streams allow at least parallel execution of an appearance
Second: tell us about the application:
- When the Android application is launched, the runtime will create one main thread, this main thread will take care of the execution of all components in android
Android UI-Toolkit is not thread safe
- As was said, there are many components in the main Android thread, now suppose that one of the components takes a long time to execute, then this makes the main thread inactive and it will show the application is not responding
- Subflows cannot directly manipulate the application (main) thread in android
- The handler acts as an interface and collects messages from sub-threads and update the main thread of the application one by one as a message, the thread handlers are implemented in the main thread.
Handler Class:
- For multithreading, we will use a handler class that comes from the
android.os.Handler package - Each thread is processed by one instance of the handler class.

- As you can see from the above figure, each thread is processed by one instance of the Handler class.
- threads interact with each other using messages
- This handler class helps maintain some synchronization of b / w streams, allowing them to work together to achieve multithreading.
Handler instance is running
Handler handlerObject = new Handler();
The final part of using a handler is to use the Runnable Interface:
- the handler class uses the runnable interface to implement multithreading
- We override the run method to execute the thread a certain amount of time.
Class NameOfClass implements Runnable { Public void run() {
Unification of all
//Create handler in the thread it should be associated with //in this case the UI thread final Handler handler = new Handler(); Runnable runnable = new Runnable() { public void run() { while(running){ //Do time consuming stuff //The handler schedules the new runnable on the UI thread handler.post(new Runnable() { //Ex.. using progressbar to set the pogress //Updating the UI is done inside the Handler }); } } }; new Thread(runnable).start();
Devrath
source share