The following questions are summarized on the Internet. Thank you for sharing them! Hope that we can make progress together and find the company we dream:
1. Whether the process of Android DVM is the same concept as that of Linux:
A: DVM is a dalivk virtual machine. Every android application runs in its own process and has a dalivk Virtual Machine instance. Every DVM is a process in Linux. Therefore, it can be considered the same concept.
2. What types of Android animations are available? What are their characteristics and differences?
A: There are two types: tween animation and frame animation. Tween animation, which allows view components to move, zoom in, or zoom out, and change transparency. Frame Animation, a traditional animation method, is achieved through sequential playback of arranged images, similar to movies.
3. Handler hexadecimal principle:
A: Android provides handler and logoff to implement inter-thread communication. Handler first-in-first-out principle. Logoff is used to manage message exchange between objects in a specific thread ).
1) loue: A thread can generate a loue object to manage the Message Queue (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 (extracted from messagequeue.
3) messagequeue: used to store messages placed by threads.
4) thread: the UI thread is usually the main thread, and a message queue will be created for the android Startup Program.
4. Refresh Android View:
A: There are many ways to update the view in Android, so you must distinguish between different application scenarios. I think the most important thing is to clarify the usage of multithreading and double buffering.
1). multithreading and double buffering are not used.
This is the simplest case. Generally, you only want to re-paint the UI when the view changes. You only need to explicitly call the invalidate () method in the view object in the activity. The system automatically calls the ondraw () method of view.
2). multithreading and no dual-buffering
In this case, a new thread needs to be enabled, and the newly opened thread cannot access the view object. If you force access, the following error will be reported: Android. View. viewroot $ calledfromwrongthreadexception: only the originalthread that created a view hierarchy can touch its views.
In this case, you need to create a subclass that inherits Android. OS. handler and override the handlemessage (messagemsg) method. Android. OS. handler can send and process messages. You need to send a message to update the UI in the activity, and then your handler (you can use an anonymous internal class) (because the anonymous internal class can access the parent class variable, you can directly call the invalidate () method in the view object ). That is, a message is created and sent in a new thread, and then captured and processed in the main thread.
3). multithreading and dual Buffering
In Android, surfaceview is a subclass of view. It also implements dual buffering. You can define a subclass and implement the surfaceholder. Callback interface. Because the surfaceholder. Callback interface is implemented, the new thread does not need the help of Android. OS. Handler. The lockcanvas () method in surfaceholder can lock the canvas. It is more convenient to draw a new image and call unlockcanvasandpost (canvas) to unlock (Display.
5. Let's talk about the principles of the MVC model, which is used in Android:
A: Android officially recommends that the MVC mode be used for application development. What is MVC?
MVC is short for model, view, and controller. MVC consists of three parts:
L model object: it is the main part of an application. All business logic should be written in this layer.
2. view object: the part of the application that generates the user interface. It is also the only layer that users can see in the entire MVC Architecture. It receives user input and displays processing results.
3. Controller objects: controls the display of user interface data and updates the status of Model Objects Based on user input. A more important navigation function of Controller, if you want to use user-related events, you can submit them to m for handling.
Android encourages weak coupling and Component Reuse. In Android, MVC is embodied as follows:
1) view layer (View): it generally uses XML files to describe the interface, which can be easily introduced during use. Of course, if you know more about Android, you can think of using JavaScript + HTML as the view layer in Android. Of course, communication between Java and JavaScript is required. Fortunately, android provides convenient communication between them.
2) control layer (Controller): The re-authorization of the control layer of Android usually falls on the shoulders of many acitores. This statement also implies that you should not write code in acitivity, another reason for this is that the response time of acitivity in Android is 5 S. If time-consuming operations are put here, programs are easily recycled.
3) model layer: database operations and network operations should all be processed in the model. Of course, operations such as business computing must also be placed in this layer.
6. Activity lifecycle:
A: oncreate: Create an interface to initialize data.
Onstart: In this step, the user can see and cannot interact with each other.
Onresume: it becomes interactive with the user. (The activity stack system manages the top of these activities through stacks. After the stack is run, it returns to the previous activity)
Onpause: This step is visible but not interactive. The system will stop animation and other CPU-consuming tasks. As you can see from the above description, you should save some of your data here, at this time, your program has a lower priority and may be reclaimed by the system. The data stored here should be stored in
Onstop: becomes invisible and overwritten by the next activity
Ondestroy: This is the last method called before the activity is killed. It may be the external class that calls the finish method or the system temporarily kills the method to save space.
7. Change the activity to a window:
A: Activity attribute setting: Sometimes an application is floating on the mobile phone's main interface. In this case, you only need to set the theme of the activity, that is, add a sentence where manifest. xml defines the activity:
Android: theme = "@ Android: style/theme. Dialog"
For translucent effect:
Android: theme = "@ Android: style/theme. translucent"
8. Five la s commonly used in Android:
Answer: linearlayout linear layout; absolutelayout absolute layout; tablelayout table layout; relativelayout relative layout; framelayout frame layout;
9. Five data storage methods for Android:
A: sharedpreferences; file; SQLite; contentprovider; Network
10. Explain the relationships among message, handler, message queue, and logoff in the single-thread model:
A: handler gets the logoff object in the current thread. logoff is used to retrieve the message from the message queue containing the message, and then handler distributes and processes the message.
11. What is the full name of aidl? How to work? What types of data can be processed?
A: aidl (androidinterface Definition Language) Android Interface Description Language
12. multiple browsers are installed on the system. Can you specify a browser to access a specified page?? Please explain the reason:
A: Send the URI directly to pass the parameter, or use the data attribute in intentfilter in manifest. The Code is as follows:
Intent intent = new intent ();
Intent. setaction ("android. Intent. Action. View ");
Uri uribrowsers = URI. parse ('http://www.sina.com.cn ");
Intent. setdata (uribrowsers );
// Package name, activity to be opened
Intent. setclassname ("com. Android. Browser", "com. Android. browser. browseractivity ");
Startactivity (intent );
13. What is ANR and how to avoid it?
A: ANR definition:
On Android, if your application has a responsive response for a period of time, the system will prompt you the ANR: application not responding dialog box. Therefore, the design of response performance in the program is very important, so that the system will not display ANR to the user.
How to avoid:
First, we will study why it occurs in Android applications and how to best build applications to avoid ANR.
Android applications usually run in a separate thread (for example, main). This means that if your application is doing things for a long time in the main thread, the ANR dialog box is triggered because your application does not have the opportunity to handle input events or intent broadcasts.
Therefore, do as few requests as possible in the main thread. In particular, an activity should be created as little as possible in its key lifecycle methods (oncreate () and onresume. Potential time-consuming operations, such as network or database operations, or high-time computing such as bitmap size change, should be in the Child thread (or take database operations as an example, through asynchronous request). However, it does not mean that your main thread is blocked and waits for the completion of the sub-thread-or it does not call thread. Wait () or thread. Sleep (). The alternative method is that the main thread should provide a handler for the sub-thread so that the handler can be submitted to the main thread upon completion. Designing your application in this way will ensure that your main thread is responsive to the input and avoid the ANR dialog box triggered by the 5-second input event timeout. This approach should be followed in other display UI threads because they are all affected by the same timeout.
The Special execution time limit of intentreceiver means that it should do small and trivial work in the background, such as saving settings or registering a notification. Like other methods called in the main thread, applications should avoid time-consuming operations or computations in broadcastreceiver, instead of doing these tasks in the Child thread (because broadcastreceiver has a short life cycle), the alternative is that if you need to perform a time-consuming action in response to the intent broadcast, the application should start a service. By the way, you should also avoid starting an activity in the intent receiver because it creates a new screen and grabs the focus from the program that the current user is running. If your application needs to show users what they want to respond to intent broadcast, you should use notification
Manager.
In general, 100 to MS is the time threshold for the user to perceive the block in the application. Below are some tips to avoid ANR and help make your application look responsive.
If your application is working in the background for the response user input, the progress of the work can be displayed (progressbar and progressdialog are useful in this case ). Especially for games, mobile computing is performed in sub-threads. If your program has a time-consuming initialization process, you can display a splash screen or quickly display the main screen and asynchronously fill in the information. In both cases, you should display the progress in progress to avoid freezing the program.
14. What will cause force close? How to avoid it? Can I capture exceptions that cause them?
A: for example, a null pointer can cause forceclose. You can check logcat and find the corresponding program code to solve the error.
15. lifecycle of the activity during screen switching:
A:
1) Create an activity and print the lifecycle of the activity.
2) run the activity and obtain the following information:
Oncreate () à
Onstart () à
Onresume () à
3) press Ctrl + F12 to switch to landscape Screen
Onsaveinstancestate () à
Onpause () à
Onstop () à
Ondestroy () à
Oncreate () à
Onstart () à
Onrestoreinstancestate () à
Onresume () à
4) When you press Ctrl + F12 to switch to the portrait screen, the same log is printed twice.
Onsaveinstancestate () à
Onpause () à
Onstop () à
Ondestroy à
Oncreate () à
Onstart () à
Onrestoreinstancestate () à
Onresume () à
Onsaveinstancestate () à
Onpause () à
Onstop () à
Ondestroy à
Oncreate () à
Onstart () à
Onrestoreinstancestate () à
Onresume () à
5) Modify androidmanifest. XML, add Android: configchanges = "orientation" to the activity, and execute step 3.
Onsaveinstancestate () à
Onpause () à
Onstop () à
Ondestroy () à
Oncreate () à
Onstart () à
Onrestoreinstancestate () à
Onresume () à
6) Modify androidmanifest. XML, add Android: configchanges = "orientation" to the activity, and execute Step 4. The same information will not be printed, but an onconfigchanged line will be printed.
Onsaveinstancestate () à
Onpause () à
Onstop () à
Ondestroy () à
Oncreate () à
Onstart () à
Onrestoreinstancestate () à
Onresume () à
Onconfigurationchanged () à
7) Change Android: configchanges = "orientation" in step 5
Android: configchanges = "Orientation | keyboradhidden". After step 3 is executed, only onconfigchanged is printed.
Onconfigurationchanged () à
8) Change Android: configchanges = "orientation" in step 5
Android: configchanges = "Orientation | keyboradhidden", step 4
Onconfigurationchanged () à
Onconfigurationchanged () à
Summary:
1) when the Android: configchanges of the activity is not set, the split screen will call the lifecycle of each activity again. The split screen will be executed once, and the split screen will be executed twice.
2) When setting the activity's Android: configchanges = "orientation", the screen will be re-called for each life cycle, and the screen will be executed only once when the screen is landscape or landscape is switched, however, an onconfigurationchanged () is printed at the end of the portrait screen ()
3) when setting the activity's Android: configchanges = "Orientation | keyboardhidden", the split screen will not re-call each lifecycle, but will only execute onconfigurationchanged (), landscape screen once, landscape screen twice
Summarize the lifecycle of the entire activity:
1) When toast and alertdialog are displayed in the event generated by the current activity, the lifecycle of the activity will not change.
2) press the Home key when the activity is running (the same as being completely overwritten)
Onsavainstancestate à
Onpause à
Onstop à
Onrestart à
Onstart à
Onresume à
3) Not fully covered, but the focus is lost:
Onpause à
Onresume à
16. How to publish the SQLite database (. DB file) with the APK file?
A: You can copy the. DB file to the res aw directory of the eclipse Android project. All files in the res aw directory will not be compressed, so that files in the directory can be extracted directly. Copy the. DB file to the res aw directory.
17. How can I open the database files in the res aw directory?
A: In Android, you cannot directly open the database file in the res aw directory. Instead, you need to copy the file to a directory in the memory or SD card of your mobile phone when the program is started for the first time, then open the database file. The basic method of copying is to use the getresources (). openrawresource method to obtain the resource inputstream object in the res aw directory, and then write the data in the inputstream object to the corresponding file in other directories. You can use sqlitedatabase. openorcreatedatabase In the android SDK to open SQLite database files in any directory.
18. Which of the following types of XML parsing classes does Android support? Which one is officially recommended? And their principles and differences:
A: There are three ways to parse XML: sax, Dom, and pull. It is relatively easy to use dom for general development on PC, but some performance-sensitive databases or mobile phones still use the sax method. The sax reading method is one-way and has advantages: it is not convenient to occupy memory space and parse attributes, but the disadvantage is that it is not convenient to process multiple branches. The DOM method loads the entire XML file into the memory. Here, the android Development Network reminds you that this method can be used with XPath in search. If the data volume is not large, it is recommended to use it, while pull is often used in j2s for better processing of nodes. Similar to the sax method, pull also saves much memory. We often use the kxml library for parsing in j2s.
19. What is the difference between ddms and traceview?
A: ddms is a program execution viewer in which thread and stack information can be seen. traceview is a Program Performance Analyzer.
20. Talk about the android IPC Mechanism:
A: IPC is short for internal process communication. It is a resource sharing "named Pipeline. The IPC Mechanism in Android is used to allow interaction between activity and service at any time. Therefore, in Android, this mechanism is only applicable to communication between activity and service, similar to remote method calls, similar to 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.
21. What is ndk:
A: ndk is a collection of tools.
Ndk provides a series of tools to help developers quickly develop C/C ++ dynamic libraries and automatically compress so and Java applications into APK packages.
Ndk integrates the cross compiler and provides MK files and isolates the differences between CPU and platform. Developers only need to modify the MK file to create the so
22. Describe the Android system architecture:
A: The Android system architecture is divided from the bottom to the Linux kernel layer, Runtime Library, application framework layer, and application layer.
Linux kernel layer: Responsible for hardware drivers, networks, power supplies, system security, and memory management.
Runtime Library and androidruntion: Runtime Library: C/C ++ function library. Most of them are open-source function libraries, such as WebKit. This function library is responsible for running Android web browsers; for example, the standard C function library libc, OpenSSL, and SQLite also include 2dsgl and 3 dopengles that support game development, mediaframework framework is available in multimedia to support playback and display of various audio and video files, such as MPEG4 and H. 264, MP3, AAC, Amr, JPG, PNG, and many other multimedia file formats. Androidruntion is responsible for interpreting and executing the generated Dalvik format bytecode.
Application Software Architecture: Java application developers use APIs encapsulated at this layer for rapid development.
Application Layer: This layer is the Java application layer. Android's built-in googlemaps, email, Im, and browsers are all at this layer, the Java developer-defined program is also at this layer, and is equal to the built-in application. It can call or replace the built-in application.
23. What are the startup modes of activity and task? What are their meanings?
A: In an activity, you call startactivity multiple times to start another activity. To generate only one activity instance, you can set the startup mode.
An activity has four startup modes: standed, signletop, singletask, and singleinstance.
Standed: standard mode. A new instance is generated when the startactivity () method is called.
Singletop: if an instance already exists at the top of the activity stack, a new instance is not generated, but the newinstance () method in the activity is called. If it is not at the top of the stack, a new instance is generated.
Singletask: This instance will be generated in a new task. This instance will be used for each call in the future and no new instances will be generated.
Singleinstance: this is basically the same as singletask. There is only one difference: In this mode, only the activity instance can exist in the task where the activity instance is located, and no other instances
24. Role of the application class:
A: The first sentence in the API is:
Base class for those who need to maintain global application state
If you want to use global variables in the entire application, static variables and public variables are generally used in Java. If you use such global variables in Android, they do not conform to the framework architecture of Android, however, you can use application context in a more elegant way.
First, you must override the application. The oncreate method is used to override the value of the variable during creation. Then the variable can be operated on in each file of the entire application.
When the application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. Then we initialize the global variables when the application is created. All the activities of the same application can obtain the values of these global variables. In other words, we changed the values of these global variables in an activity, so the values of other activities in the same application will change.
25. Description of when onsaveinstancestate () and onrestoreinstancestate () are called:
A: The onsaveinstancestate () and onrestoreinstancestate () of an activity are not life cycle methods. They are not necessarily triggered because they are different from the Life Cycle methods such as oncreate () and onpause. Onsaveinstancestate () is called only when an activity is destroyed by the system when the application encounters an unexpected situation (such as insufficient memory or the user directly presses the Home key. However, when a user actively destroys an activity, for example, pressing the return key in the application, onsaveinstancestate () will not be called. In this case, the user's behavior determines that the activity status does not need to be saved. Generally, onsaveinstancestate () is only suitable for saving some temporary states, while onpause () is suitable for persistent data storage.
In addition, when the screen direction changes, the activity will be destroyed and re-created. If you want to cache some data before the activity is destroyed, the cached data is restored after the activity is re-created. You can override the onsaveinstancestate () and onrestoreinstancestate () Methods of the activity.
26. What is the lifecycle of the android service? Which method can be called multiple times:
Answer: 1) lifecycle methods related to starting a service using the context. startservice () method
Oncreate ()-> onstart ()-> ondestroy ()
Oncreate () This method is called when a service is created. This method is called only once. No matter how many times the startservice () or bindservice () method is called, the service is created only once.
Onstart () calls back this method only when the context. startservice () method is used to start the service. This method is called when the service starts running. Although the startservice () method is called multiple times, the onstart () method is called multiple times.
Ondestroy () is called when the service is terminated.
2) lifecycle methods related to starting a service using the context. bindservice () method
Oncreate ()-> onbind ()-> onunbind ()-> ondestroy ()
Onbind () calls back this method only when the context. bindservice () method is used to start the service. This method is called when the caller binds to the service. When the caller and the service are already bound, multiple calls to the context. bindservice () method will not cause this method to be called multiple times.
Onunbind () calls back this method only when the context. bindservice () method is used to start the service. This method is called when the caller and the service are unbound.
If you use the startservice () method to start the service, then call the bindservice () method to bind to the service, then call the unbindservice () method to unbind, and finally call the bindservice () method to bind to the service again, the method for triggering the lifecycle is as follows:
Oncreate ()-> onstart ()-> onbind ()-> onunbind () [true must be returned for the overloaded method]-> onrebind ()
27. Life Cycle of Android broadcast:
Answer: 1) There is only one callback method in the life cycle of the broadcast receiver:
Void onreceive (context curcontext, intent broadcastmsg)
When the receiver receives a broadcast message, Android will call oncaster Er () and pass it to an intent object, which carries the broadcast message. In our opinion, only when this method is executed, the broadcast receiver is active; when this method is returned, it is terminated. This is the life cycle of the broadcast receiver.
2) due to the short life cycle of the broadcast receiver, an active broadcast receiver process is protected to avoid being killed, android will kill processes that carry components that are no longer active at any time, so this problem may occur.
3) a service is used to solve the problem. Android considers that there are still active components in the process where the service is located.
28. Differences between Android view, surfaceview, and glsurfaceview:
A: 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.
Based on the above, the game features are generally divided into two categories.
1) passively update the image. For example, you can use view for chess. Because image update relies on ontouch for update, you can use invalidate directly. In this case, this touch and the next touch take a longer time and will not be affected.
2) Actively update. For example, a person is always running. This requires a separate thread to repeatedly repaint the state of the person, to avoid blocking the main UI thread. Obviously, the view is not suitable and needs surfaceview for control.