In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the time-consuming operation in the main thread of UI. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
problem
Since the release of Android Ice Cream Sandwich, the problem has spread on StackOverflow:
My application works well on Android2.x, but it is always forced to fall back on 3.x and 4.x systems. what causes it?
This is a great question, after all, developers always hope that applications based on the old version of the system will still be compatible with the new version of Android. In my opinion, the causes of the problems may be varied. But most of the time, the reason is very simple: you put a potentially time-consuming operation into the UI thread.
What is a UI thread?
The concept and importance of the main UI thread for an application should be understood by every Android developer. When an application starts, the system creates a main thread called "main" for the application. This main thread (that is, the UI main thread) is mainly responsible for distributing events to the appropriate view or widget, so it is very important. It is also a thread of UI interaction between your application and your application. For example, if you click a button on the screen, the UI thread passes the click time to view, and when view receives the event, it sets its pressed status and sends an invalidate request to the event queue. The UI thread reads the queue in turn and tells view to redraw itself.
Unless your Android application is implemented very reasonably, this single-threaded model will make performance extremely low. In extreme cases, if the UI thread is responsible for all the operations in the entire application, time-consuming operations such as sending network requests or database queries can lead to blocking the user interface. All time, including drawing and touch screen events, will not be dispatched until these operations are completed. From the user's point of view, the program seems to be stuck.
In these cases, immediate feedback is very important. The research shows that 0.1s is the critical value of whether the user feels the system is smooth or not. Anything slower than the critical value is considered a delay (Miller 1968; Card et al. 1991). While 1 second may not seem to matter, in GooglePlay, even 1/10 seconds can be the difference between good and bad reviews. To make matters worse, if the UI thread is blocked for more than 5 seconds, the user will receive a "program unresponsive" (ANR) dialog box and force the exit.
Why does Android crash the application?
The main reason why the application works well on the 2.x system and crashes on the platform 3.0 and above is that the platform above 3.0 is more strict in dealing with the abuse of UI thread resources. For example, when the 3.0 platform detects a network request in the UI thread, it throws a NetworkOnMainThreadExceptionwill exception:
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example/com.example.ExampleActivity}: android.os.NetworkOnMainThreadException
This is also well explained in the documentation on the Android developer website:
When the application attempts to perform a network operation in the main thread, the NetworkOnMainThreadException is thrown. It will be thrown only when running Honeycomb SDK and later. Earlier versions of SDK allowed network operations in the main event loop thread, but this was very, very discouraged.
List some operations that ICS and Honeycomb do not allow in UI threads:
Open the socket connection (i.e. New Socket ()).
HTTP request (i.e. HTTPClient and HTTPUrlConnection).
Attempt to connect to a remote MYSQL database.
Download the file (i.e.Downloader.downloadFile ()).
If you want to do something in a UI thread, be sure to package it into a worker thread. The easiest way is to use AsyncTask, which allows you to do some asynchronous operations in your user interface. AsyncTask puts blocking operations in the worker thread and returns the results to the UI thread, and you don't have to deal with any thread-related work.
This is what the editor shares with you about the time-consuming operation in the main thread of UI. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.