10 Suggestions for Improving the Operational Efficiency of android
- 2021-12-21 05:14:07
Here are 10 practical suggestions on how to make Android programs run smoothly, quickly and efficiently with limited memory and battery resources:
1. Valid threads
1. How to use background threads to avoid overloading the foreground?
By default, all application operations are run on the main foreground thread (UI thread), and application responsiveness can be affected, resulting in crashes, crashes, and even system errors. In order to improve the responsiveness, long-running tasks (such as network or database operations, complex calculations) should be moved away from the main thread of the application and switched to run in a separate background thread.
The most efficient way to accomplish this 1 switch task is at the class level. You can use the
Class to organize background work. 1 Once you achieve 1
Which starts when needed, using a new 1 worker thread to process the request (intent).
You should consider the following limitations:
Activity. Only 1 request is processed at a time. No request processing can be interrupted.
2. How can I avoid an application that does not respond to ANR?
To avoid prolonged unresponsive ANR, unload long task operations from the UI main thread to the background thread, you can inherit
Ways to achieve
Another way is to create a thread or
Implementation class, it is worth noting that you also need to specify the "background" thread permission of the thread, because the default permission of this new thread is the same as that of UI thread, which will also slow down the running speed of the whole application.
3. How do I initialize a query in a separate thread?
Data display is not instant, although you use
Object can speed up the speed, and this object can be used without disturbing users and
In the case of interaction, the query is carried out in the background.
Use this object to arm your application for each
The query operation initializes a separate background thread, and then returns to the
IntentService2 Detect potential long task operations in UI threads that you want to implement operations. Use special tools, such as
IntentService4 To find response bottlenecks in your application. Display Operation Flow Icon If the initial installation is very time consuming, display the startup screen.
2. Optimization of equipment battery
The main effects on battery power consumption time are:Periodic wake-up updates Pass
IntentService6 Data transfer of Text data parsing is not
IntentService7 Regular expression of
5. How to optimize the network?If there is no connection, stop your application operation, only 3G or
IntentService8 Update when you can connect. Choose to compress data, such as packing text and binary data in one request. Use an efficient parser and use streams
IntentService9 Parsing instead of tree
IntentService0 Parser. Reduce the number of round trips with the server It is possible to use text data
IntentService1 , make full use of
6. How to optimize the foreground application?When designing wake-up locks, set the lowest possible level. Avoid potential
IntentService3 Battery consumption and use caused by
IntentService4 , put an end to the infinite loop. Activate
IntentService6 . Consider manually having GC garbage collect Java objects, such as
IntentService8 For regular use of the
IntentService0 ), using the
IntentService1 Be careful of synchronous operation
IntentService2 Although it is safe when driven by UI threads. In
ListViewThe recycling strategy should be used more often, and one component should be reused as much as possible Use network location instead of GPS whenever possible, In
onPauseThe GPS update must be unregistered in 1, and the user can activate GPS in the settings himself Calculating floating points requires a lot of batteries, so you may have to consider the accuracy of geography and mathematics when using
DPI tasks withThe result of the calculation is cached when.
7. How to optimize the application running in the background?
Because each process requires 2M, the current program needs to be restarted when it needs memory to ensure that each service has a short life cycle.
Keep memory consumption as low as possible
The design application is updated every 30 minutes, but only when the device wakes up.
Sleep is not good, answer to use
In this way
Element, which is called when completed
When starting a service, use the
Note that through
Reset so that Android can wrap your app updates when using
When, dynamically in the
Activate or disable its components, especially in the case of empty operation.
8. OthersCheck the battery and network status before making a complete update, and wait for the battery to be in good condition during batch processing Provides options for the user's battery usage to determine the update cycle and background behavior.
3. Implement UI with minimal memory
9. How do you identify layout performance issues?
When you create pages using Layout Manager, you may introduce 1 delay. In order to balance the UI with less memory consumption, you need to search for potential performance bottlenecks in the layout. You can use Android
Another tool that finds great performance is Lint, which can scan the application source code to find layers
10. How do you fix performance problems?
You can do this by squashing the layers, for example, by removing the
Class to use the
Class, lower the level.