Explained - Android Looper and handler

Explained - Android Looper and handler - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Explained - Android Looper and handler, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article and, article android, article Explained, article handler, article Looper, article Tutorial, we can write this you understand. Well, happy reading.

Title : Explained - Android Looper and handler
link : Explained - Android Looper and handler

Baca juga


Explained - Android Looper and handler

Hi guys.

code tutor here.

and my name isAnil Deshpande This is a continuation of the previous video.

Where were talking about the updation of the UI.

as I had told in the previous video.

But updating the UI from a separate thread.

Is little bit tricky.

Let us see what happens if we try to updatethe UI thread from a separate thread.

I have slightly modified the design.

In thisparticular case.

I have kept a variable called count.

whichI keep on increasing in the while loop.

I have now added a text view and i will betrying to update that particular text view every time the thread.

increments the valueof the counter.

So just for the benefit.

I will show youhow this particular app runs.

with the new count variable in place.

So you can see.

the app starts.

the threadinitially is one.

and when i click on the start thread button.

The counter will keep on increasing.

You can see that the thread id is also different.

And on click on the stop thread button.

the count will stop at 10.

Now instead ofoutputting the value of count to the log.

I want to update it to the text view.

So i have instance of a text view here.

And this particular text view have to be set with the new count value.

So let me do that.

text view dot set textand count.

I can just update it with count value.

So let me re-run this.

the thread had notyet started.

I will click on the button.

and on click on that.

it should try to updatethe UI.

but the app crashes.

If you see the error.

there is a fatal exception.

And if you further go into details.

it will say that.

only the original thread.

thatcreated a view hierarchy can touch this particular view.

what it is basically saying is.

text viewwas created on a separate thread.

which was a main thread.

this particular countvalue.

is getting executed or updated on a separate thread.

This particular thread can not update theview.

i.

E.

text view.

so how do i solve this particular problem.

for that let us keep these things aside forfew minutes.

and understand few more concepts related to message queue.

Android maintains a queue which is calledas a message queue.

which is always populated with the tasks.

that need to update the UI.

Thistask could be as simple as rendering a button.

or doing something on click of a button.

Anythingfor that matter.

So this message queue is basically a queueof tasks.

that need to be executed one after another.

Now the question is.

who is going to executethe tasks.

that are there in this particular queue? For that we have a another thread.

whichis under constant loop.

which keeps on executing any task that is there in that particularmessage queue.

Since this thread is under constant loop.

It is called as looper.

In our case.

since we want to update theUI.

the UI thread itself is under a constant loop.

and it is trying to execute any task.

That you put in this particular message queue.

We have a thread.

and thread wants to puta task.

in this particular queue.

But this is not possible to do that.

threadcan not directly put the task in that particular queue.

For that we use something called as Handler.

Thread gives the task.

that it wants to put in the queue to the handler.

and handlerwill be having reference to the message queue.

and it will place that particular task inthe message queue.

As per Android.

the task should be of typeRunnable instance.

This Runnable instance will be put in this particular queue.

As longas queue contains the task.

the looper in this particular case.

the main thread.

Will keep on executing the tasks that are there in the message queue.

Let us see this in execution.

This particular count variable cannot be updateddirectly like this.

So what I will do is.

I will create a instance of Handler.

ThisHandler I will initialize here.

new handler.

and this new Handler should have a referenceto the message queue of the UI thread.

So for that what I can do is.

get applicationcontext () dot get main looper.

So this will make sure that this handler.

Will always a reference to my main looper which is also a main thread or Ui thread.

Now what I will do it handler dot post.

I am posting a task.

the task should of type Runnable.

So let me create a Runnable instancehere.

and inside the run method.

textview dot set text and here I can now put the valueof count.

so let me run this.

I will click on the button.

You can now seethat the text view is getting updated.

Because now the updation task.

is being puton the message queue.

which in turn is getting executed by a looper thread.

which is a mainthread.

Another simpler way of updating the UI.

inthat case I don't have to write all these things.

What I can do is directly.

text view postnew Runnable.

here.

text view thread count set count.

this is also doing the same thing.

but itis you can say.

kind of.

just short had notation.

of achieving the same behavior.

Let me run this.

start the thread.

you cansee that we are getting the same behavior.

So if at all you want to update the UI froma separate thread.

it has to be done through the concept of looper and handler.

That brings us to the end of this particularvideo.

comment down below the video.

on what new concepts you would like to learn.

Subscribe to the Channel.

Take care bye.



Thus Article Explained - Android Looper and handler

That article Explained - Android Looper and handler This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Explained - Android Looper and handler the link address http://svedda79.blogspot.com/2016/04/explained-android-looper-and-handler.html

0 Response to "Explained - Android Looper and handler"

Post a Comment