Get a quick overview of Android's key mechanisms

Source: Internet
Author: User

Transfer from http://www.jianshu.com/p/5f6d79323923

First, the Android system underlying research

The underlying knowledge is not in an article can be explained clearly, see my Android underlying research series, constantly updated.

Second, Android important points of knowledge

The following is a selection of more common knowledge points, resolutely eliminate 简单罗列答案 the way, because that can not be understood can not remember. So try to 层层递进而简单粗暴 express it in the same way. Be patient and you'll be able to handle most interview questions.

Tips:可以先阅读自己熟悉的知识点,然后再去看那些不太熟悉的。
1. Activity Start Mode
什么是启动Activity?

In general, you create an Activity object and then manage the activity into a task stack (including switching, closing, and so on). So the question is, if the activity object is already in the stack, do I have to create a new object? Android offers four options:

    • Create a new activity on top of the stack, regardless of whether it is in the stack:standard模式
    • If the activity I want to open is right on top of the stack, use it directly, or else create a new one:singleTop模式
    • This is more overbearing. That is, if the activity already exists in the stack, then when you want to open the activity, even if there are other activity on the top of the stack, it will be removed and become the top of the stack. And if not, create them as well. :singleTask模式
    • This is more overbearing. When this activity is opened, a stack is created directly, and the activity is created into the new stack without squeezing a stack with others. To open this activity later, jump directly to the new stack. : singleInstance模式 (that is, to create only one instance, which is shared with the other task stacks in a single task stack)

Boot Mode 2, Activity (or component) communication mechanism
Activity(组件)之间何时需要通信?

In general, communication mechanisms are involved when you need to pass data from one activity (component) to another activity (component). For example, open a new activity and pass data to it.

如何来通信/传递数据?

More commonly used is the messenger of the Android system: Intent .
The function of the intent is to bring the data to be delivered and then go to the specified destination.
So the question is, what if we bring all sorts of data? And how do you navigate to find your destination?

如何带上各种各样的数据?

Just like a letter, you can't put a pile of messy stationery directly to the Messenger and send it to someone, and it will be filled with envelopes.
Android Also, the data will be packaged first, and then to the Messenger, here is involved bundle . Yes, we are 把一堆凌乱的数据放在一个bundle , and then we 把bundle给Intent can.

 如何导航找到目的地呢?

If you want to mail something there are two ways: 1, write on the envelope, 详细地址 2, if you want to mail useless quilt books to the disaster area, but you do not care to send to which disaster area, you can write 帮我随便寄到一个灾区去 , and then the Post Office administrator will be in the nationwide inquiry, if the disaster areas such as Wenchuan, then mailed to that.
Also in Android, there are explicit Intent and implicit Intent, which specify a specific component name, such as specifying the target activity, which does not specify a name, only a description (Intent-filter). Then Android will search based on this description.

However, with regard to data packaging, one problem is that if you pass the normal data directly into the bundle, but if you want to pass the object.

如何在bundle中打包并传递对象呢!?
3. Object serialization mechanism

As we have already learned, we are going to use bundles to package the data and then hand it to the messenger intent when communicating between components. So that's the problem?

bundle如何打包数据呢?

The data here we are divided into two, one is 原型数据 , such as int, string, etc., can be directly packaged and passed. Here's how to package:

bundle.putInt("key1", 20);bundle.putString("key2", "hello");

The other is java对象 that this is not directly to the bundle, but to pass the 序列化 bundle, and then the object will be transmitted in the form of a binary stream, until the target component receives the bundle, to the object binary data to 反序列化 get the real object.

那么如何对对象进行序列化呢?

Two different methods.

    • Serializable serialization method based on Java language: object class to implement Serializable interface;
    • Parcel serialization method based on Android system: Implement Parcelable interface.
      As long as the class implements both interfaces, the object can be stored in the bundle:
Object);bundle.putParcelable(Key, Object);

So

 这两种机制有何区别?
    • The serialize mechanism in Java is to convert an object into a byte stream 存储 on an external device, at the time of the 需要 重新生成 object (Adoption java反射 mechanism). Mainly used for external devices to save object state, network transmission objects and other scenes. The disadvantage is to produce a lot of intermediate objects and cause a certain GC (garbage collection), in short Serialize更慢 ;
    • The parcel mechanism provided by Android is 移动设备 a 轻量级高效 mechanism for object serialization. The entire process is in memory and does not involve external devices, and the original object is read when deserializing, without creating a new object. Simply put, Parcel更快 but it 使用复杂 .

In this step we know that to serialize the object before it can be put into bundles passed to other components by intent,

为什么需要对对象序列化后才能通过intent在组件间传递呢!?

Take StartActivity (Intent) as an example, when the function starts B activity from activity A, it also packages the data to intent and passes it to B. In other words, if the data in the intent has a Java object, it is necessary to serialize the object to be passed to B.
Here's why: (see the intent mechanism for easy learning from the source point of view)
Because in the process of starting B activity, it is necessary to 离开应用程序所在进程 call the native method to 进入linux kernel进程 perform the actual operation of the activity switch. After completion, then re-transfer the data in the tape 回到应用程序进程 , the original packaging data to parse.
That is, and 在启动B Activity过程中,所打包的数据要经过不同进程:应用程序->linux kernel->应用程序 java对象是无法直接在进程间传输的 (see below), so we need to serialize the Java object in advance so that it can withstand 跨进程传输 the test behind it.

4. Inter-process communication mechanism IPC
什么是进程?

In simple terms, a process often represents an application. The operating system allocates a certain amount of resources to each process 内存空间 , and the process runs in that memory.

什么是进程间通信?

Generally, in order to ensure security, the process obtains a memory space when an abstract memory is mapped to the actual piece of physical memory, thereby, 每个进程都无法访问其他进程所在内存里的数据 . For example, the mobile phone opened QQ and a bank client, the operating system will create two separate memory area for them to run, and QQ process is unable to access the bank client data, which ensures the security of the data.
However, the disadvantage is that the process can not directly transfer data, so to introduce a unique inter-process communication mechanism.

进程间可以直接传递对象吗?

As we know before, the communication between different processes means 在两块不同的物理内存间传递数据 . First, the raw data, such as int string data, can be passed, so long as you describe what type of data it is, you can recover from the target process. But the object data, and 如果直接把对象传递过去,目标进程是无法复原的 therefore not recognized.


Memory Area Data communication
Android系统中不同应用程序之间通信。一个进程代表一个应用程序。

Inter-process communication
附加:IPC机制中很重要的是Binder机制,利用Binder实现不同进程间传递。这部分内容面试一般不会问的太难。目前本人还在整理该部分内容,继续更新中。
5. Multithreading Management

The above mentioned inter-process communication, the following explanation of the Android multi-threaded management, will explain the inter-thread communication.
First, the basic concept of threading is explained here, as long as the thread is a lightweight process that performs the actual operation in a limited amount of process memory space resources.

Thread 和 Worker Thread?

A main thread, also called the UI thread, is automatically created when the application is turned on, and the primary function is to complete UI界面绘制和与用户交互 . Other threads other than the main thread are called child threads or worker thread. These threads are often used 执行耗时操作 such as network communication or database access.
If a time-consuming operation is performed in the main thread, it is not possible to draw an interface or respond to user actions during execution, which, in the view of the user 卡顿 , may result in a ANR phenomenon that destroys the user experience.

主线程是线程不安全的

Many places on the Web explain 线程不安全 not why, but rather on the ' interpretation ' result, that is, the UI control can only operate in the main thread, but not in other threads, because the thread is not secure (because it is threaded).
This explanation not only explains why the thread is not secure, but rather makes people feel that the Android system restricts the UI elements to be manipulated only in the main thread, and is easily misunderstood as it is not very secure?
My explanation is:
First of all we are indeed able to manipulate UI elements in a child thread through code, and then cause the program to crash. Assuming that the main thread is secure, it is safe to assume that even if we 不小心 operate the UI on a child thread, we do not have to worry because the system will block the operation. In fact, the system 并未屏蔽 , as long as the developer operates UI elements in a child thread, is likely to cause a crash. That's why we said the main thread in the Android system is not secure.

附加知识:什么是线程安全/不安全?在多线程环境里,往往一段代码会被多个线程分别执行。而常见的情况是一个线程执行了该段代码的一部分后,会被另一个线程抢走时间片又去执行这段代码,并修改其中变量。当原线程再次回来继续运行时,其实里面的变量已经被别人改动了但它却不知,最后会导致错误。这种线程就是不安全的。而安全的线程在执行一段代码时,只要没执行完,其他线程就不能来执行这段代码或修改变量知道它执行完。

So

多个线程间时如何通信的呢?

Here relates to another set of mechanisms handler, Looper, MessageQueue mechanism, in my other article has 生动详细 the description, see handler realization mechanism
To put it simply, each thread has three things above, such as thread A wants to send a message to thread B, then threads a 调用线程B的handler sends a message, the message will be 自动发送线程B的消息队列 in thread B looper在不断遍历这个队列 , and if a new message is found, it will be processed automatically.

Problems:

Not Response)现象呢?

Try to perform time-consuming operations (such as network requests or database reads, etc.) in a child thread, or a resource-intensive operation, and use handler to notify the main thread to make an update when the child thread finishes executing.

6. Memory management mechanism

As mentioned earlier, each application runs in a separate process (actually a separate Dalvik process), and the system allocates a fixed amount of memory to it. Memory 内存泄漏 Leak and (OOM) are often used in RAM, 内存溢出 where memory overflows are very serious and often cause programs to crash. Therefore, it is very important to manage memory reasonably and effectively.
Detailed management (including the interview Focus: image caching mechanism ) can refer to my other article: Android Storage Management and optimization

Do not repeat here.

Just talk about three kinds of image caching ideas:

    • LRU Algorithm : Set the maximum number of cached pictures, when the number of pictures exceeds the maximum value long deleted using fewer pictures, thereby optimizing memory.
    • FTU Algorithm : Sets the cache time limit for a picture, which is deleted when the time limit is reached, from the last use.
    • FMU Algorithm : Sets a fixed-size cache space to delete the maximum size of a picture when the space limit is reached.
7. Next Step:

The article is still in the update, the reader is interested in the topic can reply in the comments



Wen/iam_wingjay (author of Jane's book)
Original link: http://www.jianshu.com/p/5f6d79323923
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Get a quick overview of Android's key mechanisms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.