"Turn" Android application Development:10 Tips for more efficient Apps

Source: Internet
Author: User

The best recipe for becoming a all flop in Google Play are to release an app that's battery and memory hungry with a Slow interface. Most likely, these qualities would ensure negative reviews from the users and result in a bad reputation, even if your app have Great Designand a unique idea.

Every drawback in product efficiency, battery and memory consumption can really affect your app ' s success. That's why it's critical to develop well-optimized, smooth running apps, the never make Android system guard against them . We'll not speak about efficient coding, since it goes without saying so the code you write should stand any Performanc E test. But even brilliant code takes time to run. In today's post, we'll talk about how to minimize this time and make Android apps that users love.

Efficient Threadingtip 1. How to off-load operations onto threads in background

Since By default all operations in a app run on the main thread (UI thread) in the foreground, the app responsiveness MiG HT be affected, which would imminently result in hangs, freezes and even system errors.

To reinforce responsiveness, you should shift long-running tasks (e.g. network or database operations, complex calculation s) from the app ' s main thread to a separate background thread. The most effective-on-the-accomplish this task is at a class level. You can use the Asynctask class or Intentservice class to organize background work. Once you has implemented an intentservice, it starts when needed and handles requests (Intents) using a worker thread.

When using Intentservice, you should consider the following limitations:

    • This class does isn't put results to the UI, so-to-show results to the users use Activity.
    • Only one request was processed at a time.
    • Any request processing can interrupted.
Tip 2. How to avoid ANR and stay responsive

The same approach of off-loading long-running operations from the UI thread would save your users from the ' Application not ' Responding "(ANR) dialog. What are need to do are to create a background worker thread by extending Asynctask and Implementing Doinbackground () Metho D.

Another option is to create a Thread or Handlerthread class of your own. Keep in mind so unless you specify ' background ' priority for the thread, it'll slow down the app since the default thr EAD priority is the same as of the UI thread.

Tip 3. How to initiate queries on separate threads

Displaying data isn't immediate, although you can fasten it by using Cursorloader objects, which allows isn't to distract A Ctivity from interacting with a user and query is processing in the background.

Armed with the object your app would initiate a separate background thread for each ContentProvider query and return ResU LTS to Activity from which the query were called only if the query is finished.

Tip 4. What else can do
    • Use Strictmode to detect potentially lengthy operations the UI thread.
    • Use special tools, I.G Systrace, Traceview, to find bottlenecks in your app responsiveness.
    • Show progress to users with a progress bar.
    • Display Splash Screens If initial setup is time-consuming.
Device Battery Life Optimization

We cannot blame users for angrily uninstalling applications this abuse battery life. The main threats to battery life is:

    • Regular wake-ups for updates
    • Data transfer via EDGE and 3G
    • Textual data parsing, regex without JIT
Tip 5. How to optimize networking issues
    1. Make your app skip operations if there is no connection; Update only if 3G or WiFi are connected and there is no roaming.
    2. Choose compact data format, e.g binary formats that combine text and binary data into one request.
    3. Use efficient parser; Consider choosing stream parsers over tree parsers.
    4. For faster UX lessen round-trip times to server.
    5. When the possible use framework is GZIP libs for text data, the best use of the CPU resources.
Tip 6. How to optimize apps working in foreground
  1. When designing Wakelocks, set the lowest level possible.
  2. To avoid battery costs caused by potential bugs you might has missed, use specific timeouts.
  3. Enable Android:keepscreenon.
  4. In addition to GC (garbage collection) Consider recycling Java objects manually, e.g. Xmlpullparserfactory and Bitmapfacto Ry Matcher.reset (newstring) for regex; Stringbuilder.setlength (0).
  5. Mind synchronization issues, although it can is safe when the driven by UI thread.
  6. Recycling Strategies is used heavily in ListView.
  7. Use the coarse network location is not a GPS when possible. Just Compare 1mAh for GPS (+ sec. * 140mA) and 0.1mAh for network (2 seconds * 180mA).
  8. Make sure to unregister as GPS location updates can continue even after onPause (). When all applications unregister, users can enable GPS in Settings without blowing the battery budget.
  9. Since the calculation of a floating point requires lots of battery power, you might consider using Microdegrees for bulk g EO math and caching values when performing DPI tasks with Displaymetrics.
Tip 7. How to optimize apps working in background
    1. Since each process requires 2MB and might is restarted when foreground apps need memory, make sure the services is short- Lived.
    2. Keep memory usage Low.
    3. Design app to update every-minutes but only if device is already awake.
    4. Services that pall or sleep was bad, that's why you should use Alarmmanager or <receiver> manifest elements:stopse LF () when finished. When your start service using Alarmmanager, apply the *_wakeup flags with caution. Let Android bin your application update together with the rest through setinexactrepeating (). When using <receiver>, enable/disable it in manifest dynamically, especially when No-ops.
Tip 8. What else can do
    • Check current states of battery and network before launching a full update; Wait for better states for bulk transfers.
    • Provide users with battery usage options, e.g. update intervals and background behavior.
Implementing UI that leaves minimum memory Footprintstip 9. How to identify layout performance problems

When creating UI sticking solely to basic features for layout managers, you risk to create memory abusing apps with Annoyin G delays in the UI. The first step to implementation of a smooth, memory caring UI are to search your application for potential layout Performa nCE bottlenecks with Hierarchy Viewer tool included into Android SDK: <sdk>/tools/.

Another great tool for discovering performance issues is Lint. It scans application sources for possible bugs along with view hierarchy optimizations.

Tip 10. How to fix them

If layout performance results reveal certain drawbacks, you might consider to flatten the layout by converting it from Lin Earlayout class to Relativelayout class, lowing level hierarchy.

To perfection and beyond

Even though each tip mentioned above might seem like a rather small improvement, you might see unexpectedly efficient resu LTS If these tips become an essential part of your daily coding. Let Google Play See more brilliant apps--smoothly, quickly, and consume less battery power, bringing the Android World one, step closer to perfection.

"Turn" Android application Development:10 Tips for more efficient Apps

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.