Android development can never escape the grasp of the Framework~

Whether you are doing mobile phone system development or APP development, you will definitely encounter the Framework layer. Unless what you're doing is just UI optimization.

Therefore, during the interview, the interviewer will inevitably test the level of the Framework layer you have mastered, and the common Android Framework serial gun interview questions include these:

1. What is Android Framework? What components and services does it contain?
2. What is the current Android Framework version? What improvements have been made over previous versions?
3. Please explain the role and difference of Activity, Service, ContentProvider and BroadcastReceiver.
4. Please introduce the life cycle of Activity. Can you draw a life cycle diagram?
5. What are the startup methods for Service? What's the difference?
6. What is AIDL? Please describe what it does and how to use it.
7. Please explain what Binder is and what is its function in Android Framework?
8. What is paging and how is it implemented?
9. What common system services does the Android Framework provide? What are their functions?
10. How to register and use BroadcastReceiver in the system?

Let me analyze and answer one by one:

1. What is the Android Framework? What components and services does it contain?

Android Framework is the application framework of the Android system, which provides the basic functions and structures required to develop Android applications. It contains various components and services that allow developers to easily write applications of high complexity. The Android Framework includes the following components and services:

  1. Activity: UI interface for creating applications;
  2. Service: used to perform tasks or operations in the background, excluding the UI interface;
  3. ContentProvider: used to share data with applications;
  4. BroadcastReceiver: used to broadcast and receive events and messages between applications and systems;
  5. Intent and IntentFilter: used to pass messages and events between applications and systems;
  6. Media components: including MediaCodec, MediaExtractor, MediaMuxer and other components for processing audio and video;
  7. Drawing components: including Canvas, Paint, Bitmap and other components, used to process images and drawings;
  8. Animation framework: used to achieve dynamic UI effects;
  9. Database framework: used to create and manage SQLite databases;
  10. System services: including ActivityManager, PackageManager, WindowManager and other services, used to manage the interaction between the system and applications.

These components and services constitute the core part of the Android Framework, providing rich functions and features for Android applications.

2. What is the current Android Framework version? What improvements have been made over previous versions?

The current Android Framework version is Android 11.

Compared with the previous version, the improvements of Android 11 mainly include the following points:

  1. Security improvements : Android 11 has enhanced privacy and security, including application rights management and private data protection.
  2. Improved message processing : Android 11 introduces a new mechanism that can classify different types of messages, such as notification messages, chat messages, to-do items, etc., to manage and process messages more conveniently.
  3. Multi-screen support : Android 11 provides better support for multi-screen devices, including expanding screens, folding screens, etc., which can better adapt to the characteristics and screen layouts of different devices.
  4. Enhanced media features : Android 11 has enhanced media features, including support for more audio and video formats, low-latency audio, etc., and also provides a new media control interface and device selection interface.
  5. Theme and interface improvements : Android 11 introduces a new theme and interface design language that presents information and actions more clearly and concisely.

Overall, Android 11 provides a better user experience and functional support, but also strengthens security and privacy protection, providing a stronger foundation and support for the development and implementation of Android applications.

3. Please explain the role and difference of Activity, Service, ContentProvider and BroadcastReceiver?

Activity, Service, ContentProvider and BroadcastReceiver are the four major components of an Android application, each of which plays a different role and function, explained in detail below:

  1. Activity : Activity is the most commonly used component in Android applications, which is responsible for the display of the user interface and the response to user interaction. Each Activity will have a corresponding layout file, in which the UI elements and layout of the interface can be defined. When the application needs to display a new interface, it can start a new Activity. Activities can jump and transfer data through the Intent object.
  2. Service : Service is a component that runs in the background, it can perform time-consuming tasks independent of user interaction, such as downloading, playing music, etc. Services can communicate with Activities, but they have no interface. When the application needs to run some tasks in the background, it can use Service to achieve.
  3. ContentProvider : ContentProvider is a database component in Android applications, which can provide data sharing and interaction. ContentProvider can store data in databases such as SQLite and expose these data through URI. Other applications can access this data and manipulate it. ContentProvider can realize data sharing and interaction between different applications.
  4. BroadcastReceiver : BroadcastReceiver is a component that receives broadcasts. It can listen to broadcast messages sent by the Android system or other applications and respond as needed. BroadcastReceiver can handle a wide range of system or application events such as incoming calls, text messages, network status, etc. It can register and send broadcasts through the Intent object.

In general, Activity, Service, ContentProvider and BroadcastReceiver are very important components in Android applications. They each have different functions and functions, and can be used to achieve different application requirements and scenarios.

Please introduce the life cycle of Activity. Can you draw a life cycle diagram?

The life cycle of Activity is divided into 7 phases, namely:

  1. onCreate() : The method that is executed when the Activity is created. Usually, some initialization work and interface layout initialization are done at this stage.
  2. onStart() : A method that is executed when the Activity is visible but has not yet started interacting. Usually, some UI update work is done at this stage.
  3. onResume() : The method executed when the Activity enters the foreground and starts interacting. Usually, some resources are loaded and dynamically updated at this stage.
  4. onPause() : The method executed when the Activity loses focus but is still visible. Usually, some resources are released and data is stored at this stage.
  5. onStop() : The method executed when the Activity is completely invisible. Usually, some resources are released and some listeners are unregistered at this stage.
  6. onRestart() : The method executed when the Activity changes from the stopped state to the running state, usually some UI and data are updated at this stage.
  7. onDestroy() : The method executed when the Activity is destroyed. Usually, some resources are released and cleaned up at this stage.

The following is the Activity life cycle diagram:

It can be seen that the life cycle of Activity consists of four phases: running period, pause period, stop period and destruction period. In actual development, we need to perform different operations in different life cycle methods according to different business needs. At the same time, you also need to pay attention to avoid life cycle problems during the development process, such as memory leaks, sudden exit and other problems.

5. What are the ways to start the Service? What's the difference?

Service has two startup methods: startService() and bindService(). Their differences are as follows:

1. startService(), start the Service in Start mode :

It is a one-way startup method, that is, after the Activity starts the Service through the startService() method, even if the Activity has been destroyed, the Service can continue to run in the background. At the same time, it also supports multi-threaded operations in the Service. The onCreate() in the Service will be called once, and the onStartCommand() will be called multiple times. Each time onStartCommand() means that a new Service is started. When the Service is stopped using stopService() or stopSelf(), the onDestroy() method will be called.

2. bindService(), start the Service in Bind mode :

It is a two-way startup method, that is, Activity and Service establish a connection through the bindService() method, Activity can communicate with Service, and Service can also send data to Activity. When using this method to start the Service, two callback methods in the ServiceConnection interface must be implemented: onServiceConnected() and onServiceDisconnected(). When the Activity is destroyed, the Service will be automatically destroyed. When no Activity is bound to the Service, the Service is still running in the background, but only when the return value of the onStartCommand() method is not START_STICKY, START_REDELIVER_INTENT, that is, the Service will not be started multiple times, but the Service can still call stopSelf( ) method to manually stop the Service.

Summary : Starting the Service through the startService() method can be operated in the background, and starting the Service through the bindService() method better realizes the communication between the Activity and the Service. At the same time, the cost of Service binding will be greater than that of starting Service with startService(). Which method to use to start Service should be determined according to business needs and development needs.

6. What is AIDL? Please describe what it does and how to use it.

AIDL (Android Interface Definition Language) is an inter-process communication (IPC) technology of Android, which provides a mechanism to enable you to transfer data across processes, allowing you to communicate and exchange data between modules. AIDL allows a process to call the method of another process by establishing a bridge between the client and server processes, and at the same time allows the client application and the remote service application to share data.

The main function of AIDL is to realize inter-process communication, allowing one process to call the method of another process, and also allowing data to be shared between different processes. It is a simple and reliable IPC solution provided by Android, which can be used to realize data sharing and interaction between multiple processes.

How to use AIDL :

1. Define the interface : first define an interface in the server. If cross-process communication is required, you need to add a RemoteException statement on the interface method; 2. Generate the interface
file : the compiler converts the interface file into a Java class and generates the corresponding Java interface file, Java Binder file, and Java Proxy file;
3. Realize the interface : on the server side, implement the interface defined by the client, and register its method in Binder;
4. Client call : on the client side, create A ServiceConnection implementation class and an IBinder object, through which the method of the server interface can be called to achieve cross-process communication.

It should be noted that the use of AIDL needs to consider thread safety issues, and it is necessary to use synchronized locks on interface methods to protect data security.

7. Please explain what Binder is and what is its function in Android Framework?

Binder is a mechanism of inter-process communication (IPC) in Android, which is used to realize cross-process communication. It is the underlying system service of Android.

In the Android Framework, the role of Binder can be summarized in the following aspects:

1. Realize inter-process communication : Binder is a lightweight IPC mechanism. In Android, Binder makes inter-process communication more efficient and convenient.
2. Provide inter-process object transfer : Binder can not only transfer basic types and simple objects, but also transfer and operate objects registered and managed in Binder between different processes.
3. Provide secure inter-process communication : Based on the Client/Server model, Binder transfers data in multiple processes, ensuring the security and independence of each process.
4. Realize multi-thread concurrency : use Binder mechanism to realize cross-process concurrent calls.

In short, Binder is the core system service in Android, which plays a key role in connecting Android applications and underlying system services, and plays an important role in the smooth operation of the Android system.

8. What is paging and how is it implemented?

Page swapping (Page swapping) is a memory management technology in the operating system, which is used to move some infrequently used pages (also called memory blocks) in the memory to the disk to release enough memory space for other process can be used. When a process needs to access a page that was scheduled to disk, the operating system brings it into memory and then accesses it.

Paging is part of the mechanism for implementing virtual memory. The virtual memory mechanism is to divide the memory into multiple pages or blocks, and each page or block is a fixed size. The virtual address space of the program is also divided into a certain number of pages or blocks. Therefore, when a program needs to access a virtual address, the operating system will first check whether the physical page corresponding to the address is in the memory, and if not, the system will transfer it into the memory, which is paging.

Paging usually uses some algorithms to select pages. Common algorithms include Least Recently Used Algorithm (LRU), First In First Out Algorithm (FIFO), Least Frequently Used Algorithm (LFU), etc. The choice of algorithm depends on different situations and application scenarios.

When implementing paging, the operating system needs to maintain a page table to record the mapping relationship between virtual addresses and physical addresses. When paging occurs, the operating system needs to update the relevant information in the page table to reflect the status of pages that have been swapped in or swapped out in memory.

In short, paging is an important technology in the operating system, which can improve the overall performance of the system by dynamically scheduling pages in memory.

9. What common system services does the Android Framework provide? What are their functions?

The Android Framework provides many commonly used system services. The following are some of the more commonly used system services and their functions:

  1. ActivityManager : Used to manage the life cycle of the application, including operations such as start, stop, and pause.
  2. WindowManager : Used to control the creation, adjustment, deletion and display of windows, as well as manage the size, position, transparency, etc. of windows.
  3. PackageManager : Used to manage and install and uninstall application packages.
  4. NotificationManager : Used to control the creation, modification, deletion and display of notifications.
  5. TelephonyManager : Provides access to mobile network and device telephony status.
  6. ContentResolver : Provides access to data storage mechanisms in the Android system, such as databases, file systems, etc.
  7. LocationManager : Used to access the device's geographic location information.
  8. SensorManager : Used to read the sensor data of the mobile phone.
  9. AudioManager : Used to control audio playback, recording and output, etc.
  10. Vibrator : Used to control the vibration of the phone.

The above system services cover a variety of application scenarios. Developers can implement the functions they want by calling system services according to their own needs. For example, TelephonyManager can be used to make calls, send text messages, etc.; LocationManager can be used to implement positioning services; NotificationManager can be used to implement functions such as message notification.

10. How to register and use BroadcastReceiver in the system?

Registering and using BroadcastReceiver can be divided into the following steps:

  1. Create a BroadcastReceiver subclass and override the onReceive() method to handle received broadcasts.
class MyReceiver : BroadcastReceiver() {
    
    
    override fun onReceive(context: Context?, intent: Intent?) {
    
    
        // 处理接收到的广播
    }
}
  1. Register the BroadcastReceiver in the application's AndroidManifest.xml file.
<receiver android:name=".MyReceiver">
    <!-- 定义广播接收的过滤器 -->
    <intent-filter>
        <!-- 定义接收的广播类型 -->
        <action android:name="com.example.ACTION_MY_BROADCAST"/>
    </intent-filter>
</receiver>
  1. Send a broadcast. The broadcast can be sent through the code, or the broadcast can be triggered through the broadcast type defined by the intent-filter in the AndroidManifest.xml file.
val intent = Intent("com.example.ACTION_MY_BROADCAST")
sendBroadcast(intent)

When sending a broadcast, you can set the broadcast data and additional information through the Intent, for example:

val intent = Intent("com.example.ACTION_MY_BROADCAST")
intent.putExtra("key", "value")
sendBroadcast(intent)

In the onReceive() method of BroadcastReceiver, broadcast data and additional information can be obtained through Intent, for example:

override fun onReceive(context: Context?, intent: Intent?) {
    
    
    val data = intent?.getStringExtra("key")
    // 处理接收到的广播数据
}

It's worth noting that some system broadcasts may require specific permissions to receive. Also, to avoid possible memory leak issues, it is recommended to unregister the BroadcastReceiver when it is not needed.


In fact, there are still many interview questions related to Framework. Due to the limited length of the article, here are only a few related interview questions and reference answers to share with you here. And in our interviews, when the interviewer asks about this content, most of them can only answer a few simple questions. If you ask more in-depth questions, some people will be confused. For this reason, here are prepared for you, Framework-related study review documents and more interview questions for reference: https://qr18.cn/AQpN4J.

"Android Framework Study Manual":https://qr18.cn/AQpN4J

  1. Boot Init process
  2. Start the Zygote process at boot
  3. Start the SystemServer process at boot
  4. Binder driver
  5. AMS startup process
  6. The startup process of the PMS
  7. Launcher's startup process
  8. The four major components of Android
  9. Android system service - distribution process of Input event
  10. Android underlying rendering-screen refresh mechanism source code analysis
  11. Android source code analysis in practice

Interview question arrangement:https://qr18.cn/CgxrRy

These questions and knowledge points recorded are only used for reference and testing your mastery of their knowledge points, not that you will definitely encounter them in the interview. The interviewer will ask shallow and deep questions according to the different situations of each individual. Therefore, we need to carefully understand each knowledge point, so that we can effectively face the interviewer's questions and solve the problems that will be encountered in the project.

Guess you like

Origin blog.csdn.net/maniuT/article/details/129994738