Preface
This article wants to share how to prepare for the interview with Ali and what you want from the interview process. I hope it can help you.
First of all, what may disappoint you is that this article will not contain large-scale answers to interview questions. If you want to read this aspect, you can read my previous article. Thanks for attention
One, Android basics
There are many basic knowledge points of Android, see the picture.
Suggested reading:
"Android Development Art Exploration"
1. Activity
# Activity's four startup modes, and application scenarios?
Activity
The four startup modes:
standard
: Standard mode, each time a newActivity
instance is generated in the active stack . Usually the activities we use are standard models.singleTop
: Stack top reuse, if theActivity
instance already exists on the top of the stack, then no new instance will be created in the active stack. A more common scenario is toActivity
set the notification jump , because you definitely don't want to click on the notification when the front desk isActivity
alreadyActivity
the case, and then create a similar one for youActivity
.singleTask
: Reuse within the stack. If theActivity
instance already exists in the current stack, allActivity
otherActivity
instances above the current instance will be removed from the stack. Commonly used to jump to the main interface.singleInstance
: Single instance mode, create a new task stack, this activity instance is alone in this activity stack.
# What is the difference between onStart and onResume in Activity? The difference between onPause and onStop?
First, Activity
there are three categories:
- Foreground
Activity
: ActiveActivity
, interacting with usersActivity
. - Visible but not in the foreground
Activity
: TheActivity
background is usually transparent at the top of the stack , and the one below itActivity
is visible but not user-interactive. - Background
Activity
: The onesActivity
that have been suspended , such asonStop
methods that have been executed .
Therefore, onStart
sum onStop
usually refers to the perspective of whether the current activity is in the foreground, and onResume
sum onPause
from the perspective of whether it is visible.
2. Screen adaptation
# How do I usually use screen adaptation? What is the principle?
The usual screen adaptation scheme is generally adopted by the headline screen adaptation scheme. To put it simply, one side of the screen is used as the adaptation, which is usually wide.
Principle: The relationship between device pixels px
and device independent pixels dp
is
px = dp * density
Assuming that the screen width of the design drawing given by UI is based on 360dp, then the pixels of the device width are known, that is, px, dp are also known, 360dp, so you can density = px / dp
modify the density
relevant knowledge points in the system according to this later .
3. Android message mechanism
# Introduction to Android message mechanism?
Four concepts in the Android message mechanism:
ThreadLocal
: The data stored in the current thread can only be retrieved from the current thread.MessageQueue
: Message queue with time priority.Looper
: Polling the message queue to see if there are new messages coming.Handler
: Where the logic is specifically handled.
process:
- Preparation: create
Handler
, if it is created in the sub-thread, also need to callLooper#prepare()
inHandler
the constructor, binds themLooper
andMessageQueue
. - Send message: create a message, use
Handler
send. - Entry
MessageQueue
: Because itHandler
is bound to the message queue, itMessage
is naturally put into the message queue. Looper
Polling the message queue: ItLooper
is an infinite loop, always observe whether there are new messages coming, thenMessage
take out the binding,Handler
and finally callHandler
the processing logic. All this happens in theLooper
loop thread, whichHandler
can also process tasks in the specified thread s reason.
# Looper does not cause the interface to freeze in an endless loop in the main thread?
- Lead to stuck is to perform time-consuming operation resulting in dropped frames interface appears, even in Ui thread
ANR
,Looper.loop()
this operation itself will not lead to this situation. - Someone might say that setting an infinite loop in the click event will cause the interface to freeze. The same is an infinite loop, isn't it all the same? Looper will block the current thread when there is no message, release CPU resources, and wake up the main thread when a message arrives.
- An infinite loop is required in the App process. If the loop ends, the App process is over.
# IdleHandler introduction?
Introduction: IdleHandler is a mechanism for handling idle tasks when Hanlder is idle.
Execution scenario:
MessageQueue
When there is no message, the queue is empty.MessageQueue
It is a delayed message, when there is no message execution.
Will an infinite loop occur: The answer is no, MessageQueue
the method of using counting guarantees that MessageQueue#next
the IdleHandler
collection will only be used once in a call to the method .
4. View event distribution mechanism and View drawing principle
Gang's "Android Development Art Exploration" is already very comprehensive, it is recommended to read.
5. Bitmap
# How to calculate the memory of Bitmap?
In the case that the length and width of the picture are known in pixels, the factors that affect the memory size will be the location of the resource file and the size of the pixel .
Pixel size : Common pixels are:
- ARGB_8888: 4 bytes
- ARGB_4444, ARGB_565: 2 bytes
Resource file location : Folders corresponding to different dpi
For example, the pixel of a picture is 180*180px
, dpi
(device independent pixel density) is 320, if it is only stored in drawable-hdpi
, then there are:
横向像素点 = 180 * 320/240 + 0.5f = 240 px
纵向像素点 = 180 * 320/240 + 0.5f = 240 px
If it is only stored in drawable-xxhdpi
, there are:
横向像素点 = 180 * 320/480 + 0.5f = 120 px
纵向像素点 = 180 * 320/480 + 0.5f = 120 px
Therefore, for a 180*180px
picture, the device dpi is 320, the resource picture only exists drawable-hdpi
, the pixel size is ARGB_4444
, and the final file memory size is:
横向像素点 = 180 * 320/240 + 0.5f = 240 px
纵向像素点 = 180 * 320/240 + 0.5f = 240 px
内存大小 = 240 * 240 * 2 = 115200byte 约等于 112.5kb
# Efficient loading of Bitmap?
The efficient loading of Bitmap is also used in Glide, the idea:
- Get the required length and width, generally get the length and width of the control.
- Set
BitmapFactory.Options
ininJustDecodeBounds
to true, it can not help us in a way to get loaded into memoryBitmap
length and width. - Compare the required length and width with the length and width of the Bitmap to obtain the compression ratio and put
BitmapFactory.Options
in theinSampleSize
attributes. - Set
BitmapFactory.Options
ininJustDecodeBounds
to false, the image is loaded into memory, and then set up to control.
Second, Android advanced
In Android Advanced, focus on inspection Android Framework
, performance optimization and third-party frameworks.
1. Binder
#Binder's introduction? What are the advantages and disadvantages of other IPC methods?
Binder is a unique IPC method in Android, quoting the words in "Android Development Art Exploration" (with slight changes):
From the perspective of IPC, Binder is a cross-process communication method in Android; Binder can also be understood as a virtual physical device, and its device driver is /dev/binder; from the beginning
Android Framework
, Binder isService Manager
connected to variousManager
and correspondingManagerService
Bridge. In terms of object-oriented and CS model,Client
itServer
communicates with remote through Binder .
Based on Binder, Android also implements other IPC methods, such as AIDL
, Messenger
and ContentProvider
.
Compared with other IPCs:
- High efficiency: In addition to memory sharing, other IPCs need to perform two data copies, and because Binder uses memory mapping, only one data copy is required.
- Good security: The receiver can obtain the sent process ID and user ID from the data packet, which is convenient for verifying the identity of the sender. Other IPCs who want to experiment can only actively save it, but this may be modified during the sending process. .
At last
If you see this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message. Will surely inquire carefully and correct deficiencies. Thank you.
Finally put a benefit at the end of the article: GitHub address
PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~
8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**
PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~
Welcome everyone to communicate and discuss together~