Android Development interview by--3. Common Android Advanced Pen questions

Source: Internet
Author: User
Tags message queue

Follow Finddreams Blog: http://blog.csdn.net/finddreams/article/details/44301359
In the previous article we have learned some basic questions about the Android written test, "Android development interview by--2." Common Android Basic pen questions
But as an experienced developer, just know the basic problem is not enough, your resume said there are more than two years of work experience, the interviewer will certainly ask some in-depth questions, see if you can answer. So in order to get a better job, we need to get to the Android advanced written question:

1. What is ANR and how to avoid it?
Anr:application not responding.
In Android, the two system services, Activity Manager and window manager, are responsible for monitoring the response of the application. Android Displays the ANR dialog box when the following conditions occur:
①. User action on the application (e.g. input events, keystrokes, touch screen events) no response within 5 seconds
②. Broadcast Receiver (Broadcastreceiver) is still not completed in 10 seconds
The Android application runs completely in a separate line approached (for example, main). This means that any operation that runs in the main thread and consumes a lot of time will cause the ANR. Because at this point, your application has no chance to respond to input events and intent broadcasts (INTENTBROADCAST).
Avoidance method: Activity should do as little as possible in its critical life-cycle methods (such as onCreate () and Onresume ()) to create operations,
Potentially time-consuming operations. For example, a network or database operation, or a high time-consuming calculation such as changing the bitmap size, should be done in the thread (or async) of the Strand.
The main thread should provide a Handler for the child thread so that it can be submitted to the main thread when it is complete.

2.Handler mechanism principle?
Andriod provides Handler and Looper to meet the communication between threads. Handler first-out principle.
The Looper class is used to manage message exchange (Messageexchange) between objects within a particular thread.
1) Looper: A thread can produce a Looper object that manages the MessageQueue (Message Queuing) of this line thread.
2) Handler: You can construct a Handler object to communicate with Looper in order to push a new message into the MessageQueue, or to receive a message sent by Looper from MessageQueue.
3) Message queue (Message Queuing): Used to hold messages placed by the thread.
4) Thread: The UI thread is usually the main thread, and the Android launcher will create a MessageQueue for it.

3. Please explain the relationship between message, Handler, message Queue, Looper in the single-threaded model.
Simply put, Handler gets the Looper object in the current thread, Looper is used to remove the message from the MessageQueue that holds the message, and then Handler to distribute and process the message.
Message queue: Used to hold a message published through Handler, usually attached to a thread that created it, can get the message queue of the current thread through Looper.myqueue ()
Handler: You can publish or process a message or manipulate a Runnable to publish a message through Handler, and the message will only be sent to the message queue associated with it, and only the messages in that message queue are processed.
Looper: Is the communication bridge between Handler and message queue, the program component first passes the message to the Looper,looper by Handler to put the message into the queue. Looper also broadcasts messages from the message queue to all
Handler:handler after receiving the message, call handlemessage for processing
Message: The types of messages that are processed by a single message in the Handlemessage method in the Handler class
In a single-threaded model, for thread communication problems, Android has designed a message queue, which can be exchanged between threads through this message queue and in conjunction with Handler and Looper components.
They are described separately below:
1. Message
Message, which is understood as information exchanged between threads, and the processing of a data background thread needs to update the UI, sending a message contains some data to the UI thread.
2. Handler
Handler processor, is the main processor of the message, responsible for the delivery of the message, the execution of the message content processing. A background thread is a sendMessage (Message) by passing in a handler object reference.
Instead of using Handler, you need to implement the Handlemessage (Message) method of the class, which is to handle these
The action content of the Message, such as the Update UI. It is often necessary to subclass Handler to implement the Handlemessage method.
3. Message Queue
Message queue, used to store messages posted through Handler, followed by FIFO execution. Each message queue will have a corresponding Handler. Handler sends a message to MessageQueue in two ways: SendMessage or post. Both messages will be inserted at the end of the message queue and
Follow the FIFO execution. However, the messages sent by these two methods are slightly different: sending a Message object via SendMessage is handled by the Handler handlemessage () function, and a Runnable object is sent via the Post method. It will execute itself.
4. Looper
Looper is the steward of the message Queue for each line thread. Android does not have Global MessageQueue, and Android automatically builds a message queue for the main thread (UI thread), but the message queue is not established on the thread. So calling Looper.getmainlooper () gets the Looper of the main thread is not NULL, but calling Looper.mylooper () gets the Looper of the current thread to be null.

4.Android thread and threads, how processes communicate with processes
1. When an Android program starts running, it will start a process separately.
By default, all Activity or Service in this program will run in this process.
By default, an Android program has only one process, but a process can have a number of Thread.
2. When an Android program starts running, a main thread mainthread is created. This thread is primarily responsible for the 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. So, each function that the Main Thread calls, its time-consuming should
The shorter the better. For more time-consuming work, you should try to give the sub-thread to do so as to avoid blocking the main thread (the main thread is blocked, causing the program to feign animation).
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 a child thread, the exception is caused.
IPC (interprocess communication) mechanism of 4.Android
IPC is the abbreviation for internal process communication and is a resource for sharing named pipes. The IPC mechanism in Android is designed to allow
Activity and service can interact at any time, so in Android, this mechanism is only applicable to activity and service communication, similar to the remote method call, similar to the C/S mode of access. Define the IPC interface by defining the Aidl interface file. The Servier end implements the IPC interface, and the Client side invokes the IPC interface local agent.

5.Android Application Framework

6.View, Surfaceview, glsurfaceview the difference between
View is the most basic, you must update the screen within the main thread of the UI, slower. The
Surfaceview is a subclass of view, similar to using a double-slow mechanism to update the screen in a new thread so that the refresh interface is faster than the view Glsurfaceview is a subclass of Surfaceview, OpenGL dedicated.
Difference: Surfaceview is a display class derived from the view base class, with the direct subclass Glsurfaceview and Videoview, you can see that GL and video playback and camera cameras are generally used Surfaceview
The essential difference between surfaceview and view is that Surfaceview is able to redraw the screen in a new, separate thread and view must update the screen in the main thread of the UI.
Updating the screen in the main thread of the UI can cause problems, such as when you're updating the screen too long, and your main UI thread is blocked by the function you're drawing. Then you will not be able to respond to keystrokes, touch screens and other messages.
When using Surfaceview because it is updating the screen in a new thread, it does not block your UI main thread. But this also brings up another problem, namely, the synchronization of events. For example, if you touch the screen, you need to surfaceview in the thread processing, it is generally necessary to have an event queue design to save touch event, which is slightly more complicated, because it involves thread synchronization.

7. What is the full name of Aidl? How does it work?
Aidl full name Android Interface definition Language (Android Interface Description Language) is an interface description language; The compiler can generate a piece of code from a aidl file, up to two through a pre-defined interface The purpose of the cross-premises object access for a process within the
section. Aidl's IPC mechanism is similar to COM or CORBA, and is interface-based, but it is lightweight. It uses a proxy class to pass values between the client and the implementation layer. If you want to use Aidl, there are 2 things to do:
1. Introduce aidl related classes.;
2. Call the class generated by the Aidl. In theory, parameters can pass basic data types and strings, and there is a derived class for bundles
when a process is going to invoke service in B-process and implement communication, we usually operate through aidl.
A project:
First we create a remoteservice.aidl file in the Net.blogjava.mobile.aidlservice package, where we customize an interface that contains the method get. The ADT plugin automatically generates a Remoteservice.java file in the Gen directory that contains an inner class named Remoteservice.stub that contains a get method for the Aidl file interface.
Description One: The location of the Aidl file is not fixed, you can arbitrarily
then define your own MyService class, in the MyService class to customize an inner class to inherit remoteservice.stub this inner class, implement the Get method. Returning an object of this inner class in the Onbind method, the system automatically encapsulates the object into a IBinder object and passes it to his caller. The
next requires the MyService class to be configured in the Androidmanifest.xml file, with the following code:

<!--registration service-->  <service  android:name  =;  <intent-filter  >  <!--specify the ID aidl to invoke the--> service;  <action  android:name  = " Net.blogjava.mobile.aidlservice.RemoteService "/>  </intent-filter ;  </service    

Why to specify the ID of the call Aidl service, is to tell the outside MyService this class can be accessed by other processes, as long as other processes know this ID, it is with this id,b project to find a project to achieve communication.
Description: Aidl does not require permissions
Project B:
First, we will copy the Remoteservice.java file generated in project A into Project B and bind the Aidl service in the Bindservice method.
The Bind Aidl service is the Remoteservice ID as the intent action parameter.
Note: If we put the Remoteservice.aidl file in a package separately, the one that we copied to the B project in the Gen directory. If we store the Remoteservice.aidl file with our other classes, we will set up the appropriate package in Project B to ensure that the Rmoteservice.java file registration is correct, we cannot modify the Remoteservice.java file
Bindservice (New Inten ("Net.blogjava.mobile.aidlservice.RemoteService"), Serviceconnection, context.bind_auto_ CREATE);
Serviceconnection onserviceconnected (componentname name, IBinder service) The service parameter in the method is an object in the MyService class in the a project that inherits the inner class of the Remoteservice.stub class.

Android Development interview by--3. Common Android Advanced Pen questions

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.