Android 2021 study questions

About the study topics of the recent interview

The following is for reference only. The detailed answer is not unique. It depends on your level of understanding. When you click on this blog, it means that you have moved your mind. I wish you every success!

Android onTouchEvent, onClick and onLongClick call mechanism?

Link: Detailed link

Question: (generally appearing together with the event distribution mechanism) Which method of pressing, lifting, and moving of the Click event of the Button and the LongClick event in onTouch is executed?

  • Answer: onTouchEvent is the first to execute Down and Up events, followed by Click and LongClick.
    LongClick is in the PostCheckForLongClick method of Down. When the long press event is executed, the system will trigger a postDelayed operation. If it is pressed for more than 500s, it will trigger the execution in the CheckForLongPress thread. If the conditions are met, then perFormLongClick() will be executed. , Call onLongClick() in this method;
    Click is executed by perFormClick after Up occurs, perFormClick will first call the onClick method of the registered listener to execute.

How to resolve the event conflict between ScollView and Listview?

Link: Details link

  • Answer:
    1. The solution is to inherit ListView, rewrite the dispathTouchEvent method, implement Move, Down, set getParent().requestDisallowInterceptTouchEvent() to true in its operation, and set getparent{}.requestDisallowInterceptTochEvent() to false in Up and Cancel. , It’s
    ok ; 2, inherit ScollView to override interceptTouchEvent, use getLocationInWindow to get the relative position of ListView, call MotionEvent’s getRawX and getRawy methods to get the screen coordinates, and set it to false when it is judged that ListView is not equal to null and the area of ​​ListView is touched , Let the child control handle it.

Which one takes effect when Android sets the startup mode in the manifest file or uses the setFlags setting of the intent?

Link: Details link

Question: (It usually appears with the four startup modes) Suppose I set the startup mode in the manifest file or when I use the intent to start, which one will take effect?

  • Answer: The effect in the code is like setting a background color in xml and setting another in the code. It must be a dynamic code that takes effect.

What are the advantages of Glide? How to achieve circular cutting?

  • Answer: glide uses RGB_565, which uses chain call, can be loaded progressively, uses three-level cache, and firstly finds pictures in the memory. If it exists, it will display it. If it does not exist, it will go to the disk to find it. Stored in the memory, if not, go to the network to load, after loading, the picture will be displayed and transferred to the disk.

How does the service work?

  • Answer: Servicer has startServer and BindServer. StartServer will always exist in the background after it is started. The life cycle of the startup is: onCreate–>onStartCommand–>onDestroy; BindServer exists in the life cycle of the Activity. If the Activity is destroyed, then onBindServer is also destroyed, life cycle: onCreate–>
    onBind–>onUnbind–>onDestroy.
    Difference: If startService() starts the Service, if the caller exits directly without calling stopService, the Service will always run in the background.
    bindService() starts the Service, the caller and the Service coexist and die

Is there only one looper and messagequeen in the handler thread?

  • Answer: There is only one looper and one MessageQueen in the handler.

How to realize the first-in-first-out of incoming multiple messages in messagequeen?

  • Answer: There is a synchronization lock mechanism in MessageQueen. If a message is sent at the same time, the system will also determine who entered first, and whoever entered first holds the lock, thus achieving first-in first-out.

There are multiple handlers in an Activity. How does it distinguish between messages sent by HandlerA or messages sent by HandlerB?

  • Answer: When the handler sends a message, it will carry the corresponding handler object. When the looper fetches the message, it will call the message in the messageQueue. This time it will call enqueueMessage(), and then assign this to msg.target, which is the handler at this time. It points to the current handler.
  • Wrong statement: Messageg must be instantiated before creating sendMessage. The instantiated Message has the what method, and methods such as arg1. Wait is used to distinguish between messages received by handlerMessage, and arg1 is the data to be passed.

What are the custom views?

  • Answer: Inheritance, self-drawing, combined

What are the methods for customizing the View self-drawing style?

  • Answer: onMeasure: measure the size of View, onLayout: measure the position of View, onDraw: use to draw itself (background, content)

What are the memory leaks encountered in the project? How to solve it?

  • Define Handler as a static internal class, hold the weak reference of Activity internally, and remove all messages in time.

What are the solutions to memory leaks?

  • The reason for the memory leak is that if a long-lived object holds a short-lived reference, a memory leak is likely to occur.
    Solution: Use weak references (recycled when memory is insufficient), the leak caused by the singleton mode can be passed into context.ApplicationContext(), let it exist with the existence of the entire App, and the memory leak caused by the handler uses weak references in Empty processing in handlerMessage

What are the causes of memory overflow?

  • Answer:
    1. Garbage collection timeout memory overflow (unable to collect unreferenced objects for a long time)
    Solution: To reduce the object life cycle, garbage collection can be carried out as quickly as possible.
    2. Java heap memory overflow (jvm memory is too small, the memory required by the object is too large)
    Solution: First, if there is no problem with the code, you can appropriately adjust the two jvm parameters -Xms and -Xmx, and use stress testing To adjust these two parameters to reach the optimal value.
    Second, try to avoid applications for large objects, such as file uploads, and large batches of data acquisition from the database. This is to be avoided. Try to block or batch processing as much as possible to help the normal and stable execution of the system.
  • Extension: Out of memory: The popular understanding is that there is not enough memory. Usually when running large software or games, the memory required by the software or game far exceeds the capacity of the memory installed in your host, which is called out of memory. .
    Memory Leak: (Memory Leak) refers to the heap memory that has been dynamically allocated in the program. For some reason, the program is not released or cannot be released, which results in a waste of system memory, resulting in serious consequences such as slowing down of the program and even a system crash.

What is the difference between inner class and static inner class and anonymous inner class?

  • Answer: 1. The internal class can directly access the static and non-static methods of the external class.
    2. The static inner class can be directly called by other activities, but it can only access the static methods and member variables of the outer class.
    3. The anonymous inner class is implemented through inheritance or interface, and must be initialized at the same time as the defined class and the implemented interface.

Multi-threaded data synchronization problem?

Link: Details link

gson string to the corresponding object?

  • Answer: The deserialization of the object is achieved through fromJson() (that is, the json string is converted into an object type)
    , and the serialization of the object is achieved through toJson() (that is, the object type is converted into a json string).

What are the Android data storage methods?

  • Answer: SQLite, sp, upload interface, local file

Can there be multiple processes in an app?

  • Answer: Yes, set up a process in the manifest file

How does process A read the storage content of process B?

  • Answer: Use broadcast, or AIDL.

How does the A thread block the storage content of the B thread?

  • Answer: Use Handler, global constant

Can the main thread process multiple messages at the same time?

  • Answer: Yes, if multiple messages are sent in an Activity (CPU processing is nanoseconds/microseconds), they can be processed. We set the wait when creating Message to distinguish the messages sent by sendMessage. Use switch to select in handlerMessage deal with.

What is the difference between java abstraction and interface?

  • Answer: 1. Abstract methods must be overridden by subclasses. Abstract methods have no method bodies. 2. The methods implemented in interfaces are static.
    3. An Activity can implement multiple interfaces, but it can only be inherited from one abstract class.

What is the difference between thread and process?

  • Answer: Processes exist with the existence of App. There can be multiple threads in a process. If one thread crashes, it will affect other threads, and if one process crashes, it will not affect other processes. So, Processes are more robust than threads.

What is the difference between wait and sleep?

  • Answer: wait is to release the lock, and Sleep is to hold the lock; wait is the method of Object, and Sleep is the method of Thread; sleep will not occupy the cpu, but will let other threads.

handler process?

Link: Details link

  • Answer: handler.sendMessage sends message messages to messagequeen. Messagequeen polls the messages one by one through looper. After the message is retrieved, the message will be distributed through the dispathmessage method and sent to the corresponding handlerMessage to update the UI.

Guess you like

Origin blog.csdn.net/weixin_44781755/article/details/114970336