Recommendations for improving the operational efficiency of Android

Source: Internet
Author: User

How to make the Android program run smoothly and quickly and efficiently under limited memory and battery resources, here are 10 practical recommendations:

A valid thread

1. How do I use a background thread to avoid front load loading?

By default, all operations of an application are run on the main thread (UI thread) of the foreground, and application responsiveness can be affected, causing a crash or even a system error. To improve responsiveness, you should move long-running tasks (such as network or database operations, complex computations) away from the main thread of the application and switch to running as a separate background thread.

The most effective way to do all this is to change tasks at the class level. Can you use the Asynctask class or the Intentservice class?? To organize back-office work. Once you have implemented a intentservice, it will start when needed, using a new worker thread to process the request (intent).

When using Intentservice, you should consider the following limitations:

    • This class does not send the results to the UI, so in order to display the results, you need to use activity.
    • Only one request is processed at a time.
    • Any request processing cannot be interrupted.

2. How do I avoid the application not responding to ANR?

To avoid an application that does not answer the ANR for a long time, unloading long task operations from the UI main thread to the background threads, you can do so by inheriting Asynctask and implementing the Doinbackground () method.

Another way is to create a thread or Handlerthread implementation class, it is worth noting that you also need to specify the thread's "background" thread permissions, because the new thread default permissions and the UI thread is the same, will also slow down the entire application run speed.

3. How do I initialize a query in a disconnected thread?

The data display is not instantaneous, although you can speed up your use of the Cursorloader object, the object can be queried in the background without disturbing the user and activity interactions.

Use this object to arm your application, initialize a separate background thread for each ContentProvider query operation, and then return to the activity only when the query is complete.

4. Other:

    • Use Strictmode to probe the potential long-task actions in the UI thread that you want to implement the operation.
    • Use special tools such as Systrace, TraceView to find the response bottlenecks in your app.
    • Show Action Process icon
    • If the initial installation is time consuming, the splash screen is displayed.

Optimization of equipment battery

The main impact on battery consumption time is:

    • Recurring Wake-up updates
    • Data transfer via 3G and Edge
    • Text data parsing regular expressions with no JIT

5. How to optimize the network?

    • If there is no connection, stop your app operation, only 3G or WiFi can be updated when connected.
    • Choose to compress the data, such as wrapping text and binary data in a single request.
    • Use the efficient parser to parse the stream stream instead of the tree.
    • Reduce round-trip times to the server
    • It is possible to use GZIP for text data to take full advantage of CPU resources.

6. How to optimize the foreground application?

    • When designing a wake-up lock, set the lowest level possible.
    • Avoid potential bugs caused by battery consumption, use timeout, and eliminate the dead loop.
    • Activates the Android:keepscreenon.
    • Consider manually letting GC garbage reclaim Java objects, such as Xmlpullparserfactory and bitmapfactory, for regular use of Matcher.reset (newstring), using Stringbuilder.setlength (0).
    • Be careful with the sync operation synchronization, although it is safe to be driven by the UI thread.
    • Reuse a single component as much as possible in the ListView by using the Recycle strategy
    • Use network positioning instead of GPs whenever possible,
    • Be sure to unregister the GPS update in the OnPause, the user can activate the GPS in the setting
    • Calculating floating point requires a lot of battery, you might want to consider the accuracy of geography math, etc. when using displaymetrics to perform DPI tasks with., the cache evaluates the results.

7. How to optimize the application of background running?

    • Because each process requires 2M, the current program needs to be restarted in memory, ensuring that each service is a short life cycle.
    • Try to keep memory consumption low
    • The app is designed to be updated every 30 minutes, but only in the event of device wakeup.
    • Service services sleep is not good, answer using Alarmmanager or <receiver> such manifest element, when finished call Stopself (), when the service is started, use Alarmmanager, use *_ Wakeup be aware that resetting with setinexactrepeating () allows Android to package your app updates, dynamically activating or invalidating its components in manifest when using <receiver>, especially in the case of empty operations.

8. Other:

    • Check the battery and network status before you make a full update, and wait for the battery to be in a better state for batch processing
    • Provides options for user battery usage to determine the update cycle and background behavior.

Implement UI with minimal memory

9. How can I tell the layout performance problem?

When you use the Layout Manager to create a page, you may introduce some delay, in order to achieve a balanced memory consumption of the UI, you need to search the layout of potential performance bottlenecks, you can use the Android SDK directory <sdk>/tools/under the Hierarchy Viewer tool.

Another discovery performance towering tool is lint, it can scan the application source level to find bugs.

10. How do I troubleshoot performance issues?

You can depress levels by flattening the hierarchy, for example, by converting from the LinearLayout class to using the Relativelayout class.

Recommendations for improving the operational efficiency of Android

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.