If you want to make the most failed case on Google Play, the best secret is that the interface is extremely slow, consumes power, and consumes memory. Next, you will get negative comments from users, and your reputation will become bad. It is useless even if your applications are well designed and creative.
Every problem that affects product efficiency, such as power consumption or memory usage, affects App success. This is why it is crucial to ensure optimization and smooth operation during development without causing problems in the Android system. We don't need to discuss efficient programming here, because we don't care whether the code you write can withstand the test. Even efficient code execution takes time to run. In this article, we will talk about how to shorten the running time as much as possible and how to develop your favorite apps.
Efficient use of threads
Recommendation 1: How to cancel some thread actions in the background
We know that all the operations in the App running process are performed in the main thread (UI thread) by default, so that the App response speed will be affected. It will cause the program to get stuck, die, or even cause a system error.
To speed up the response, you need to move time-consuming operations (such as network requests, database operations, or complex computations) from the main thread to a separate thread. The most efficient way is to complete this operation at the class level. You can use AsyncTask or IntentService to create background operations. If you choose to use IntentService, it will start as needed, and then process the request (Intent) through a working thread ).
When using IntentService, pay attention to the following restrictions:
- Do not pass information to the UI. If you want to display the processing result information to the user, use Activity;
- Only one request can be processed at a time;
- Each request processing process cannot be interrupted;
Recommendation 2: how to keep the response from ANR
Remove time-consuming operations from the UI thread. This method also prevents user operations from appearing in the ANR dialog box. What needs to be done is inherit AsyncTask to create a background working thread and implement the doInBackground () method.
Another way is to create a Thread class or HandlerThread class. Note that this will also slow down the App, because the default thread priority is the same as that of the main thread, unless you explicitly set the thread priority.
Recommendation 3: How to initialize query operations in the thread
When the query operation is being processed in the background, the displayed data is not real-time, but you can use the CursorLoader object to speed up the process. This operation will not affect the interaction between the Activity and the user.
After using this object, your App will Initialize an independent background thread for ContentProvider to query. After the query is complete, it will return results to the Activity that calls the query.
Recommendation 4: Other considerations