Turn http://www.iteye.com/problems/69457When the activity starts, clicking on an interface button opens a service (tentatively Padservice), and a thread (tentatively Thread-3) initiates a socket connection in Padservice. Our project uses Mina as the socket communication framework, the comrades who have used Mina should be familiar with, Thread-3 is only responsible for monitoring, the specific message processing is another thread. The problem with handling messages in our Iohandler is that I nee
Recently approached a more challenging project, I found that the use of a large number of message mechanisms, and now this blog I want to specifically analyze: What is the message of Android, what is the message mechanism in the end what good?Actually speaking of Android message mechanism, we may soon think of message/handle/looper these three objects, yes, in Android is through the three objects to implement the message mechanism. So what I want to a
empty message.
6, Final Boolean sendemptymessagedelayed (int what,long delaymills): Specifies how many milliseconds to send empty messages.
7. Final Boolean SendMessage (msg): Send a message now.
8. Final Boolean sendmessagedelayed (Message Msg,long delaymillis): Specifies how many milliseconds to send messages.
Using the above methods, the program can easily use the handler class for message delivery.
Working principle of Handler, Loop and MessageQueue
To better understand how handler wo
Handlerthread is a special thread, that is, looper thread, since there is looper, then we can use this looper toCreate a handler to interact with it, such as you can send a message to it in the UI thread through the handler object associated with it. HandlerthreadIt is generally possible to perform certain background operations, such as reading and writing files
the new message (), but Android recommends that we pass Message.obtain () or handler.obtainmessage () Gets the message object. This is not necessarily a direct creation of a new instance, but rather a message from the pool to see if there are any instances of messages available, and the existence is directly removed and returned to this instance. Conversely, if there is no message instance available in the messages pool, a new one is given based on the parameter. By analyzing the source, the An
If you're deciding whether a thread is the main line in Android? For this question, you might say that according to the thread's name, of course this solves the problem, but is it the most reliable? In case one day, Google will be the thread of the name of the other god horse things.
Method revealed
The following approach is the most reliable solution.
Copy Code code as follows:
public static Boolean Isinmainthread () {
return Looper.mylooper () = = Looper.getmainlooper ();
I. OverviewHandler, Looper, and messages are all related to the Android asynchronous message processing thread. So what is called an asynchronous message processing thread?After the asynchronous message processing thread starts, it enters an infinite loop body, takes one message out of its internal message queue, and then recalls the corresponding message handler function, and then continues the loop after executing a message. If the message queue is
without blocking the main thread, but only one request can be processed at the same time.After knowing the instructions of Intentservice, we continue to look at the code, first look at the definition of Intentservice member variables, the code is as follows:Provides Message Queuing and private volatile Looper mservicelooper;//processing messages private volatile Servicehandler mservicehandler;// Represents the worker thread's name private String mnam
= =null) {Smainthreadhandler =Thread.gethandler (); } if (false) {Looper.mylooper (). setmessagelogging (New Logprinter (Log.debug, "Activitythread")); }//End of event Activitythreadmain.trace.traceend (Trace.trace_tag_activity_manager); Looper.loop ();throw new RuntimeException ("Main thread loop unexpectedly exited"); }In the above code, the red part of the code before the main use for the environment initialization, androidkeystoreprovider installation, etc., here do not focus on the descrip
Handler1. Why use handlerAfter Android4.0, Google is considering the use of the system and the user experience, if you do some time-consuming operations, do not allow directly in the main thread, but to send a message object by handler method to modify the main thread UI interface2.Handler Principle IntroductionIn all UI interfaces, a dead loop (Looper) is being executed to receive and listen to the instructions from the user, but once the instruction
In the previous article, we talked about the method of calling handler in the SendMessage, and eventually we would go into a way called Sendmessageattime, as follows: public boolean sendmessageattime (Message msg, long Uptimemillis) { MessageQueue queue = mqueue; if (queue = = null) { runtimeexception e = new RuntimeException (this + "sendmessageattime () called with no Mqueue "); LOG.W ("Looper
Basic introductionThe asynchronous message processing thread refers to a thread that enters a wireless loop body after it is started, does not loop once, pulls a message from the internal message queue, and calls back the corresponding message handler, and then resumes the loop after executing a message. If the message queue is empty, the thread pauses to know that there are new messages in the message queue.The asynchronous message processing thread is still essentially a thread, except that th
, problem analysis1) Why the need for multi-threading is the nature of asynchronous processing, the intuitive point is not to let users feel "very card."Eg: you click the button to download a song, and then the button has been in the pressed state, then the user experience is poor.2) Multithreading implementation mode implements Runnable or extends Thread3) Multithreading core mechanism is handler4) Provide the following ways of implementationHandler———————————— Instructions 1When creating a han
information classes between threads in Android.To make it clear that handler is very simple to use, but I'll talk a little bit more about the handler mechanism.In order to allow handler to pass messages between threads, we also need to use several classes. They are looper,messagequeue,message.The looper here is not the Hollywood blockbuster of the previous period, his main function is to run a message ring
Handlerthread Introduction:We know that thread threads are disposable, and threads are automatically destroyed when thread threads perform a time-consuming task. If I have another one at this timeA time-consuming task needs to be executed, we have to recreate the thread to perform the time-consuming task. However, there is a performance problem: Creating and destroying threads multiple times is a costlysystem resources. In order to understand this problem, we can build a cyclic thread
Pre-order: Each app corresponds to a process, which has a activitythread thread called the main thread (i.e. the main thread of the UI), in addition to other threads, this re-argument.Android's message system analysis.
Each thread corresponds to only one looper
Each looper corresponds to only one MessageQueue
n a message in each MessageQueue
Specify a maximum of one handler per message
tool that is specifically used to pass information classes between threads in Android.To make it clear that handler is very simple to use, but I'll talk a little bit more about the handler mechanism.In order to allow handler to pass messages between threads, we also need to use several classes. They are looper,messagequeue,message.The looper here is not the Hollywood blockbuster of the previous period, his
Android's message mechanism is almost an interview question, and certainly not because of the interview, but to learn, more importantly, it is essential in the development of Android, occupy a pivotal position, so it is necessary to understand it. Here is the basic thing to say.
Looper
Role:
Associate Thread
Looping out messages
1, Looper can be directly instantiated?
The
This article is mainly on the handler and message loop implementation of the principle of source analysis, if unfamiliar handler can see the blog "Detailed Android handler use method", It explains why Android introduced the handler mechanism and how to use handler.
In general, Handler is a mechanism introduced in Android that allows developers to participate in the processing of message loops in threads. When we use handler, we deal with the message most, message is the hanlder mechanism to the
Android's message mechanism is almost a must-ask topic, of course, not because of the interview, but to learn, more importantly, it is essential in the development of Android, occupies a pivotal position, so it is necessary to understand it. Here's the basic thing to say.
LooperRole:
Associate Thread
Loop out messages
1. Can looper be instantiated directly?The Looper construction meth
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.