6 years ago (2015-08-18)  Algorithm language Software development |   First to comment  109 
post score 0 times, average 0.0

Method one: (java habits, not recommended for use in android)

Just started to contact the android thread programming, the habit is like java, trying to solve the problem with the following code

Can realize the function, refresh UI interface.However, this is not possible because it violates the single-threaded model: Android UI operations are not thread-safe and these operations must be performed in the UI thread. Android uses threads to update the UI in several ways

Method two: (Thread+Handler)

After consulting the documentation and apidemo, I discovered that the common method is to use Handler to implement UI thread updates. The Handler handles UI updates based on received messages.The Thread thread issues a Handler message to notify the UI of the update.


Method 3: (java habits, not recommended)

In the Android platform, you need to perform the cycle-by-cycle method. You can use the TimerTask class that comes with Java. The TimerTask consumes less resources than the Thread. In addition to using the Timer that is provided by Android, the Timer timer is better. Solution. We need to import import java.util.Timer; and import java.util.TimerTask;


Method 4: (TimerTask + Handler)

In fact, this is not enough. This is related to Android's thread safety!Should achieve timer function through Handler!


Method 5: (Runnable + Handler.postDelayed(runnable,time) )

The UI is updated periodically in Android, usually using java.util.Timer, java.util.TimerTask, android.os.Handler combinations.In fact, Handler itself has provided regular functions.


Then call elsewhere


Summary of knowledge added:

Many newcomers to Android or Java development are still confused about Thread, Looper, Handler, and Message. Derived are HandlerThread, java.util.concurrent, Task, AsyncTask. Since the books and other materials on the market are not talking about these issues, Let's make a more systematic summary of this issue today.The Service, Activity, and Broadcast we create are all handled by a main thread. Here we can understand the UI thread.However, when operating some time-consuming operations, such as reading and writing of large files for I/O reading and writing, database operations and network downloading take a long time, in order not to block the user interface, an ANR response prompt window appears. At this time, we can consider using Thread thread to solve. For programmers who have worked on J2ME development, Thread is relatively simple. Create a rewrite run method anonymously and call the start method to execute it.Or inherited from the Runnable interface, but for the Android platform UI controls are not designed to be thread-safe type, so need to introduce some synchronization mechanism to make it refresh, this point Google is referring to the next Win32 message processing mechanism when designing Android .

  •  1. For the refresh of a view in the thread as a base class interface, you can use the postInvalidate () method to deal with the thread, which also provides some rewrite methods such as postInvalidate (int left, int top, int right, int bottom) to refresh A rectangular area, as well as delayed execution, such as postInvalidateDelayed(longdelayMilliseconds) or postInvalidateDelayed(longdelayMilliseconds, int left, int top, int right, int bottom) methods, where the first parameter is milliseconds
  •  2. Of course, the recommended method is to handle these through a Handler, which can be implemented by calling the handler object's postMessage or sendMessage method in a thread's run method. The Android program internally maintains a message queue, which will be rotated for processing if you are a Win32 program. The staff can understand these message processing well, but compared to Android, there is no way to provide PreTranslateMessage these interference internal methods.
  • 3. Looper is what? In fact, every Thread in Android is followed by a Looper, Looper can help Thread maintain a message queue, but Looper and Handler has nothing to do, we can see from the open source code Android also provides a Thread inheritance The class HanderThread can help us to deal with, in the HandlerThread object can get a Looper object control handle by getLooper method, we can map its Looper object to a Handler to achieve a thread synchronization mechanism, the implementation of the Looper object needs to initialize Looper. The prepare method is the problem that we saw yesterday. At the same time, we need to release resources and use the Looper.release method.
  • What is 4.Message on Android? For Handler Android can pass some content, through the Bundle object can encapsulate String, Integer and Blob binary objects, we pass a Bundle object to the thread using the sendEmptyMessage or sendMessage method of the Handler object to Handler processor.For the Handler class, an overriding method handleMessage(Message msg) is provided to distinguish each message by msg.what.Unpack the Bundle to implement the Handler class to update the contents of the UI thread to implement the control refresh operation.The relevant Handler objects related to the message sending sendXXXX related methods, as well as postXXXX related methods, these are basically the same with the principles in Win32, one for the direct return after sending, one for processing to return.
  • 5. Java.util.concurrent object analysis, for programmers engaged in Java development in the past will not be unfamiliar to the Concurrent object, he is an important feature of the new JDK 1.5 later as a handheld device, we do not advocate the use of this class, taking into account Android We have already designed the Task mechanism. We don't make too many details here. Related reasons are as follows:
  • 6. Android also provides a different processing method than the thread, Task and AsyncTask, can be seen from the open source code is for the Concurrent package, developers can easily handle these asynchronous tasks.

This article has been transferred from http://www.cnblogs.com/playing/archive/2011/03/24/1993583.html. If you feel that this article is useful to you, you can click on "Sponsored Author" below the article to reward it. Author!

Post comment


No Comment


Forget password?