Summary of the key Android interview questions

Summary of the key Android interview questions

1. Please explain the relationship between Message, Handler, Message Queue, and Looper in a single-threaded model.

Take the main thread as an example. When the main thread starts, it calls the Looper.prepare() method, initializes a Looper, puts it in Threadlocal, and then calls Looper.loop() to continuously traverse the Message Queue.

The creation of Handler depends on the Looper in the current thread. If the current thread does not have a Looper, Looper.prepare() must be called. Handler, sendMessage to MessageQueue, Looper constantly

Take the message from the MessageQueue and call back the handleMessage method.

2. If there is a 100M file that needs to be uploaded to the server, but the server form can only upload a maximum of 2M, what method can be used?

I think this question is not very clear. First of all, using the http protocol to upload data, especially in Android, has nothing to do with the form. Traditionally in the web, writing a file upload in the form, in fact, the browser does

The purpose is to parse our data into a string and send it to the server in a stream. The POST method is used to upload files, and the POST method has no size limit.

Back to the topic, we can say that if we can only upload 2M each time, then we may have to truncate the file and upload it separately.

3. What is the difference between memory overflow and memory leak? When does a memory leak occur? What are the methods for memory optimization?

Memory overflow can be understood as the memory required for software (application) to run exceeds its maximum available memory.

Memory leak means that we use a certain memory space but do not release it after use.

Memory optimization: The part of Android that is prone to memory overflow is the loading of images. We can use image compression and LruCache caching to control the memory that images can use.

There is also a timely closure of resource-consuming objects, such as Database Conn, various sensors, Services, etc.

4. In what scenarios is AsyncTask used? What are its shortcomings? How to solve them?

The scenario in which AsyncTask is used is that we need to perform some time-consuming operations, update the main thread after the time-consuming operations are completed, or update the UI of the main thread during the operation.

Defect: AsyncTask maintains a thread pool of 128 in length, which can execute 5 worker threads at the same time, and a buffer queue. When there are 128 threads in the thread pool and the buffer queue is full, if

Submitting a task to the thread at this time will throw a RejectedExecutionException.

Solution: A control thread handles the call of AsyncTask to determine whether the thread pool is full. If it is full, the thread sleeps, otherwise it requests AsyncTask to continue processing.

5. Is there any size limit for Activity to save data using SharedPreferences?

I really can't find this...

6. Is there any limit on the size of data passed between Activities via Intent?

It looks like 40K.

7. Is there any limit on the size of files placed in the assest folder?

The assets directory is more like an appendix-type directory. Android does not generate IDs for files in this directory and save them in the R class, so it is less compatible with some classes and methods in Android.

At the same time, since you need a string path to get the file descriptor of this directory, the access speed will be slower. But putting some files in this directory will make some operations more convenient.

For example, copy a database file to the system memory. Please note that you cannot reference files in the assets directory in the Android XML file, you can only access them through the AssetManager.

These files, database files and game data are more suitable to be placed in this directory. In addition, the information about assets and raw on the Internet is the same, so the information about individual files in the two is not very useful.

The **error** description that the size cannot exceed 1M is also spreading, that is, if you read a file larger than 1M, it will report "Data exceeds UNCOMPRESS_DATA_MAX (1314625 vs 1048576)".

IOException, and also derived a variety of solutions. I personally think there should be no such restrictions. In order to verify this statement, I wrote a demo and found that nearly 5M of compressed files were in assets and raw.

All can be accessed normally, so let me correct it here. In theory, as long as the package does not exceed the Android APK size limit of 50M, there is no problem. Of course, it is not ruled out that it is a very early Android

Sometimes aapt limits the size of these two folders during compilation due to device hardware reasons. If this is the case, the newer version of ADT should not have this problem.

From: http://my.eoe.cn/futurexiong/archive/5350.html

8. To start a program, you can click the icon on the main interface to enter, or you can jump to it from a program. What is the difference between the two?

This is because when launching the program (the main interface is also an app), I found that there is an activity in this program that is set to <category android:name="android.intent.category.LAUNCHER" />.

So the launcher will bring up the icon and put it on the main interface. When the user clicks the icon, an Intent is sent:

Intent intent = mActivity.getPackageManager().getLaunchIntentForPackage(packageName);

mActivity.startActivity(intent);

You can jump to any allowed page. For example, if a program can be downloaded, the page actually downloaded may not be the home page (it may also be the home page). In this case, you still need to construct an Intent and startActivity.

The action in this intent may have multiple views, including download. The system will select the program or page that can be opened for your intent based on the functions registered by the third-party program with the system. So the only point is

The difference is that the action of the intent started by clicking the icon is relatively simple, while jumping or starting from the program may have more styles. The essence is the same.

9. Understanding the affinity between programs.

1. By default, all activities of an application have the same affinity and are inherited from the application. The affinity of the application is the package name of the manifest by default.

2. Affinity is like an ID card for Activity, which can tell the Task that it is a member of it.

3. Applications:

a: Reselect a suitable host Task for the Activity based on affinity;

b: cooperate with the allowTaskReparenting attribute;

c: Start the Activity using an Intent with the FLAG_ACTIVITY_NEW_TASK flag set.

10. Can different Activities of the same program be placed in different Task stacks?

You can put it in different tasks. You need to set different affinity attributes for different activities, and the Intent that starts the activity needs to include the FLAG_ACTIVITY_NEW_TASK flag.

11. The life cycle of Activity when switching between horizontal and vertical screens.

1. If you do not set the android:configChanges of the Activity, the screen switching will re-call each life cycle. It will be executed once when switching to landscape mode and twice when switching to portrait mode.

2. When setting the Activity's android:configChanges="orientation", the screen switching will still call each life cycle again. It will only be executed once when switching between horizontal and vertical screens.

3. When setting the Activity's android:configChanges="orientation|keyboardHidden", the screen switching will not call each life cycle again, but only execute the onConfigurationChanged method

12. What is the full name of AIDL? How does it work?

The full name is: Android Interface Define Language

In Android, each application can have its own process. When writing UI applications, Services are often used. How to pass objects between different processes? Obviously, cross-process memory sharing is not allowed in Java.

Therefore, to transfer objects, you can only split the objects into simple forms that the operating system can understand in order to achieve the purpose of cross-border object access. In J2EE, using RMI, objects can be transferred through serialization. In Android,

Use AIDL. In theory, AIDL can transfer Bundles, but in practice it is more troublesome to do.

AIDL (AndRoid Interface Description Language) is an interface description language; the compiler can generate a piece of code from the aidl file to achieve the purpose of internal communication between two processes through a pre-defined interface. If necessary

In an Activity, to access an object in another Service, you need to first convert the object into parameters that AIDL can recognize (possibly multiple parameters), and then use AIDL to pass these parameters. At the receiving end of the message, use

These parameters are assembled into the objects you need. AIDL's IPC mechanism is similar to COM or CORBA, it is based on interfaces, but it is lightweight. It uses proxy classes to pass values ​​between the client and the implementation layer. If you want to use AIDL,

There are two things to do: 1. Import AIDL related classes. 2. Call the class generated by aidl.

AIDL creation method:

AIDL syntax is very simple and can be used to declare an interface with one or more methods, as well as passing parameters and return values. Due to the needs of remote calls, these parameters and return values ​​are not of any type.

The following are some of the data types supported by AIDL:

1. Simple Java programming language types that do not require import declarations (int, boolean, etc.)

2. String and CharSequence do not require special declaration

3. List, Map and Parcelables types. The data members contained in these types can only be simple data types, String and other unsupported types.

(In addition: I didn't try Parcelables, it doesn't compile under Eclipse+ADT, maybe it will be supported in the future

13. Are the DVM process, the Linux process, and the application process the same concept?

The DVM process is the dalivk virtual machine process. Each Android program runs in its own process. Each Android program system will assign a separate liunx uid (user id) to it.
Each dvm is a process in linux. So these two processes are one process.

<<:  Interpreting ASP.NET 5 & MVC6 Series (3): Project Release and Deployment

>>:  Summary of Web App Development Skills

Recommend

Xiaohongshu marketing method!

The "Insight Report on the Rise of Domestic ...

iOS 9 Learning Series: UIKit Dynamics

UIKit Dynamics was first introduced in iOS 7, all...

5 steps to write a hit article with 100,000+ views (Part 1)

What is your purpose in writing? No matter what yo...

Xiaomi Content Report Card

Xiaomi has three core businesses: smartphones, sm...

Here's why Siri is hard to use, and it's about to be solved

When many people talk about Siri, they often comp...

Apple HomePod? We'll just see if we actually buy it or Echo

According to AppleInsider, a survey released on W...

APP promotion: A brief discussion on the cold start of Internet products!

No matter how cool the concept of a product is or...

A cold start project from 0 to 1: My operational thinking

This series of articles mainly discusses the four...

Is it worth upgrading old devices to iOS 8? Security should not be ignored

According to the New York Times, iOS 8 has been o...