Android development interview post-3. common Android advanced pen questions, android advanced

Source: Internet
Author: User

Android development interview post-3. common Android advanced pen questions, android advanced

Follow finddreams blog: http://blog.csdn.net/finddreams/article/details/44301359
In the previous article, we have learned some basic questions about the Android test. Android development interview: 2. Common basic Android test questions
However, as an experienced developer, it is not enough to know the basic questions. If your resume says you have more than two years of work experience, the interviewer will certainly ask some in-depth questions, check whether you can answer the question. So in order to find a better job, we also need to take a look at the Android advanced test questions:

1. What is ANR and how to avoid it?
ANR: Application Not Responding.
In Android, the activity manager and window manager system services are responsible for monitoring application responses. Android displays the ANR dialog box when the following conditions occur:
① The user's operations on the application (such as input events, buttons, and touch screen events) do not respond within 5 seconds.
②. The broadcast receiver (BroadcastReceiver) has not been executed within 10 seconds.
Android applications run completely in an independent thread (such as main ). This means that any operation running in the main thread that consumes a lot of time will trigger ANR. At this time, your application has no chance to respond to input events and Intentbroadcast ).
Avoidance Method: The Activity should do as few creation operations as possible in its key lifecycle methods (such as onCreate () and onResume,
Potential time-consuming operations. For example, network or database operations, or high-time computing, such as changing the bitmap size, should be completed in the Child thread (or asynchronous mode.
The main thread should provide a Handler for the sub-thread so that it can be submitted to the main thread upon completion.

2. How does Handler work?
Andriod provides Handler and logoff to satisfy inter-thread communication. Handler first-in-first-out principle.
The logoff class is used to manage MessageExchange between objects in a specific thread ).
1) Logoff: A thread can generate a logoff object to manage MessageQueue (Message Queue) in this thread ).
2) Handler: You can construct a Handler object to communicate with logoff, so as to push new messages to MessageQueue; or receive messages sent by logoff from MessageQueue.
3) Message Queue: used to store messages placed by threads.
4) thread: the UI thread is usually the main thread, and Android will create a MessageQueue for it when starting the program.

3. Explain the relationship among Message, Handler, Message Queue, and logoff in the single-thread model.
To put it simply, Handler gets the logoff object in the current thread, and logoff is used to retrieve the Message from the MessageQueue storing the Message, and then Handler distributes and processes the Message.
Message Queue (Message Queue): used to store messages published through Handler. It is usually attached to a thread that creates it. You can use logoff. myQueue () to obtain the Message Queue of the current thread.
Handler: You can publish or process a message or operate on a Runnable. When you publish a message through Handler, the message will only be sent to the Message Queue associated with it, and you can only process the messages in the message queue.
Logoff: a bridge between Handler and message queue. The program component first passes the message to logoff through Handler, and logoff puts the message into the queue. Logoff also broadcasts messages in the message queue to all
Handler: Handler receives the message and calls handleMessage for processing.
Message: the type of the Message. A single Message is obtained from the handleMessage method in the Handler class for processing.
In the single-thread model, Android designs a Message Queue (Message Queue) for thread communication. The Message Queue can be used between threads to exchange information with Handler and logoff components.
We will introduce them as follows:
1. Message
Message, which is understood as information exchanged between threads. When the background thread for data processing needs to update the UI, the Message contains some data to the UI thread.
2. Handler
Handler is the main Handler of the Message, responsible for sending the Message and executing the Message content. The background thread uses the passed Handler object reference to sendMessage ).
When Handler is used, the handleMessage (Message) method of the implement class is required.
Message operation content, such as Update UI. It is usually necessary to subclass Handler to implement the handleMessage method.
3. Message Queue
Message Queue is a Message Queue used to store messages published by Handler and run first-in-first-out. Each message queue has a corresponding Handler. Handler sends a message to messagequeue in two ways: sendMessage or post. Both messages are inserted at the end of the message queue and
First-in-first-out. However, messages sent using the two methods are executed in a slightly different way: a message object is sent using sendMessage, which will be processed by the handleMessage () function of Handler; the post method sends a runnable object, which is executed by itself.
4. Logoff
Logoff is the manager of the Message Queue in each line. Android does not have Global MessageQueue, while Android automatically creates Message Queue for the main thread (UI thread), but no Message Queue is created in the Child thread. Therefore, the logoff value of the main thread obtained by calling logoff. getmainlogoff () is not NULL, but the logoff value of the current thread may be NULL by calling logoff. mylogoff.

4. How to communicate between threads and threads in Android and between processes
1. When an Android program starts running, a Process is started separately.
By default, all the activities or services in this program run in this Process.
By default, an Android program also has only one Process, but a Process can have many threads.
2. When an Android program starts running, a main thread MainThread is created. This Thread is mainly responsible for display, update, and control interaction of the UI interface, so it is also called the UI Thread.
When an Android Program was created, a Process presented a single-threaded model-that is, the Main Thread,
All tasks are run in one thread. Therefore, the time consumed for every function called by Main Thread should be
The shorter the value, the better. For time-consuming work, we should try to hand it over to the sub-thread to avoid blocking the main thread (the main thread is blocked, and the program will be suspended ).
3. Android single-threaded model: Android UI operations are not thread-safe and must be executed in the UI thread. If you modify the UI directly in the Child thread, an exception occurs.
4. Android IPC (inter-process communication) Mechanism
IPC is short for internal process communication and is a resource sharing "named Pipeline. The IPC Mechanism in Android is used to enable
Activity and Service can interact with each other at any time. Therefore, this mechanism in Android is only applicable to communication between Activity and Service, similar to remote method call and access in C/S mode. Define the IPC interface by defining the AIDL interface file. The Servier side implements the IPC interface, and the Client side calls the IPC interface as a local proxy.

5. Android Application Framework

6. Differences between View, surfaceView, and GLSurfaceView
View is the most basic. You must update the screen in the main UI thread, which is slow.
SurfaceView is a subclass of view. Similar to the double-easing mechanism, SurfaceView updates the screen in the new thread. Therefore, refreshing the interface is faster than the view. GLSurfaceView is a subclass of SurfaceView and is dedicated to opengl.
Difference: SurfaceView is a display class derived from the View base class. Its direct sub-classes include GLSurfaceView and VideoView. It can be seen that GL, video playback, and Camera cameras generally use SurfaceView.
The most essential difference between SurfaceView and View is that surfaceView can be re-painted in a new separate thread, and View must be updated in the main thread of the UI.
Updating the screen in the main UI thread may cause problems. For example, if you update the screen too long, your main UI thread will be blocked by the function you are painting. Then, messages such as buttons and touch screens cannot be responded.
When surfaceView is used to update the screen in a new thread, it will not block your main UI thread. But this also brings about another problem, that is, event synchronization. For example, if you touch the screen, you need to process the thread in surfaceView. Generally, you need an event queue design to save the touch event. This is a little more complicated because it involves thread synchronization.

7. What is the full name of AIDL? How to work?
The full name of AIDL is Android Interface Definition Language (Android Interface Description Language), which is an Interface description Language. The compiler can generate a piece of code through the aidl file and implement two processes through a predefined Interface.
The purpose of cross-border object access in a communication process. The IPC Mechanism of AIDL is similar to that of COM or CORBA and is based on interfaces, but it is lightweight. It uses the proxy class to pass values between the client and the Implementation Layer. If you want to use AIDL, you need to do two things:
1. Introduce related classes of AIDL .;
2. Call the class generated by aidl. Theoretically, the parameter can pass the basic data type and String, and the Bundle derived class.
When process A is going to call the service in process B and implement communication, we usually operate through AIDL.
Project:
First, create a RemoteService. aidl file in the net. blogjava. mobile. aidlservice package. In this file, you can customize an interface that contains the get method. The ADT plug-in will automatically generate a RemoteService. java file under the gen Directory, which contains an internal class named RemoteService. stub, which contains the get method of the aidl file interface.
Description 1: the location of the aidl file is not fixed and can be arbitrary.
Then define your own MyService class. In the MyService class, define an internal class to inherit the internal class RemoteService. stub and implement the get method. The onBind method returns the object of this internal class. The system will automatically encapsulate this object as an IBinder object and pass it to its caller.
Configure the MyService class in the AndroidManifest. xml file. The Code is as follows:

<! -- Register the service --> <service android: name = ". MyService"> <intent-filter> <! -- Specify the ID of the AIDL service to be called --> <action android: name = "net. blogjava. mobile. aidlservice. RemoteService"/> </intent-filter> </service>

Why do you need to specify the ID of the AIDL service to be called, that is, to tell the outside world that the MyService class can be accessed by other processes, as long as other processes know this ID, it is precisely with this ID, project B can find Project A to implement communication.
Note: AIDL does not require permissions.
Project B:
First, we need to copy the RemoteService. java file generated in Project A to Project B and bind the aidl service to the bindService method.
Binding the AIDL Service uses the RemoteService ID as the intent action parameter.
NOTE: If we put the RemoteService. aidl file in a bag separately, copy the package under the gen directory to Project B. If we set the RemoteService. aidl files are stored together with other classes, so we need to create corresponding packages in Project B to ensure RmoteService. the java file registration is correct. We cannot modify RemoteService. java files
BindService (new Inten ("net. blogjava. mobile. aidlservice. RemoteService"), serviceConnection, Context. BIND_AUTO_CREATE );
The service parameter in the onServiceConnected (ComponentName, IBinder service) method of ServiceConnection is the object that inherits the internal class of RemoteService. stub in MyService class of Project.

8. About AndroidOOM and how to avoid it?
The Android virtual machine is a register-based Dalvik. Its maximum heap size is generally 16 M, and some machines are 24 M. Therefore, the memory space we can use is limited. If our memory usage exceeds a certain level, an OutOfMemory error occurs.
1. Why is there insufficient memory? I think there are two main reasons: Due to program errors, some resources (such as Context) can be referenced for a long time, causing memory leakage and resources cannot be released. Saves multiple objects (such as Bitmap) that consume too much memory, resulting in memory exceeding the limit.
②. How to Avoid optimization?
1. Avoid using static member variables to reference instances that consume too much resources, such as Context. Use the Application Context whenever possible. Because the Application Context has a long life cycle, it will not cause memory leakage if referenced. Use WeakReference instead of strong reference. For example, you can use WeakReference mContextRef;
2. threads are also an important source of Memory leakage. The main cause of thread Memory leakage is the uncontrollable thread lifecycle. Change the internal class of the thread to a static internal class. 3. Bitmap problem: most people with OutOfMemory problems are due to Bitmap problems. Because Bitmap occupies too much memory, it is a "super fat man", especially a high-resolution image. If you want to display multiple images, the problem is even more significant.
How can we solve the memory problems brought by Bitmap?
Timely destruction. Although the system can confirm that the memory allocated by Bitmap will eventually be destroyed, it may exceed the java heap limit because it occupies too much memory. Therefore, when using Bitmap, You need to delete the recycle in time. Recycle is not sure that Bitmap will be released immediately, but it will imply to the Virtual Machine: "The image can be released ". Set a certain sampling rate. Sometimes, the area we want to display is very small and it is not necessary to load the entire image. Instead, we only need to record a reduced image. At this time, we can set a certain sampling rate, therefore, the occupied memory can be greatly reduced. The following code: BitmapFactory. Options options = new BitmapFactory. Options (); options. inSampleSize = 2; // The image width and height are 1/2 of the original image, that is, the image is 1/4 of the original image.
4. Use SoftRefrence skillfully. Sometimes, Bitmap does not retain its reference, so we cannot call the Recycle function. By using soft references, Bitmap can be effectively released when memory is insufficient.
5. Release Cursor in time;
6. Use a 9path image whenever possible. The Adapter must use convertView for reuse;

9. Introduction to AsyncTask
When developing Android mobile clients, multithreading is often used for operations. We usually place time-consuming operations in separate threads for execution, avoid occupying the main thread and bring a poor user experience. However, the main thread (UI thread) cannot be operated in the Child thread, and an error occurs when the UI thread is operated in the Child thread. Therefore, android provides a Handler class to update the UI thread in the Child thread, and uses the message sending mechanism to update the UI interface and present it to the user. This solves the problem of updating the UI of the sub-thread. However, time-consuming task operations always start some anonymous sub-threads. Too many sub-threads impose a huge burden on the system, resulting in some performance problems. Therefore, android provides a tool class AsyncTask, which is called asynchronous task execution. This AsyncTask is born to process some time-consuming tasks in the background, which brings users a good user experience and looks much more elegant in programming syntax, you can complete asynchronous operations and refresh the user interface without the child thread and Handler.

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.