Android Programming's 10 recommendations for the efficient development of apps _android

Source: Internet
Author: User
Tags thread class

This article describes 10 recommendations for the efficient development of apps in Android programming. Share to everyone for your reference, specific as follows:

If you want Google Play to do a most failed case, the best secret is that the interface is incredibly slow, power consumption, memory consumption. Then you get negative comments from the user, and then your reputation stinks. Even if your application is well-designed and creative is no use.

Every problem that affects product efficiency, such as power consumption or memory footprint, can affect the success of the app. That's why it's important to make sure that it's optimized, running smoothly in development and doesn't make the Android system go wrong. There is no need to discuss efficient programming because we don't care if the code you write can withstand testing. Even efficient code takes time to run. Today in this article we will talk about how to shorten the running time as much as possible and how to develop the app that users like.

Efficient Use of threads

Recommendation one: How to cancel the action in some threads in the background

We know that all operations in the app run are done by default in the main thread (UI thread), so the app's response speed is affected. can cause the program to fall into the cotton, die or even a system error occurs.
In order to speed up the response, time-consuming operations, such as network requests, database operations, or complex computations, need to be moved from the main thread to the individual threads. 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 it is needed, and then processes the request through a worker thread (Intent).
You need to be aware of the following limitations when using Intentservice:

① This class does not pass information to the UI, if you want to show the user the processing results information, please use activity;
② can only handle one request at a time;
③ Each processing request process can not be interrupted;

Recommendation two: How to keep the response does not occur ANR

Removing time-consuming operations from the UI thread also prevents user actions from appearing in the system unresponsive (ANR) dialog box. All you have 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 also slows the app down, because the default thread priority is the same as the primary, unless you explicitly set the priority of the thread.

Recommendation three: How to initialize query operations in threads

The presentation data is not instantaneous when the query is being processed in the background, but you can use the Cursorloader object to speed up the operation so that the interaction between the activity and the user is unaffected.
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: Other aspects to be noted

① uses 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;
③ with the progress bar to show the user the operation progress;
④ If initialization is time-consuming, please show a welcome interface.

Optimize battery life of equipment

If the application is very electricity, please do not blame the user uninstall your application. For battery use, the main cost of electricity is as follows:

① often wakes up the program when updating data;
② use Edge or 3G to transmit data;
③ text data conversion for non-JIT regular expression operations.

Recommendation five: How to optimize the network

① If there is no network connection, ask your application to skip network operation, and update the data only if there is a network connection and no roaming;
② Select a compatible data format to convert all requests containing text data and binary data into binary data format requests;
③ use of efficient conversion tools, more consider the use of streaming conversion tools, less use of tree-shaped conversion tools;
④ for a faster user experience, please reduce the duplication of access to the server operation;
⑤ if possible, use the GZIP Library of the framework to compress text data to efficiently use CPU resources.

Recommendation six: How to optimize the application in front of the work

① If you consider using Wakelocks, try to set the minimum level;
② to prevent potential bugs from consuming electricity, specify the timeout period;
③ enable Android:keepscreenon properties;
④ In addition to the system's GC operations, consider manually recycling Java objects, such as Xmlpullparserfactory and Bitmapfactory. There is also regular expression of matcher.reset (newstring) operation, Stringbuilder.setlength (0) operation;
⑤ should pay attention to the problem of synchronization, although it is safe in the main thread;
⑥ in the ListView should be used more than repeated use strategy;
⑦ if allowed to use rough network positioning rather than GPS, compared to GPS needs 1mAh (25s * 140 MA), and the general network only 0.1mAh (2s * 180mA);
⑧ ensures that the GPS location update operation is logged off because the update operation will continue in OnPause (). When all applications are logged out of this operation, the user can re-enable the GPS in the system settings without wasting power;
⑨ consider using a low precision variable in a large number of mathematical operations and caching the variable value when the DPI task is performed with Displaymetrics;

Recommendation seven: How to optimize the application of work in the foreground

① Make sure that the service lifecycle is short-lived because each process requires 2MB of memory and is restarted when the foreground program requires memory;
② keep the use of memory not too large;
③ if you want to apply updates every 30 minutes, keep the device in a wake state;
④service is not good in pull or sleep state, which is why the Alarmmanager or configuration attribute stopself () is used at the end of the service.

Recommendation VIII: Other considerations

① Check the status of the battery and the network state before making the whole update, wait for the best state to carry on the large load change operation;
② lets the user see the electricity situation, for instance the renewal cycle, backstage operation time;

Implement low memory footprint UI

Recommendation IX: How to find layout display problems

When we create a separate UI for the layout, we are creating a memory-abusing app that has a nasty latency in the UI. The first step in implementing a fluent, low footprint UI is to search your application to find the potential bottleneck layout. Use the Hierarchy Viewer tool tool from the Android sdk/tools/.
Another good tool is lint, which scans the application's source to find possible bugs and optimizes control results.

Recommendation 10: How to solve the problem

If the layout shows the problem, you can consider simplifying the layout structure. LinearLayout types can be converted into relativelayout types, reducing the hierarchical structure of the layout.

To be more perfect and constantly optimized

Although each of these tips seems to be a minor improvement, if it can be part of your daily code, you'll see unexpected results. For Google Play to see more outstanding, smoother, faster, more power-saving apps, move on to Android's perfect goal.

I hope this article will help you with your Android programming.

Related Article

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.