Top 10 Tips to Optimize Android App Performance

Top 10 Tips to Optimize Android App Performance

[[121763]]

Regardless of the emergence of Hammer and Eggplant phones, the market share of Android phones is still the largest, so the number of apps developed based on Android is also huge. So, how can we develop higher-performance Android apps? I believe this is a big headache for software development companies and programmers. Today, I 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 operations during the running of the App are performed in the main thread (UI thread) by default, which will affect the response speed of the App, causing the program to freeze, crash, or even cause system errors.

In order to speed up the response, you need to move time-consuming operations (such as network requests, database operations, or complex calculations) 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 when needed and then process the request (Intent) through a worker thread.

There are a few limitations to note when using IntentService:

  • This class should not pass information to the UI. If you want to display the processing result information to the user, please use Activity;

  • Only one request can be processed at a time;

  • Each request processing process cannot be interrupted;

2. Keep responding without ANR

Removing time-consuming operations from the UI thread can also prevent the user from seeing 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. Please note that this will also slow down the App, because the default thread priority is the same as the main thread priority unless you explicitly set the thread priority.

3. Initialize the query operation in the thread

When the query operation is being processed in the background, the display of data is not instant, but you can use the CursorLoader object to speed up the process. This operation can keep the interaction between Activity and user unaffected.

After using this object, your App will initialize an independent background thread for ContentProvider to query, and when the query is completed, it will return the result to the Activity that called the query.

4. Other aspects that need attention

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

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

  • Use progress bars to show users the progress of operations;

  • If the initialization operation is time-consuming, display a welcome screen.

Optimize your device's battery life

If your app consumes a lot of battery, please don't blame the user for uninstalling your app. For battery usage, the main battery consumption is as follows:

  • Wake up the program frequently when updating data;

  • Use EDGE or 3G to transmit data;

  • Text data conversion, performing non-JIT regular expression operations.

5. Optimize the network

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

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

  • Use efficient conversion tools, consider using streaming conversion tools more often and use less tree-based conversion tools;

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

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

6. Optimize the application's work on the front end

  • If you consider using wakelocks, try to set them to the lowest level possible;

  • To prevent potential bugs from causing battery drain, specify a timeout explicitly;

  • Enable the android:keepScreenOn attribute;

  • In addition to the system GC operation, consider manually recycling Java objects, such as XmlPullParserFactory and BitmapFactory. There are also the regular expression Matcher.reset(newString) operation and StringBuilder.setLength(0) operation;

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

  • Use more reuse strategies in Listview;

  • If possible, use rough network positioning instead of GPS. GPS requires 1mAh (25s * 140 mA), while the general network only uses 0.1mAh (2s * 180mA).

  • Make sure to unregister GPS location updates, because these updates will continue in onPause(). When all apps unregister this operation, users can re-enable GPS in the system settings without wasting power;

  • Consider using low-precision variables for heavy math operations and caching variable values ​​when using DisplayMetrics for DPI tasks;

7. Optimize the application of work in the foreground

  • Please make sure that the service life cycle is short-lived, because each process requires 2MB of memory and will be restarted when the foreground program needs memory;

  • Keep memory usage small;

  • If you want the app to update every 30 minutes, do it when the device is awake;

  • It is not good for a Service to be in pull or sleep state, which is why you should use AlarmManager or configure the property stopSelf() when the service ends.

8. Other matters needing attention

  • Before performing the overall update, check the battery status and network status, and wait for the best status before performing a major replacement operation;

  • Allow users to see power usage, such as update cycles and background operations;

Implementing low memory usage UI

9. Find layout display problems

When we build UI for layouts alone, we are creating apps that abuse memory and cause terrible delays in the UI. To achieve a smooth, low-memory UI, the first step is to search your app for potential bottleneck layouts. Use Safe.ijiami and the Hierarchy Viewer Tool in the Android SDK/tools/.

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

10. Problem Solving

If you find problems with the layout display results, you can consider simplifying the layout structure. You can convert the LinearLayout type to the RelativeLayout type to reduce the layout hierarchy.

Pursue more excellence and continuous optimization

I hope that each of the above tips can become part of your daily code, and then you will see unexpected results. Let Google Play see more outstanding, smooth, faster, and more power-saving applications, and take a step towards the goal of Android's development.

Original article address: Optimizing Android App performance? Top ten tips

<<:  Statistical analysis of mobile game players’ payment behavior in 2014

>>:  Chukong's Cocos has completed integration with Google AdMob advertising platform

Recommend

A family member is suddenly diagnosed with cancer, what can we do for them?

It is inevitable that relatives, friends, and acq...

Uncover the iOS apps that only Apple employees can use

Currently, Apple's App Store has nearly 1.3 m...

6 simple and practical principles to effectively increase the use of coupons

Friends who do not need coupons in their marketin...

Geek Academy "Unity3D Engineer" Junior, Intermediate, Senior and Senior Engineer

Course Catalog ├──01-Junior Unity3D Development E...

How to optimize and quickly increase the number of app store users?

On my first day at CallApp, the CEO gave me an ex...