Home  >  Article  >  Backend Development  >  Top Ten Tips to Optimize Android App Performance

Top Ten Tips to Optimize Android App Performance

巴扎黑
巴扎黑Original
2016-11-11 13:31:031072browse

Regardless of the continuous emergence of hammer or eggplant mobile phones, Android system still has the largest market share of mobile phones, 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 major headache for software development companies and programmers. Today, I will provide you with several tips to improve the performance of Android Apps.

Use threads efficiently

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, so the response speed of the App will be affected Influence. It will cause the program to freeze, die, or even cause system errors. To speed up response times, time-consuming operations (such as network requests, database operations, or complex calculations) need to be moved 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 be started when needed, and then handle the request (Intent) through a worker thread.

You need to pay attention to the following restrictions when using IntentService:

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

2. Only one request can be processed at a time;

3. Each request processing process cannot be interrupted;

2. Keep the response without ANR

Removing time-consuming operations from the UI thread This method can also prevent the system not responding (ANR) dialog box from appearing in user operations. 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. Note that this will also slow down the app because the default thread priority is the same as the main thread's priority unless you explicitly set the thread's priority.

3. Initialize the query operation in the thread

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

After using this object, your App will initialize an independent background thread for ContentProvider to query, and when the query is completed, the results will be returned 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 some special tools such as Safe.ijiami, Systrace or Traceview to find bottlenecks in your application;

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

If the initialization operation is time-consuming, please display a welcome interface.

Optimize your device’s battery life

Don’t blame users for uninstalling your app if it consumes battery. Regarding battery usage, the main power consumption is as follows:

Frequently wake up the program when updating data;

Use EDGE or 3G to transfer data;

Convert text data and perform non-JIT regular expression operations.

5. Optimize the network

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

Choose a compatible data format and convert text data and All binary data requests are converted into binary data format requests;

Use efficient conversion tools, consider using streaming conversion tools more and less tree-shaped conversion tools;

For a faster user experience, please reduce repeated visits to the server Operation;

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

6. Optimize the application’s front-end work

If you consider using wakelocks, try to set it to the smallest level;

To prevent battery consumption caused by potential bugs, please specify the timeout clearly;

Enable the android:keepScreenOn attribute;

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

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

Use more reuse strategies in Listview ;

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

Make sure to log out of the GPS location update operation , because this update operation will also continue in onPause(). When all applications have logged out of this operation, users can re-enable GPS in system settings without wasting battery;

Please consider using low-precision variables in heavy mathematical operations and caching variable values ​​when performing DPI tasks with DisplayMetrics;

7. Optimize applications that work in the foreground

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

Maintain memory usage Not too big;

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

It is not good for Service to be in pull or sleep state, which is why AlarmManager or configuration property stopSelf() should be used when the service ends.

8. Other precautions

Check the battery status and network status before performing the overall update, and wait for the best condition before performing a large-scale replacement operation;

Let users see the power usage, such as update cycle, background During operation;

Implement low memory usage UI

9. Find the layout display problem

When we create a separate UI for the layout, we are creating an App that abuses memory, and it will cause a nasty delay in the UI. The first step in achieving a smooth, low-memory UI is to search your application for potential bottleneck layouts. Use Safe.ijiami and the Hierarchy Viewer Tool tool that comes with Android SDK/tools/.

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

10. Solve the problem

If problems are found in the layout display results, you can consider simplifying the layout structure. You can convert the LinearLayout type into the RelativeLayout type to reduce the layout hierarchy.

For each of the above tips, I hope it can become a 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 closer to the goal of Android perfection.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn