Top 10 tips to optimize Android App Performance

Source: Internet
Author: User

Top 10 tips to optimize Android App Performance

No matter whether the hammer or eggplant mobile phone keeps popping up, the mobile phone market share of the Android system is still the largest, so the number of apps developed based on Android is also very large. So how can we develop a higher-performance Android App? I believe it is a headache for software development companies and programmers. Today, we will provide you with several tips to improve the performance of Android apps.

Efficient use of threads

1. Cancel actions in some threads 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;

2. Keep the response from ANR

Remove time-consuming operations from the UI thread. This method also prevents the user operation from responding to ANR. 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.

3. Initiate 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.

4. Other considerations

  • Use StrictMode to check possible time-consuming operations in the UI thread;

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

  • Displays the operation progress with a progress bar;

  • If Initialization is time-consuming, a welcome page is displayed.

Optimized battery life of devices

If the application is too expensive, do not blame the user for uninstalling your application. For battery use, the main power consumption is as follows:

  • The program is often awakened when data is updated;

  • Use EDGE or 3G to transmit data;

  • Converts text data to non-JIT regular expressions.

5. Optimize the network

  • If there is no network connection, please let your application skip network operations; update data only when there is a network connection and no roaming;

  • Select a compatible data format to convert all requests that contain text data and binary data into binary data;

  • The use of efficient conversion tools, streaming conversion tools, less tree conversion tools;

  • For faster user experience, Please reduce repeated server access operations;

  • If possible, use the GZIP library of the framework to compress text data to efficiently use CPU resources.

6. Optimize the front-end work of the application

  • If you want to use wakelocks, set it to the minimum level;

  • To prevent power consumption caused by potential bugs, specify the timeout time;

  • Enable the android: keepScreenOn attribute;

  • In addition to the system's GC operations, you need to manually recycle Java objects, such as XmlPullParserFactory and BitmapFactory. There are also the Matcher. reset (newString) and StringBuilder. setLength (0) operations of the regular expression;

  • Pay attention to the synchronization problem, although it is safe in the main thread;

  • The Reuse Policy should be adopted in Listview;

  • If it is allowed, use a rough network location instead of GPS, and compare the GPS with 1 mah25s * 140 mA), while the general network only uses 0.1mAh2s * 180mA );

  • Make sure that the location update operation of the GPS is canceled because the update operation will continue in onPause. When all applications cancel this operation, you can re-enable GPS in system settings without wasting power;

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

7. Optimization of front-end applications

  • Make sure that the service life cycle is short, because each process requires 2 MB of memory, and the front-end program restarts when the memory is required;

  • Keep the memory usage not too large;

  • If you want to update the application every 30 minutes, perform this operation when the device is in the wake-up status;

  • The Service status in pull or sleep is poor, Which is why AlarmManager or the attribute stopSelf () should be used at the end of the Service.

8. Other considerations

  • Check the battery status and network status before the overall update, and wait for the best status to undergo a large change;

  • Allows users to see the electricity usage, such as the update cycle and background operations;

UI for low memory usage

9. Locate the layout display problem

When we create a UI for the layout separately, we are creating an App with memory abuse. It will have a bad latency in the UI. To implement a smooth and Low-memory UI, the first step is to search your application to find out the potential bottleneck layout. Use the Hierarchy Viewer Tool that comes with Safe. ijiami and Android SDK/tools.

Another good tool is Lint, Which scans the application source code to find possible bugs and optimizes the control results.

10. Solve the Problem

If the layout display result shows a problem, you can consider simplifying the layout structure. The LinearLayout type can be converted to the RelativeLayout type to reduce the hierarchical structure of the layout.

Pursuing perfection and continuous optimization

Every tips above, hopefully it can become part of your daily code, and then you will see unexpected results. To enable Google Play to see more outstanding, smooth, fast, and power-saving applications, it is a step towards the perfect target of Android.

Original article address: optimized Android App performance? Top 10 skills

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.