The most complete collection of Android interview questions in history

Time:2020-8-14

Link to the original text: https://blog.csdn.net/xiangzhihong8/java/article/details/96280254

Basic knowledge of Android

1. General knowledge points
1. Android class loader
In Android development, both plug-in and component-based are designed based on the class loader classloader of Android system. However, the virtual machine on Android platform runs DEX bytecode, a product of class file optimization. The traditional class file is a java source file, which generates a. Class file. Android combines and optimizes all class files, and then generates a final one class.dex The purpose is to keep only one copy of the repeated items in different class files In droid application development, if the Android application is not divided into DEX processing, the APK of the last application will only have one DEX file.

There are two kinds of class loaders commonly used in Android, dexclassloader and PathClassLoader, both of which inherit from basedexclassloader. The difference is that dexclassloader passes an optimized directory parameter when calling the parent class constructor. This directory must be an internal storage path to cache DEX files created by the system. The parameter of PathClassLoader is null and can only load DEX files of internal storage directory. So we can use dexclassloader to load external APK files, which is the basis of many plug-in technologies.
The most complete collection of Android interview questions in history

2、 Service
To understand Android service, you can understand it from the following aspects:

Service is executed in the main thread, and time-consuming operations (network request, database copy, large file) cannot be performed in service.
You can set the process where the service is located in XML, and let the service execute in another process.
The maximum operation performed by the service is 20s, the broadcastreceiver is 10s, and the activity is 5S.
Activity is bound to service through bindservice (intent, serviceconnection, flag).
Activity can start service through startservice and bindservice.
IntentService
Intentservice is an abstract class that inherits from service. There is a servicehandler (handler) and handlerthread (thread) inside. Intentservice is a class for processing asynchronous requests. In intentservice, there is a handlerthread to handle time-consuming operations. The way to start intentservice is the same as normal. However, when the task is finished, intentservice will stop automatically. In addition, the intentservice can be started multiple times, and each time-consuming operation will be executed in the onhandleintent callback of the intentservice in the form of a work queue, and a worker thread is executed each time. The essence of intentservice is that it encapsulates an asynchronous framework of handlerthread and handler.

2.1 life cycle diagram
As one of the four components of Android, service is widely used. Like activity, service also has a series of lifecycle callback functions, as shown in the figure below.
The most complete collection of Android interview questions in history
Generally, there are two ways to start a service: startservice and bindservice.

2.2. Startservice life cycle
When we call the startservice method of context, we start the service. The service started by the startservice method will run indefinitely. Only when the stopservice of context is called externally or the stopself method of service is called internally, the service will stop running and be destroyed.

onCreate
Oncreate: when executing the startservice method, if the service is not running, the service will be created and the oncreate callback method of the service will be executed; if the service is already running, the oncreate method of the service will not be executed when the startservice method is executed. In other words, if the startservice method of context is executed multiple times to start the service, the oncreate method of the service method will only be called once when the service is created for the first time, and will not be called again. We can complete some service initialization related operations in the oncreate method.

onStartCommand
Onstartcommand: after the startservice method is executed, the oncreate method of the service may be called. After that, the onstartcommand callback method of the service will be executed. In other words, if the startservice method of context is executed multiple times, the onstartcommand method of service will be called multiple times. The onstartcommand method is very important. In this method, we perform actual operations according to the incoming intent parameters. For example, we will create a thread here to download data or play music.

public @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) {
}

When Android is faced with memory shortage, it may destroy the service you are currently running, and then you can re create the service when the memory is sufficient. The behavior of the service being forcibly destroyed by the Android system and rebuilt again depends on the return value of the onstartcommand method in the service. There are three commonly used return values, start_ NOT_ STICKY、START_ Sticky and start_ REDELIVER_ Intent, these three values are static constants in service.

START_NOT_STICKY
If you return to start_ NOT_ Sticky means that when the running process of a service is forcibly killed by the Android system, the service will not be re created. Of course, if startservice is called again after it is killed, the service will be instantiated again. What is the appropriate situation to return this value? If the work performed by a service is interrupted several times, or needs to be killed when Android memory is tight, and it will not be re created immediately. If this behavior is acceptable, we can set the return value of onstartcommand to start_ NOT_ STICKY。 For example, a service needs to get the latest data from the server regularly: let the timer start the service to get the latest data of the server every specified n minutes. When the onstartcommand of the service is executed, a timer after n minutes is planned to start the service again and open a new thread to perform network operation. Suppose that the service is forcibly killed by the Android system in the process of getting the latest data from the server, and the service will not be re created. It doesn’t matter, because in another n minutes, the timer will start the service again and retrieve the data again.

START_STICKY
If you return to start_ Sticky means that after the running process of the service is forcibly killed by the Android system, the Android system will still set the service to the started state (i.e. the running state), but will no longer save the intent object passed in by the onstartcommand method. Then, the Android system will try to re create the service and execute the onstartcommand callback method, but the onstartcommand callback method will not be used If the intent parameter is null, that is, the onstartcommand method will execute, but the intent information cannot be obtained. If your service can run or end at any time without any problem and does not need the intent information, you can return start in the onstartcommand method_ Sticky, such as a service used to play background music, is suitable to return this value.

START_REDELIVER_INTENT
If you return to start_ REDELIVER_ Intent means that after the process running by the service is forcibly killed by the Android system, it will return to start_ Similar to sticky, the Android system will re create the service and execute the onstartcommand callback method. However, the difference is that the Android system will again retain the intent in the onstartcommand method that was last passed into the onstartcommand method before the service is killed, and then re pass it into the onstartcommand method of the re created service, so that we can read it To the intent parameter. Just go back to start_ REDELIVER_ Intent, then the intents of onstartcommand must not be null. If our service needs to rely on a specific intent to run (it needs to read relevant data information from the intent), and it is necessary to recreate the operation after the forced destruction, then such a service is suitable for returning to start_ REDELIVER_ INTENT。

onBind
The onbind method in service is an abstract method, so the service class itself is an abstract class, that is, the onbind method must be overridden, even if we can’t use it. When using service through startservice, we only need to return null when overriding the onbind method. The onbind method is mainly used when calling service for the bindservice method.

onDestroy
Ondestroy: the service started by the startservice method will run indefinitely. Only when stopservice of context is called or stopself method is called inside the service, the service will stop running and destroy, and the service callback function will be executed when destroying.

2.3. Bindservice life cycle
The most complete collection of Android interview questions in history
The bindservice mode starts the service with the following life cycle functions:

onCreate():
This method is called the first time the service is created. If the service is already running, this method is not called, it is called only once.

onStartCommand():
This method is called when another component requests to start a service by calling startservice().

onDestroy():
This method is called when the service is no longer in use and will be destroyed.

onBind():
This method is called when another component binds to the service by calling bindservice().

onUnbind():
This method is called when another component unbinds the service by calling unbindservice().

onRebind():
When the old component is unbound from the service, and another new component is bound to the service, and onunbind() returns true, the system will call this method.

For free access to advanced Android materials, click the link below

https://shimo.im/docs/tXXKHgdjPYj6WT8d/