Optimize Android app performance? Ten tricks must be known!

Source: Internet
Author: User

Whether the hammer or eggplant phone is constantly popping up, the Android system's mobile market share is still the largest, so the number of apps based on Android is also very large. So, how can you develop a higher-performance Android App? It is a big headache for software development companies and programmers in general. Today, we offer a few tips to improve the performance of Android apps.


Efficient use of threads1. Cancel actions in some threads in the background

We know that all of the actions in the app run are done by default in the main thread (UI thread), so that the app's responsiveness is affected. can cause the program to stutter, die, or even get a system error.

To speed up responsiveness, 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 to do this is to do this at the class level, and you can use Asynctask or intentservice to create a background operation. If you choose to use Intentservice, it starts up when needed and then processes the request (Intent) through a worker thread.

There are a few limitations to note when using Intentservice:

    • This class does not pass information to the UI, if you want to show the user processing results information, please use activity;

    • Only one request can be processed at a time;

    • Each processing request process can not be interrupted;


2. Maintain response does not occur in ANR

Removing time-consuming actions from the UI thread also prevents user actions from appearing in the system not responding (ANR) dialog box. All you need to do is inherit asynctask to create a background worker thread and implement the Doinbackground () method.

Another way is to create a thread class or Handlerthread class yourself. It is important to note that this also slows down the app because the default thread priority is the same as the primary, unless you explicitly set the priority of the thread.

3. Initializing query operations in the thread

When the query operation is being processed in the background, the presentation data is not instantaneous, but you can use the Cursorloader object to speed up the interaction between the activity and the user.

After using this object, your app will initialize a separate background thread for ContentProvider to query, and when the query is finished, it will return results to the activity that invoked the query.

4. Other areas to be aware of
  • Use Strictmode to check for potentially time-consuming operations in the UI thread;

  • Use special tools such as Safe.ijiami, Systrace, or TraceView to find bottlenecks in your application;

  • Use the progress bar to show the user the progress of the operation;

  • If the initialization operation is time consuming, please show a welcome interface.


Optimize the battery life of your device

If the app is very power-consuming, don't blame the user for uninstalling your app. For battery use, the main charges are as follows:

    • Wake up the program frequently when updating data;

    • Use Edge or 3G to transmit data;

    • Text data conversion for non-JIT regular expression operations.


5. Optimize your network
  • If you don't have an Internet connection, let your app skip network operations and update the data only if there is a network connection and no roaming;

  • Select a compatible data format to convert all requests containing textual data and binary data into binary data format requests;

  • Using efficient conversion tools, consider using streaming conversion tools, less tree-shaped conversion tools;

  • For a faster user experience, reduce the duplication of access to the server;

  • If you can, use the framework's gzip library to compress text data to efficiently use CPU resources.


6. Optimize the work of the application in the front end
  • If you consider using wakelocks, try to set it to the minimum level;

  • To prevent potential bug-induced power consumption, specify the time-out period explicitly;

  • Enable the Android:keepscreenon property;

  • In addition to the system's GC operations, consider manually recycling Java objects, such as Xmlpullparserfactory and Bitmapfactory. There are also regular expression Matcher.reset (newstring) operations, Stringbuilder.setlength (0) operations;

  • Be aware of the problem of synchronization, although it is safe in the main thread;

  • In the ListView, the reuse strategy should be used more.

  • If allowed to use more rough network positioning instead of GPs, compared to GPS need 1mAh (25s * mA), and the general network only with 0.1mAh (2s * 180mA);

  • Make sure to unregister the GPS location update operation, as this update operation will continue in OnPause (). When all applications are logged out of this operation, the user can re-enable GPS in the system settings without wasting power;

  • Consider using low-precision variables in a large number of mathematical operations and caching the variable values when using Displaymetrics for DPI tasks;


7. The application of the optimization work in the foreground
  • Make sure that the service life cycle is short, because each process requires 2MB of memory and restarts when the foreground program needs memory;

  • Keep the amount of memory in use not too large;

  • If you want the app to be updated every 30 minutes, do so at the device's wake-up state;

  • Service is not good in pull or sleep state, which is why you should use Alarmmanager or configure attribute stopself () at the end of the service.


8. Other Precautions
    • Check the status and network status of the battery before making the overall update, and wait for the best state for a large-scale reload operation;

    • Let the user see electricity situation, such as update cycle, background operation;


Implementing a low memory footprint UI9. Find the layout display problem

When we create a UI for the layout alone, we create an app that abuses memory, and it has a nasty delay in the UI. To achieve a smooth, low-footprint UI, the first step is to search your app to find out the potential bottleneck layout. Use the Hierarchy Viewer tools tool that comes with Safe.ijiami and Android sdk/tools/.

Another good tool is lint, which scans the application's source code for possible bugs and optimizes control results.

10. Problem solving

If the layout shows a problem, you can consider simplifying the layout structure. You can convert the LinearLayout type to a relativelayout type, reducing the hierarchy of the layout.

The pursuit of perfection and continuous optimization

With each of these tips, hopefully it will be part of your everyday code, and you'll see unexpected results. For Google Play to see more outstanding, smooth, faster, and more power-saving applications, take a step toward the perfect goal of Android.


Optimize Android app performance? Ten tricks must be known!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.