Optimize Android app performance? Ten tricks must be known!

Source: Internet
Author: User

Whether the hammer or eggplant phone is constantly popping up, the Android system's mobile market share is still the largest, so the number of apps based on Android is also very large. So, how can you develop a higher-performance Android App? It is a big headache for software development companies and programmers in general. Today, we offer a few tips to improve the performance of Android apps.   Efficient use of threads 1. Cancel actions 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 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 be aware of when using Intentservice:  This class does not pass information to the UI, if you want to show the user processing results information, use activity; only one request can be processed at a time Each processing request process can not be interrupted;  2. Maintain response does not occur when ANR removes the time-consuming action from the UI thread this way 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.    3. Initializing a query 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.    4.Other aspects to be aware use Strictmode to check for potentially time-consuming actions in the UI thread, use special tools such as Safe.ijiami, Systrace, or TraceView to find bottlenecks in your application, and 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 application is very power-consuming, please do not blame the user for uninstalling your app. For battery use, the main charge is the following:  update data when the program is often awakened, using edge or 3G to transfer data, text data conversion, non-JIT regular expression operation.  5. Optimize your network If you do not have an Internet connection, let your app skip network operations, update the data only if there is a network connection and no roaming, select a compatible data format, convert all requests containing textual and binary data into binary data format requests, and use efficient conversion tools to Consider the use of streaming conversion tools, less tree-based conversion tools; For a faster user experience, reduce the duplication of access to the server, and if you can, use the framework's gzip library to compress text data to efficiently use CPU resources.  6. Optimize application work in the front end if you consider using Wakelocks, try to set it to the minimum level, specify the time-out period to prevent potential bugs, and enable the Android:keepscreenon property, in addition to the system's GC operation, Consider manually recycling Java objects, such as Xmlpullparserfactory and Bitmapfactory. There are also Matcher.reset (newstring) operations for regular expressions, Stringbuilder.setlength (0) operations, and attention to synchronization issues, although it is safe in the main thread, and multiple reuse policies are used in the ListView , if allowed to use rough network positioning instead of GPs, compared to GPS needs 1mAh (25s * mA), and the general network only use 0.1MAH (2s * 180mA); Make sure to unregister the GPS location update operation, because this update operation in OnPause () will continue in the same. When all applications are logged out of this operation, the user can re-enable GPs in system settings without wasting power; Consider using low-precision variables in a large number of mathematical operations and caching variable values when DPI tasks are performed with Displaymetrics;  7. Optimization work in foreground application please ensure that the service life cycle is short, because each process requires 2MB of memory, and when the foreground program needs memory, it will restart; Keep the memory usage not too large; If you want to apply every 30 minutesUpdate once, when the device is awake, service in pull or sleep state is not good, this is why at the end of the service to use Alarmmanager or configuration Properties stopself () reason.  8. Other considerations Check the status of the battery and the network status before making the overall update, and wait for the best state for a large reload operation; Let the user see the power situation, such as the update cycle, background operation time;   Achieve low memory footprint UI9. Finding 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/.     a good tool is lint, which scans the application's source code for possible bugs and optimizes control results.    10. Troubleshoot problems if the layout shows the results, you can consider simplifying the layout structure. You can convert the LinearLayout type to a relativelayout type, reducing the hierarchy of the layout.     pursue perfection and constantly optimize each of these tips, and hopefully it will be part of your everyday code, and 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.

Optimize Android app performance? Ten tricks must be known!

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.