1. How to avoid ANR?
Answer: Anr:application not responding, five seconds in Android, both the Activity Manager and the window Manager are responsible for monitoring the application's response.
Android Displays the ANR dialog box when the following conditions occur:
More than 5 seconds of response to input events such as keystrokes, touch-screen events
Intent Receiver (Intentreceiver) has not been executed for more than 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 (Intent broadcast).
Therefore, any method that runs in the main thread should do as little work as possible. In particular, important methods in the life cycle of the activity, such as OnCreate () and Onresume (), should be more so.
Potentially more time-consuming operations, such as accessing networks and databases, or expensive computations, such as changing the size of bitmaps, need to be done in a separate sub-thread (or with an asynchronous request , such as a database operation).
But this does not mean that your main thread needs to go into a blocking state that has waited for the child thread to end--and does not need to call the therad.wait () or Thread.Sleep () method. Instead, the main thread provides a handle (Handler) for the child thread, allowing the child thread to call it at the end of the process (Xing: See the example of snake, which differs from what we were in contact with previously). Using this approach involves your application, which ensures that your program responds well to the input and avoids the ANR resulting from an input event that is not processed for more than 5 seconds.
This practice needs to be applied to all threads that display the user interface, because they all face the same timeout problem.
2. Principles of the handler mechanism
A: Andriod provides Handler and Looper to satisfy the communication between threads .
Handler first-out principle. The Looper class is used to manage the exchange of messages (message exchange) between objects within a particular thread.
1) Looper: A thread can produce a Looper object that manages this line thread message queue.
2) Handler: You can construct a Handler object to communicate with Looper in order to push a new message into the message queue, or to receive a message sent by Looper out of the message queue.
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 message Queue for it.
3. What is the intention of Android to introduce a broadcasting mechanism?
For:
A: From the perspective of MVC (in-app) actually answer this question when you can ask, why Android has the 4 large components, now the mobile development model is basically a copy of the web that a set of MVC architecture, just a bit of a dowry.
The four components of Android are essentially designed to implement the MVC architecture on mobile or embedded devices, where there is a time of interdependence, sometimes a complementary relationship, and the introduction of broadcast mechanisms to facilitate the interaction of information and data between several large components .
B: Inter-Program communication (for example, in their own application monitoring system calls )
C: Efficiency (refer to the UDP broadcast protocol for the convenience of the LAN)
D: In design mode (an application of inversion control, similar to listener mode)
4. What causes force Close? How can I avoid catching the exception that caused it?
A: Generally like a null pointer Ah, you can look at Logcat, and then correspond to the program to resolve the error.
5. How to set an activity to the window's style.
Talk about the easy, maybe someone wants to make the app is a floating in the main interface of mobile phone things , then very simple you just need to set up the theme of the activity can be defined in Androidmanifest.xml Activity place in a word:
XML code 1. Android:theme= "@android: Style/theme.dialog"
This causes your application to become a dialog box that bounces out the form,
or XML code 1. Android:theme= "@android: style/theme.translucent" becomes translucent,
[Friendly tip-.-] similar to the properties of this activity can be on Android. As seen in the Androidmanifestactivity method of the R.styleable class, the properties of all elements in Androidmanifest.xml can be referenced in this class android.r.styleable
It says the attribute name, what the value is on Android. As you can see in R.style, this "@android: Style/theme.dialog" corresponds to Android. R.style.theme_dialog, (' _ ' replaced with '. ' <--NOTE: This is the article content is not a smiley face)
Can be used in the description file, find the class definition and description of the corresponding relationship in the file is understood.
(simply to introduce style directly in the profile.) The application of the pursuit effect of this use to quite a lot);
6. What are the advantages of intentservice?
Acitivity process, when processing intent, will generate a corresponding service Android process processor will now as far as possible do not kill you very easy to use
(This answer actually compares ...) In fact, I don't know why, because looking at the help document says a service that handles asynchronous requests, and stops when you're done. No use, no evaluation).
7, the life cycle of the broadcast?
Broadcast life cycle is very short, when sent intent will go to the Androidmanifest.xml method to find whether it is a matching action, if any, call receiver, then obtain receiver object, and then execute the Onreceiver method , the receiver object is useless at this time,
When we click the button again, we regain the object, which is the life cycle of the broadcastreceiver.
It is not possible to do more time-consuming operations in Broadcastreceiver, otherwise the dialog box for ANR (Application No Response) will pop up.
If a time-consuming task needs to be completed, it should be done by sending intent to the service and by the service. It is not possible to use a child thread here because the Broadcastreceiver life cycle is short and the child threads may not be finished, and the Broadcastreceiver is finished first. Once the broadcastreceiver is over, the broadcastreceiver thread is easily killed when the system needs memory because it belongs to the empty process (the process without any active components). If its host process is killed, the working child thread is also killed, so it is unreliable to use a child thread to resolve it.
8. What are the differences between the two methods of starting a service?
One is StartService () and the other is Bindservice ().
The difference between the two is that the first way the caller opens the service, it loses contact with the service, and the two are not associated. Even if the visitor exits, the service is still running. You must explicitly call the StopService method if you want to dismiss the service.
It is mainly used in cases where the caller does not interact with the service, that is, the caller does not need to get the business method in the service. such as telephone recording .
The latter is bound together with the service. When the caller exits, the service exits with it. Used to interact with the service.
"Reprint" 2015Android face question 01