preface
The main audience of this article is developers, so it does not involve Android service deployment and other operations, and the content is quite large, we are ready to be patient and melon seed mineral water. In my spare time, I sorted out the Android high-frequency interview questions left by the former Ali P7 senior engineer. Before that, I happened to see an interview article related to Android on the Internet, and found that some of the questions WERE not well answered. Although most of the knowledge points were known, I could not connect the knowledge together. Therefore, I decided to make an Android soul 100 question, and try to answer the question in a way that allows me to understand the knowledge point a little more deeply. This article will not explain the usage of Android in detail, mainly aimed at some knowledge points developers need to know about Android, including basic, project, performance optimization, open source framework and other aspects, in the form of high-frequency questions in the interview to give answers (there are surprises at the end of the article).
The Activity related to the
1. What is an Activity? One of the four major components, in general, a user interface corresponding to an activity the setContentView (), the layout of the / / to display the button. SetOnclickLinstener {}, An activity is a subclass of Context that implements both window.callback and keyevent.callback to handle events that interact with a form user. I develop common FragmentActivitiyListActivity PreferenceActivity, TabAcitivty etc. 2, please describe the Activity life cycle Activity from creation to destruction of a variety of state, Callback methods are fired when going from one state to another. These callback methods include: OnCreate onStart onResume onPause onStop onDestroy onCreate onStart onResume onPause onStop onDestroy OnStart is visible and onStop is not; OnResume can be edited (that is, focus) with onPause; If the interface has a common feature or function, it defines its own BaseActivity. 3. Common Activity types include FragmentActivitiy, ListActivity, TabAcitivty and so on. An Activity has multiple states from creation to destruction. When it goes from one state to another, callback methods are fired. These callback methods include: OnCreate onStart onResume onPause onStop onDestroy onCreate onStart onResume onPause onStop onDestroy OnStart is visible and onStop is not; OnResume can be edited (i.e., focus) with onPause. 4. What methods must be executed when jumping between two activities? For example, there are two activities called A and b. When B is activated in A, A calls onPause(), and B calls onCreate(),onStart(), and onResume(). At this point B overrides the form and A calls the onStop() method. If B is transparent or dialog style, A’s onStop() method will not be called.
The Service related
1. Whether the Service is executed in the main thread and whether time-consuming operations can be performed in the Service? By default, services and activities run in the main thread of the process where the current app is running, if not indicated. In special cases, you can configure the process in which the service is executed in the manifest file so that the service can be executed in another process
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
Copy the code
IntentService IntentService IntentService IntentService IntentService What are the advantages? We usually only use Service, so maybe IntentService is the first thing most of you have ever heard of. So saw the following introduction believe you no longer strange. If you still don’t know about it, be honest in the interview that you haven’t used it, don’t know about it, etc. Not all questions need to be answered.
3. How to bind an Activity to a Service? How to start a Service in an Activity?
- The Activity is bound to the service by bindService(Intent Service, ServiceConnection conn, int Flags). When the binding is successful, the service will pass the proxy object to the CONN as a callback. So we have the Service proxy object provided by the Service.
- You can start a Service in an Activity using the startService and bindService methods. In general, if you want to obtain the Service object of the Service, you must use the bindService () method, such as music player, third-party payment, etc. Use the startService () method if you want to start a background task only.
Android performance optimization
1, how to analyze the performance of Android applications an App is smooth or not installed in their own real machine, play a few days can have a general perceptual understanding. However, professional analysis tools can enable us to better analyze our applications. In real development, once we’ve solved all the bugs in the current application, we start thinking about new optimizations. If you don’t consider using other third-party performance analysis tools, you can use the tools in DDMS, which are already very powerful. There are tools in DDMS such as TraceView, Heap, allocation Tracker, etc., which can help us analyze the method execution time efficiency and memory usage of an application
Android’s virtual machine is a regist-based Dalvik, with a maximum heap size of 16M and 24M for some machines. So the amount of memory space we can use is limited. OutOfMemory error occurs when we use more than a certain amount of memory. There are several reasons for this: (1) resource release, (2) excessive object memory, and (3) static keyword use
3, how to avoid the abnormal OOM OOM memory, want to avoid abnormal OOM first we want to know what conditions can lead to abnormal OOM (1) the picture is too big lead to OOM (2) (3) query the database interface switching lead to OOM didn’t close the cursor Adapter (4) structure, (6) The most typical case in other Android applications where you need to pay attention to freeing resources is during the Activity lifecycle, Situations where appropriate resource release is required in the onPause(), onStop(), onDestroy() methods. Using a broadcast without logging out will also generate OOM
4. What are the ways of inter-thread communication in Android? shared memory (variable); files, databases; Handler. Java wait(), notify(), notifyAll()
What is the structure of an Android application? Android application structure is our engineering structure:
The event processing
1. What is the difference between onTouch and onTouchEvent in event distribution, and how to use them? Both methods are called within the View’s dispatchTouchEvent, with onTouch executing before onTouchEvent. If the event is consumed in the onTouch method by returning true, the onTouchEvent will no longer execute. The first is that the value of mOnTouchListener must not be null, and the second is that the control being clicked must be enable. So if you have a control that is not enable, registering onTouch events for it will never be executed. For this type of control, if we want to listen for its touch event, we have to override the onTouchEvent method in the control.
What is the full name of AIDL? How does it work? What types of data can you handle? AIDL Android Interface Definition Language (AIDL) is an Interface description Language. The compiler can use the AIDL file to generate a piece of code that communicates between two processes using a pre-defined interface to access cross-border objects. Two things need to be done:
- Introduce AIDL related classes.
- In theory, you can pass the basic data types and strings as arguments, as well as classes derived from bundles. However, in Eclipse, the current ADT does not support Bundle as arguments.
Thread article
1. Explanation of relevant concepts
- Main Thread (UI Thread) Definition: When an application is first started, Android starts a Main Thread at the same time. The Main Thread is responsible for handling uI-related events
- Definition: Handler receives and processes the Message object (Bean object) function: communication related information storage and transfer
- ThreadLocal: a data store class within a thread that stores and retrives Looper of the thread
- MessageQueue (Message queue) definition: use a single linked list of data structure to store the Message list function: used to store the Message sent by the Handler, in the first in first out execution
- Definition: The main role of Message Handler: responsible for sending messages to the Message queue & processing the messages dispatched by Looper
- Looper (Looper) definition: acts as a bridge between Message Queue and Handler: Message loop: loops to fetch messages from the Message Queue. Message dispatch: delivers messages to the corresponding Handler
Why does creating a Handler in a child thread throw an exception? The Handler’s work is dependent on loopers. loopers (and message queues) belong to a thread (ThreadLocal is a data store class within a thread that stores data in a given thread but not in other threads) and are not accessible to other threads. So the Handler is indirectly bound to the thread. Therefore, to use Handler, you must ensure that the thread created by the Handler has Looper objects and starts the loop. Because there is no Looper in the child thread by default, an error will be reported. The correct way to use it is:
private final class WorkThread extends Thread { private Handler mHandler; public Handler getHandler() { return mHandler; } public void quit() { mHandler.getLooper().quit(); } @Override public void run() { super.run(); // create a Looper for this thread, // internal implementation // 1. New Looper () // 2. The lopper in step 1 is placed in a ThreadLocal. ThreadLocal is used to save data. The main application scenario is: the data between threads does not affect each other // 3. New MessageQueue() in the constructor of Looper in step 1; // In fact, create a Looper for the thread pair. Looper creates a MessageQueue to implement the message mechanism. // If you don't know the message mechanism, you can refer to the data. Looper.prepare(); mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.d("WorkThread", (Looper.getMainLooper() == Looper.myLooper()) + "," + msg.what); }}; // Enable infinite loop processing of messages: dispatchMessage looper.loop (); Log.d("WorkThread", "end"); }}Copy the code
When the system has multiple time-consuming tasks to execute, each task will start a new thread to execute the time-consuming task. This will cause the system to create and destroy threads multiple times, which will affect performance. To solve this problem, Google provides HandlerThread. HandlerThread is to create a Looper loop in the thread, and let Looper poll the message queue. When a time-consuming task enters the queue, it does not need to start a new thread, but just execute the time-consuming task in the original thread. Otherwise the thread blocks.
AsyncTask is a lightweight asynchronous task class. It can execute background tasks in the thread pool, and then pass the progress and final results to the main thread and update the UI in the main thread. It is easier to execute background tasks and access the UI in the main thread. However, AsyncTask is not suitable for time-consuming background tasks, for which a thread pool is recommended.
Update the UI in the child thread with the runOnUiThread() method on the Activity object:
If it is in a non-context class (Activity), it can be invoked by passing the context;
Update UI with view.post (Runnable r) method
Open Source Frameworks
2. XUtils (network, picture, ORM)
2, can first say the principle of level 3 cache
- Load from cache.
- Load (database, SD) from local file
- Load from the network.
A. When loading a Bitmap, you do not need to consider the oom(memory overflow) phenomenon during the bitmap loading process and the image dislocation when the Android container slides quickly. (16 MB) b. Network and local images can be loaded. C. Memory management uses LRU algorithm (remove objects that have the least frequency) to better manage bitmap memory. D. You can configure the number of threads to load, cache size, cache path, loading display animation, etc.
3, can first say the role of Gson, and then in the backward expansion. Gson is an open source framework provided by Google for fast parsing of JSON data. Originally, we used jsonObject jsonArray to parse data layer by layer. I found that parsing layer by layer was a waste of time, so I studied Gson in my spare time. Gson meets our rapid development characteristics, as long as the JSON data from the server is parsed by Gson, Gson will return a data object, we can directly operate on the data. The original resolution may take more than ten minutes, now two or three minutes to finish
4, imageLoader (image processing framework) the current mainstream image cache processing framework, as long as the configuration of relevant parameters can be.
5. The process of the project may not be asked in the project, but everyone should know the process of project development. As an experienced programmer, the process of the program must be known. Arrange according to the timeline: project approval: determine the project, the person in charge, the development cycle, cost, manpower, material needs: document, prototype development: coding test: test personnel online: product department maintenance: repair new bugs upgrade: revision, add new functions
project
1. What is socket? TCP and UDP Size of each packet Socket is usually called “Socket”, used to describe the IP address and port, is a communication connection handle, the application program usually through the “Socket” to send a request to the network or reply to the network request, it is the abstract representation of the endpoint in the process of network communication. It mainly includes TCP and UDP protocols.
- The size of a UDP packet is 1492 – IP header (20) – UDP header (8) = 1464 bytes
- The TCP packet size is 1492-IP header (20) -TCP header (20) = 1452 bytes
(1) Volley itself does not support the download of large data files, if you must use volley to download, you can use breakpoint download; Cut each download into as many smaller files as possible. (2) Secondary encapsulation ① Directly encapsulate a data interface, so that we can directly get the result data of network request in the Activity. (2) encapsulate a JsonObject request results of generic encapsulation class, convenient for different entity class resolution. ③ Encapsulate Volley request methods, including GET, POST and other mainstream request methods, and json file parsing methods. Finally, call in the Activity.
In Android, you can upload files through HTTP or Socket, but HTTP cannot be used to upload large files. Here describes a breakpoint continuation through Socket. The server will record the uploading progress of files. When an upload process unexpectedly terminates, you can continue the next upload, using J2SE knowledge.
When the client first uploads, it sends the message “Content-Length=35; Filename = WinRAR_3. 90 _sc. Exe. Sourceid = “a string in the format of sourceID =2324838389. The server will check whether the file has been uploaded. If so, return the location where the file has been uploaded, otherwise return the generated sourceID and position 0, similar to sourceID =2324838389. Position =0 “. The client receives the returned string and uploads the file from the specified position.
4. Give a brief introduction to MVC and MVP design patterns, and talk about the characteristics of each
In MVC, the View has direct access to the Model! Thus, the View will contain the Model information and, inevitably, some business logic. In the MVC Model, the focus is on the invariance of the Model, while there are multiple different representations and views of the Model at the same time. So, in the MVC Model, the Model doesn’t depend on the View, but the View depends on the Model. Not only that, but because some of the business logic is implemented in the View, it is difficult to change the View, or at least that business logic is not reusable.
5. Talk about the principle of MVC pattern. It uses MVC in Android, namely model-View-Controller. Such an application is divided into three layers — the model layer, the view layer, and the control layer. The interface part of Android also adopts the current popular MVC framework. In Android, M is the binary data in the application, and V is the user interface. Android interface directly using XML files saved, interface development becomes very convenient. An Activity can have multiple interfaces. Just pass the view ID to setContentView() to specify which view model to display data in. Data binding in the Android SDK also uses a similar approach to the MVC framework to display data. Data binding is achieved by encapsulating data in the control layer according to the requirements of the viewmodel (i.e., Adapter in the Android SDK) so that it can be displayed directly on the viewmodel. For example, the ListActivity that displays all the data in the Cursor has a view layer called a ListView. The view layer encapsulates the data into a ListAdapter and passes it to the ListView. The data is displayed in the ListView.
Due to the limited space of the article, the data in my QQ technical communication group can be taken away by yourself, if you encounter problems in study or work, there will be some great help in the group to answer, sometimes you think about a day, not as good as others a few words will be enlightened, group 793544421
Recommend left left left
long
According to the
guan
note