[Android] Handler in IdleHandler

### throws
`IdleHandler`` Handler` in
? + What power it has
? + What it was
+ can think of some appropriate scene it?
> Answer1:

First, look at the source of the annotation
`` `
/ **
* When the Callback interface for Discovering IS going to Block A Thread
* More Waiting for messages.
* /
Public static interface IdleHandler {
/ **
* When the Called The Message Queue has RUN OUT of messages and by Will now
* the wait for More. the Return to true to the Keep your IDLE Handler the Active, false
* to have have IT removed. This May BE Called IF there are Still messages
* the Pending in at The Queue, But They are All Scheduled to BE dispatched
* the After . the Current time
* /
Boolean queueIdle ();
}
`` `
annotation explicitly indicated when the message queue is empty performs` IdelHandler` `queueIdle ()` method, which returns a value `boolean`,
if` false` is finished after the removal of this message,
If the reservation is `true`, will perform again until the next idle

The following look `MessageQueue` of` next () `method can be found is indeed the case.
`` `
The Message Next () {
......
for (;;) {
......
the synchronized (the this) {
// here treated as normal message queue
......
IF (mQuitting ) {
Dispose ();
return null;
}
IF (pendingIdleHandlerCount <0
&& (mMessages now == null || <mMessages.when)) {
pendingIdleHandlerCount mIdleHandlers.size = ();
}
IF (pendingIdleHandlerCount <= 0) {
// .. NO IDLE handlers to RUN and the wait some Loop More
mBlocked = to true;
Continue;
}
IF (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new new IdleHandler [Math.max (pendingIdleHandlerCount,. 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler

boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}

if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
pendingIdleHandlerCount = 0;
nextPollTimeoutMillis = 0;

}
}
`
Processed after` IdleHandler` will be set to `0`` nextPollTimeoutMillis`, the message queue is not blocked, the code is to be noted here, of course, the same can not be executed too time-consuming, because it is synchronous, if too time-consuming certainly affect `message` later execution.
The above is probably the ability to speak so, then the ability to determine the usefulness, usefulness essence is taking advantage of the message queue free time doing something, of course, it will depend on the specific use of specific treatment.
To use `IdleHandler` just call` MessageQueue # addIdleHandler (IdleHandler handler) ` method to

** Some scenes can be appropriate from the following: **
+ message queue associated
+ primary thread capable of things
+ and false returns true bring different results

** currently conceivable scenarios: **
1.`Activity` start optimization: `onCreate, onStart, onResume` in a relatively short time-consuming, but not the necessary code can be placed in `IdleHandler` executed, reducing start-up time
2. want to add a View drawing after the completion of other `view` depend on the` view`, of course, this `View # post ()` can be achieved, the difference is that the former will be executed when the message queue is idle.
3. happened a return `true` in` IdleHandler`, something to make a `View` flashing, so when you can induce the user clicks the` View` user trance, which is kind of cool operation.
4. Use some third-party libraries, such as [LeakCanary] (https://github.com/square/leakcanary), Glide to use, specifically to view itself.
> Answer2:

The `IdleHandler`, it is declared in an interface` MessageQueue` inside, so we can guess it must have a relationship with `MessageQueue`, this interface is only one way:` queueIdle`, literally means idle queue.
`MessageQueue` look at the source code can be found in two statements on` IdleHandler`, namely:
+ store `IdleHandler` the ArrayList` (mIdleHandlers) `
+ there is a` IdleHandler` array `(mPendingIdleHandlers)`

Behind the array, which put its `IdleHandler` Examples are temporary, that is, after each use (called` queueIdle` methods), blanking will `(mPendingIdleHandlers [i] = null )`
that they will when to use it?
is in it `next` method of` MessageQueue`
** probably the process is this: **
1. If this cycle to get `Message` is empty, or! The `Message` is a delay of a message and not to trigger a specified time, then finds the current queue for the free time.
2. `mPendingIdleHandlers` then traverses the array (the array elements inside every time to get the mIdleHandlers) to call a` queueIdle` method `IdleHandler` each instance.
3. If this method returns `false`, then this example will be removed from the` mIdleHandlers`, that is the next time the queue is idle, will not continue to call back its queueIdle way to go.
** look at its use in the scene where the source: **
For example, in `ActivityThread`, there is a named inner class` GcIdler` achieve a `IdleHandler` interface.
When it is `queueIdle` callback method, do` GC` operation force (i.e., call `` faceGc` BinderInternal` method), but the premise is forced `GC` with the previous forced apart at least 5 seconds` GC` the above.
** This GcIdler that will use it? At what point **
When `ActivityThread` of` mH` (Handler) received `GC_WHEN_IDLE` message.
** When will I receive `GC_WHEN_IDLE` message? **
When the `AMS` (ActivityManagerService) These two methods are called:
+` doLowMemReportIfNeededLocked`, this method see the name to know it is not enough memory when called.
+ `ActivityIdle`, this method, that is called when when` `handleResumeActivity` ActivityThread` method is called (` onResume` callback method `Activity` also in this method there).
> Answer3:

`IdleHandler`: idle listener (as if I had nothing better to do, and made a face in the group, this time other people will know that I am very busy up)
every time you get next message is processed, found that did not have to be processed message (queue is empty, the message and not only delay the time, no blocking asynchronous message synchronization) will be notified of these subscribers.

Suitable for a number of non-essential things, because the notice was too unstable (like someone else said a few days you eat, you silly waiting estimated to starve to death)
> Answer4:

See the source `next`` MessageQueue` method is invoked (in Message` `MQ message when executed completely message or a delay processing) when` MessageQueue` idle.

**: ** systems are still a few places applied
1. `Activity onResume` and` view` called after the completion of initialization
2. forcibly `GC`, invoked when there is insufficient memory
occurs after the broadcast 3.` TTS` synthesis of
4 adding () `before performing the onCreate Activity in`
5. keyboard-related calls

Combined with the scene of the system, guess what, you can do the UI operation, you can not be too time-consuming, calling the timing of vague correlation is not so strong.

Reference links:
[Daily asked Handler in IdleHandler heard it? ] (https://www.wanandroid.com/wenda/show/8723)

 

Guess you like

Origin www.cnblogs.com/merbn/p/11246118.html