10 tips for developing Android apps efficiently

Source: Internet
Author: User

If you want Google Play to do one of the most unsuccessful cases, then the best secret is the interface is extremely slow, power consumption, memory consumption. Then you get negative comments from the user, and then the reputation stinks. Even if your application is well designed and creative, it's useless.

Every problem that affects product efficiency, such as power consumption or memory footprint, can affect the success of your app. That's why it's important to ensure that you're optimizing, running smoothly in development and not making Android systems problematic. There's no need to talk about efficient programming, because we don't care if the code you write can stand the test. Even efficient code takes time to run. In this article we will talk about how to shorten the running time as much as possible and how to develop user-preferred apps.

Efficient use of threading recommendation one: How to cancel actions in some threads in the background

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: How to 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: How to initialize a query operation in a 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: Other 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: How to optimize the 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: How to optimize the work of the application in 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: How to optimize the application of the work in the foreground
    • 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: Other considerations
    • Check the status and network status of the battery before making the overall update, and wait for the best state for a large-scale reload operation;
    • Let the user see electricity situation, such as update cycle, background operation;
Implementing low memory Footprint UI Recommendations 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: How to fix the problem

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.

To be more perfect and continuously optimized

While each of these suggestions looks like a small improvement, if it can be part of your everyday code, you'll see unexpected results. For Google Play to see more outstanding, smooth, faster, and more power-saving applications, take a step toward the perfect goal of Android.

Original link: Azoft translation: Bole online-Chris

10 tips for developing Android apps efficiently

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.