10 Suggestions for Improving the Operational Efficiency of android

  • 2021-12-21 05:14:07
  • OfStack

Directory 1. Effective threads 1. How to use background threads to avoid overloading the foreground? 2. How to avoid applications not responding to ANR? 3. How do I initialize a query in a separate thread? 4. Others 2. Equipment battery optimization 5. How to optimize the network? 6. How to optimize the foreground application? 7. How to optimize the application running in the background? 8. Others 3. Implement UI with minimal memory 9. How do you identify layout performance issues?

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 AsyncTask Class or IntentService Class to organize background work. 1 Once you achieve 1 IntentService Which starts when needed, using a new 1 worker thread to process the request (intent).

When using IntentService You should consider the following limitations:

This class does not send the results to UI, so in order to display the results, you need to use 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 AsyncTask And implementation doInBackground() Ways to achieve

Another way is to create a thread or HandlerThread 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 CursorLoader Object can speed up the speed, and this object can be used without disturbing users and Activity In the case of interaction, the query is carried out in the background.

Use this object to arm your application for each IntentService0 The query operation initializes a separate background thread, and then returns to the Activity .

4. Others

Use IntentService2 Detect potential long task operations in UI threads that you want to implement operations. Use special tools, such as IntentService3 , 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 IntentService5 And 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 IntentService2 Resources.

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 IntentService5 : IntentService6 . Consider manually having GC garbage collect Java objects, such as IntentService7 And IntentService8 For regular use of the IntentService9 ( IntentService0 ), using the IntentService1 Be careful of synchronous operation IntentService2 Although it is safe when driven by UI threads. In ListView The 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 onPause The 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 DisplayMetrics Execute DPI tasks with The 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.
Services Service Sleep is not good, answer to use AlarmManager Or <receiver> In this way manifest Element, which is called when completed stopSelf() When starting a service, use the AlarmManager , using *_WAKEUP Note that through setInexactRepeating() Reset so that Android can wrap your app updates when using <receiver> When, dynamically in the manifest Activate or disable its components, especially in the case of empty operation.

8. Others

Check 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 SDK Directory <sdk>/tools/ Under Hierarchy Viewer Tools.

Another tool that finds great performance is Lint, which can scan the application source code to find layers IntentService3 .

10. How do you fix performance problems?

You can do this by squashing the layers, for example, by removing the AsyncTask 1 Class to use the AsyncTask 2 Class, lower the level.


Related articles: