Top ten tips for efficient Android app development

Source: Internet
Author: User

Why is it important to make sure that you're optimized, running smoothly, and not causing problems with Android when you use Android to develop apps? Because every problem that affects the efficiency of your app's products, such as power consumption or memory usage, is a key factor in app success. Small series for everyone to summarize the 10 efficient development of ANDROIDAPP recommendations, I hope you have some help.

Recommendation one: Efficient use of threads

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;

Recommendation two: Remain responsive 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.

Recommendation three: Initialize query operations in 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.

Recommendation IV: Areas to be aware of

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

Use special tools such as 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.

Recommendation five: 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.

Recommendation six: Optimize your app's work on 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;

Recommendation Seven: Optimize the work in front of the application

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.

Recommendation VIII: Battery Other precautions

Check the status of the battery and the network status before the overall update, wait for the best state to carry out large-scale operation;

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

Recommendation IX: How to find layout display problems

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 the Android sdk/tools/. Another good tool is lint, which scans the application's source code for possible bugs and optimizes control results.

Recommendation 10: Resolve the issue

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.

Each of the above suggestions seems to be a small improvement, but the details determine success or failure, in this customer pursuit of the ultimate Internet era, app application only to achieve the perfect, to be more users of the pro-gaze.

Top ten tips for efficient Android app development

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.