The study of Asyncqueryhandler this class encountered several important unclear points of knowledge .1. The relationship between handler and Thread,looper2. What's handlerthread for?3. What is the Threadlocal class for? Handler is mainly used to send and process messages, but when a message is sent, how is the message delivered? This is the role of looper, each handler will have a Looper object, if not specified when creating handler, the system will default to the current thread looper bound to handler, Looper objects in the maintainer of a message queue, Hander sent messages are stored in this message queue, Looper constantly traverse the message queue, take out the message, to Handlemessage method processing. The looper belongs to which thread, and the Hadlemessage method executes in that thread. Handlerthread not only provides asynchronous processing, but the method of handler processing messages is also executed in this thread, and his most important role is to provide a thread. (This class has yet to be studied.) The Threadlocal class is primarily used for multiple modules sharing variables, but the values of variables between different threads are not the same. What is the working mechanism of Asyncqueryhandler? Asyncqueryhandler inherits the handler object, but he does not provide a looper parameter in the constructor, which means that he and his current thread are bound, Asyncqueryhandler has a Hhandler object inside. Called Mworkerhandler, he's bound to a handlerthread, Mworkerhandler is responsible for sending the packaged message, processing it, and sending the result as a message to Asyncqueryhandler. How did he send it? Inside the Asyncqueryhandler is a Workerargs perfect class that encapsulates the parameters of Startasyncquery and other methods, and through this line of code Java code: Workerargs args=NewWorkerargs (); Args.handler= ThisThe copy code encapsulates the current handler, sends it to Handlerthread, and mworkerhandler the message to get the result, Args.handler sends the structure to itself for processing. (This is the communication between the threads) Mworkerhandler and a sub-thread binding, able to handle more time-consuming operations, Asyncqueryhandler provide asynchronous processing. 1, if we do not asyncqueryhandler, directly in the UI thread call Contentresolve to manipulate the database such as query, if your database data is very little good, if a lot of, will appear ANR. The general solution to the ANR, is to open the thread to solve. When I was doing a case last year, I had this kind of situation, the data is too much, I have to open the thread to query, that thread will have to interact with the UI thread through handler, let the UI thread know when the query is complete, you can update the UI to display the results of the query. 2, if we use Asyncqueryhandler,asyncqueryhandler it will take the initiative to help you open the thread to avoid the ANR, and it is inherited from Handler, So you can tell the UI thread has been queried (delete ...) through oncompletequery. Finished.
The asynchronous class of the SMS operation, when the text is executed, you can call the Srartquery method to execute the asynchronous query, and then call the Onquerycomplete method to implement the data update.
NULL NULL NULL NULL null);
private final class Queryhandler extends asyncqueryhandler{ public queryhandler (contentresolver cr) { Supe R (CR); // TODO auto-generated constructor stub } @Override
protected void onquerycomplete ( int token, Object cookie, Cursor Cursor) { // TODO auto-generated method stub Super
.onquerycomplete (token, cookie, cursor);
if (Cursor.getcount () > 0 ) { int count = Cursor.getcou NT (); Sizemap.put (token, count); Madapter.notifydatasetchanged (); } cursor.close (); } }