Android Development interview by--4. Common Android Advanced pen questions (update ... )

Source: Internet
Author: User


 Android Development (+)


Copyright NOTICE: This article for the Dream-finddreams original article, please pay attention to: Http://blog.csdn.net/finddreams



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]
(http://blog.csdn.net/finddreams/article/details/44219231)
After the written test, is the interviewer asked you a question, compared to the written test, we tend to have a bit of tension, because do not know what the interviewer will ask strange questions, if asked the question just don't understand, then more scalp numbness. In order to be able to get a satisfactory performance in the interview question, I have specially arranged the technical question which the common interviewer asks:
Android Development interview by--5. Common interviewer questions Android question ①
Android Development interview by--6. Common interviewer Questions Android ② (update ... )



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, Active Manager and window manager, are responsible for monitoring the response of the application. When the following occurs, Android displays the ANR dialog box:  
①. The user's actions on the application (such as input events, keystrokes, touch screen events) do not respond within 5 seconds  
②. Broadcast Receiver (Broadcastreceiver) &NBSP is still not executed within 10 seconds; the
Android application runs completely on 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.  
Surfaceview is a subclass of view, similar to using a double-slow mechanism to update the screen in a new thread so the refresh interface is faster than the view Glsurfaceview is Surfaceview subclass, OpenGL dedicated.  
Difference: Surfaceview is a display class derived from the view base class, the direct subclass has Glsurfaceview and Videoview, you can see that GL and video playback and camera camera are generally used Surfaceview   The most essential difference between the
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.  
then updating the screen in the main thread of the UI can cause problems, such as when you update the screen too long, your main UI thread will be blocked by the function you are drawing. Then you will not be able to respond to keystrokes, touch screens and other messages. &NBSP
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 in-process   through a pre-defined interface, and
for the purpose of cross-premises object access for the communication process. 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, you need to complete 2 things:  
1. Introduction of AIDL related classes.;  
2. Call the aidl generated by the class. Theoretically, parameters can pass the basic data type and string, as well as the derived class of the bundle  
When a process is going to invoke a service in B-process and implement communication, We usually do this 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: Aidl file location is not fixed, you can arbitrarily  
then define your own MyService class, In the MyService class, customize an inner class to inherit the inner class of remoteservice.stub and 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.  
Next you need to configure the MyService class in the Androidmanifest.xml file, with the following code:


<! -- registration service -- >
<service android:name=".MyService">
<intent-filter>
<! -- specifies the ID of the call to the Aidl 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.


8. What about Androidoom and how to avoid it?
Android virtual machine is a register-based Dalvik, its maximum heap size is generally 16M, and some machines are 24M. So the amount of memory space we can use is limited. OutOfMemory errors occur if our memory footprint exceeds a certain level.
①. Why is there a situation where memory is not enough? I think there are two main reasons: due to our program errors, long-term maintenance of certain resources (such as the context) of the reference, resulting in memory leaks, resources caused by the failure to release. Multiple objects that consume too much memory (such as bitmap) are saved, causing memory to exceed the limit.
②. How to avoid optimization?
1, should try to avoid the static member variable reference resources too expensive instances, such as context. The context tries to use application context as much as possible because the application context has a longer life cycle, and it does not issue a memory leak. Use WeakReference instead of strong references. For example, you can use WeakReference mcontextref;
2. Threads are also an important source of memory leaks. The main reason for a thread's memory leak is that the thread life cycle is not controllable. Changes the internal class of a thread to a static inner class. 3, Bitmap problem: Can say that the majority of people who appear outofmemory problem, are because of bitmap problem. Because bitmap occupies too much memory, it is a "super fat", especially the resolution of large pictures, if you want to display more than the problem is more significant.
How to solve the memory problem that bitmap brings to us?
Timely destruction. Although the system is able to confirm that the memory allocated by bitmap will eventually be destroyed, it is likely to exceed the Java heap limit because it consumes too much memory. Therefore, in the use of bitmap, should be timely recycle off. Recycle is not sure that the bitmap will be released immediately, but will give the virtual machine a hint: "The picture can be released."  Set a certain sample rate. Sometimes, we want to show the area is very small, there is no need to load the entire picture, and only need to record a reduced image, when you can set a certain sampling rate, then you can greatly reduce the memory occupied. As in the following code: Bitmapfactory.options Options = new Bitmapfactory.options (); Options.insamplesize = 2;//Picture width is the original One-second, that is, the picture is the original One-fourth.
4. Clever use of soft references (softrefrence) Sometimes we don't keep references to them after we use bitmap, so we can't call the Recycle function. By using soft references skillfully, the bitmap can be effectively released when the memory is not fast enough.
5, the timely release of the cursor;
6, try to use 9path pictures. Adapter to use Convertview multiplexing and so on;


9. Introduction of Asynctask
When developing an Android mobile client, you tend to use multithreading to do it, and we typically put time-consuming operations on separate threads to avoid taking the main thread and creating a bad user experience for the user. However, the main thread (UI thread) cannot be manipulated in a child thread, and an error occurs when manipulating the UI thread in a child thread. So Android provides a class Handler to update the UI thread in a child thread, updating the UI interface with a message-sending mechanism and presenting it to the user. This solves the problem of the child thread updating the UI. However, time-consuming task operations always start some anonymous sub-threads, and too many sub-threads bring a huge burden on the system, resulting in some performance problems. So Android provides a tool class Asynctask, which, as the name implies, executes tasks asynchronously. This asynctask is born to handle some of the more time-consuming tasks behind the scenes, to give users a good user experience, from the programming syntax is much more elegant, no longer need child threads and handler to complete the asynchronous operation and refresh the user interface.


10. Talk about the MVC pattern principle, its use in Android
A: The official Android recommendation application is developed using MVC mode. What is MVC?
MVC is an acronym for Model,view,controller, and MVC consists of three parts:
L, Model object: is the main part of the application, and all business logic should be written on that layer.
2. View object: Is the part of the application that is responsible for generating the user interface. It is also the only layer that the user can see in the entire MVC architecture, receiving input from the user and displaying processing results.
3, controller (Control) object: is based on user input, control user interface data display and update Model object state part, controller more important a navigation function, want to use the user to start the related events, to M processing.
Android encourages weak coupling and component reuse, and MVC in Android is embodied in the following:
1) View layer: The general use of XML file interface description, the use of the time can be very convenient to introduce, of course, how you learn more about Android, you can think of Android can also use javascript+ HTML and so on as the view layer, of course, there is a need for the communication between Java and JavaScript, fortunately, Android provides a very convenient communication between them implementation.
2) control layer (Controller): The task of Android control layer usually falls on the shoulders of many acitvity, this phrase also implies do not write code in acitivity, to through activity delivery model business Logic layer processing, Another reason for this is that the response time of Acitivity in Android is 5s, and if time-consuming operations are put here, the program is easily recycled.
3) Model: the operation of the database, the operation of the network, etc. should be processed in the model, of course, business calculations and other operations must be placed in the layer.

11. Describe the Android digital signature according to your own understanding.
A: (1) All applications must have a digital certificate and the Android system will not install an application without a digital certificate
(2) The digital certificate used by the Android package can be self-signed and does not require an authoritative digital certificate Authority signature Authentication
(3) If you want to formally publish an Android, you must sign the program with a digital certificate generated by a suitable private key, instead of using the ADT plugin or the debug certificate generated by the Ant tool to publish.
(4) Digital certificates are valid, and Android only checks the validity of the certificate when the application is installed. If the program is already installed in the system, even if the certificate expires, it does not affect the normal function of the program


12. Talk about the Android NDK understanding
NDK Full Name: Native development Kit.
1. The NDK is a collection of tools. * The NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically apply so and Java applications
Packaged together as an apk. These tools are great for developers. * NDK integrates the cross compiler, and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers only need to simply modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so. * The NDK can automatically
So and Java applications packaged together, greatly reducing the developer's packaging work.
2. The NDK provides a stable, functionally limited API header file declaration. Google expressly declares that the API is stable and supports the currently published API in all subsequent releases. As seen from this version of the NDK, these APIs support very limited functionality,
Includes: C standard library (LIBC), Standard math Library (LIBM), compression library (LIBZ), Log Library (liblog).


Application of 13.ViewStub
This is often the case when developing an application that dynamically determines which view or layout to display at run time, depending on the condition. The most common idea is to write the view that is likely to be used on it, first to set their visibility to view.gone, and then dynamically change its visibility in the code. The advantage of this approach is that logic is simple and control is more flexible. But its disadvantage is that it consumes resources. Although the view is initially visible view.gone but in the inflate layout, the view will still be inflate, that is, the object will still be created, instantiated, and set properties. In other words, it consumes resources such as memory.
The recommended practice is to use android.view.viewstub,viewstub to be a lightweight view, an invisible, non-occupying layout location that occupies very small resources of the control. You can specify a layout for viewstub, in inflate layout, only viewstub is initialized, and when Viewstub is set to visible, or when viewstub.inflate () is called, The layout viewstub is inflate and instantiated, and the layout properties of the Viewstub are passed to the layout that it points to. In this way, you can use viewstub to make it easier to run, or not to display a layout.
But Viewstub is not omnipotent, the following summarizes what viewstub can do and when to use the viewstub, when the visibility of the control.
First of all, some features of viewstub:
1. Viewstub can only be inflate once, and then viewstub objects will be empty. According to the sentence, a viewstub specified by the layout is inflate, it will not be enough to control it through the viewstub.
2. Viewstub can only be used to inflate a layout file, not a specific view, of course, you can also write the view in a layout file.
Based on the above characteristics, the use of viewstub can be considered as follows:
1. During the run of the program, a layout will not change after inflate, unless it is restarted.
Because viewstub can only be inflate once and then be empty, you cannot expect to use Viewstub to control the layout later. So when you need to show and hide a layout more than once at run time, Viewstub can't. You can only use the visibility of the view to control it.
2. You want to control what is displayed and hidden is a layout file, not a view.
Because the ID of a layout file can only be set to viewstub, it cannot be controlled for a view.
So, if you want to control the display and concealment of a view (such as button or TextView), or if you want to show and hide a layout or view at run time, you can only use the visibility of the view to control it.

TopOneStep2
      • Previous Android development interview by--3. Common Java Foundation Pen questions
      • Next Android Development interview by--5. Common interviewer Questions Android ①



Android Development interview by--4. Common Android Advanced pen questions (update ... )


Related Article

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.