随着手机平台的发展和所提供应用的提升,质量成为应用成功的最重要因素。如果那些应用无法提供强大的功能和稳定的用户体验,那就会很快被用户卸载。开发者需要记住的是,虽然Android智能手机和平板电脑的速度日益提升,但其应用仍然运行在资源有限的环境中,其电池及处理器性能与最新的台式电脑、手提电脑仍存在差距。以下是某些优化应用运行效果的方法:
我们先来阐述某些让应用反应灵敏的编程技巧。
技巧1:从优秀的编程开始
要采用已为用户所接受的运算法则和标准的设计样式,这些被人们长期使用的编程法则也同样适用于Android应用,尤其当这些应用使用内在设备服务时。
比如,假设你编写的应用需要以地理定位服务为基础。只需要在必要时开始注册进行位置更新,在无需更新信息时,确保应用停止更新进程。这会帮助节省设备的电量和系统处理器的负担。
技巧2:保持应用的灵活性
通过使用AsyncTask、IntentService或自定义背景服务来保持应用的灵活性。使用加载器来简化加载时间较长数据的状态管理,比如光标。不可让应用在其他进程进行时显得缓慢或完全静止。
如果某些操作需要一定的时间和资源,应当将这个进程单独分离出来异步处理,这样你的应用才能够保持流畅的运行。可以运用这种方法的操作包括:磁盘读写,访问内容供应方、数据库和网络,其他需要较长时间的任务。
技巧3:使用最新的Android SDK版本和API
保持应用的更新,使用Android平台提供的最新内容。随着Android平台的发展,它也在逐步改善中。某些功能被移除,或者替换成更好的选项。其核心API中的漏洞已修复,整个API性能已得到提升。该平台已引入装载器之类的新API,帮助开发者编写更为稳定和反应灵敏的应用。
Android 3.0应用支持硬件加速,你可以加以应用。应当理解的是,最佳的表现情况会随着时间逐渐改变。睿智的开发者会更新平台发布的最新内容和API。
技巧4:检查Strict Mode
你可以使用称为“StrictMode”的Android API来查找编程中的问题。StrictMode会帮助你识别应用是否正在耗费内存,也可以帮你检查应用是否正在尝试开展漫长的模块化操作。
StrictMode类(游戏邦注:即android.os.StrictMode)与Android 2.3同期发布。
技巧5:在发布之前停用或最小化调试和诊断
你在Android应用的开发中可能会将某些调试代码构建其中。在应用发布之前确保这些功能被最小化或完全停用。
接下来,让我们来讨论如何用优秀的用户界面设计原则让你的应用加载速度更快。
技巧6:保持布局简洁自然
简洁自然的布局会加快加载速度。不要让屏幕布局中充斥过多不必要的内容。花点时间开发用户可以有效使用的简洁用户界面,不要将过多的功能性内容塞入单个屏幕中。这不仅对应用表现有帮助,而且会帮助用户更有效地使用应用。
分割内容可以帮助划分用户界面功能性,同时不牺牲应用在各种不同设备上的灵活性。
技巧7:根据目标设备调整应用资源
根据特定的设备配置来调整资源,这样它们就能够有效地加载。在图像资源方面,这个显得尤为重要。如果你的应用中有大型的图片资源需要加载,那么要做好调整。
另一个技巧是,当以许多种设备为目标时,保持应用包文件大小合适,只需要在其中包含应用运行所需的核心资源即可,然后让用户根据具体设备下载应用其他内容。
技巧8:使用Hierarchy Viewer工具
Hierarchy Viewer工具可以帮助你解除应用布局中的漏洞。它还提供了许多有价值的信息,比如每个View控制需要多长的时间。找到问题所属领域,这样解决问题会更加简单。
技巧9:使用layoutopt工具
layoutopt工具是个简单的命令行工具,可以帮助你识别不必要的控制和其他让你布局资源崩溃的事项,提升其性能。它可以帮助你找到不必要的多余布局控制。较少和较浅布局可优化应用运行性能。
最后,在自认为应用达到最好状况时,对其进行测试。
技巧10:使用Traceview和其他Android应用压缩工具
Android SDK中有许多可以压缩应用的工具。可能最流行的工具就是Traceview,这个图像工具可以帮助你调试和找到应用的性能问题。
结语
目前有许多方法可以让你的Android应用运行加速。有些涉及到使用某种具体的运算法则,其他依靠某些真正的调试和运行监管技术。Android平台中有许多种免费的工具可以用来帮助跟踪和调整应用中的表现问题。你已经知道了以上10种技巧,现在可以尝试使用它们!(本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦)
10 Android App Performance Tips for Developers
Lauren Darcey & Shane Conder
As any mobile platform evolves and its application offerings improve, quality becomes paramount to success. These days, users demand responsiveness and reasonable performance from the Android applications they choose to install on their devices. And if those applications fail to deliver both exciting functionality and a stable user experience, then the application is quickly uninstalled.
Although Android smartphones and tablets are getting faster every day, developers need to remember that their applications are still running in a resource-constrained environment powered primarily by battery and processors that do not rival the latest desktops or laptops. Here are some ways to slim down your applications so they run optimally on the Android devices of today and tomorrow.
Let’s start with some coding tips for responsive applications.
Android App Performance Tip #1: Start with Good Coding Practices
Be a good steward and use common sense, well-established algorithms and standard design patterns. Resource-wise, if you open it, close it. Acquire late, release early. These long-standing coding mantras apply to your Android applications as well, especially if they use underlying device services.
For example, let’s say you’ve written an application that relies upon location-based services. Don’t start register for location updates until you absolutely have to, and make sure you un-register for updates as soon as you no longer need the information. This will help you not drain the device battery or hog the system unnecessarily.
Android App Performance Tip #2: Keep Blocking Operations Off the Main UI Thread
Keep your applications nimble by using an AsyncTask, thread, IntentService, or custom background service to do the dirty work. Use loaders to simplify state management of long loading data, such as cursors. You cannot afford for your application to lag or freeze while some processing is going on.
If an operation takes time and resources, offload that processing and perform it asynchronously so that your application remains responsive and the user can go about their business. This applies to operations such as: reading and writing to disk, accessing content providers, databases and the Internet, as well as parsing and other lengthy tasks.
Android App Performance Tip #3: Use the Latest Android SDK Versions, APIs and Best Practices
Keep your applications up-to-date and using the latest that the Android platform has to offer. As the Android platform evolves, it is improved. Some features may be retired, or replaced with better options. Core APIs receive bug fixes and performance improvements. New APIs like loaders have been introduced to help developers write more stable and responsive applications.
Did you know you can enable hardware acceleration in your Android 3.0 apps? Do it! Understand that best practices change over time. Smart developers stay on top of what’s new in the platform and what’s no longer recommended.
Android App Performance Tip #4: Check Out Strict Mode
You can use an Android API called StrictMode to help you track down violations in several good coding practices. StrictMode will help you identify if your application is leaking memory, as well as detect whether or not your application is trying to perform lengthy blocking operations that should be offloaded to threads or otherwise (see #2).
The StrictMode class (android.os.StrictMode) was introduced in Android 2.3.
Android App Performance Tip #5: Disable or Minimize Debugging and Diagnostics Prior to Publication
If your Android application has taken more than a few minutes to develop, you’ve probably got some logging and debugging code built into your application. Writing to logs and other such output comes at a performance hit. Make sure these features are minimized or completely disabled prior to release.
Now let’s talk about how to make your application screens load faster with good user interface design principles:
Android App Performance Tip #6: Keep Your Layouts Simple, Elegant, and Shallow
Simple screens make for the easiest reading and simple layouts load the fastest. You don’t want to nest your layouts too deeply or clutter your screens with more View controls than absolutely necessary. Spend the time to develop elegant user interfaces that users can use effectively instead of trying to cram too much functionality onto a single screen. Not only will this help application performance, but it will help make your application more efficient for users.
Fragments can help compartmentalize user interface functionality without sacrificing flexibility for designing for different types of devices.
Android App Performance Tip #7: Tailor Your Application Resources To Your Target Devices
Include resources that are tailored to specific device configurations, so that they load as efficiently as possible. This is especially important when we’re talking about graphics resources. If you include huge drawable image resources that require loading and resizing, you’re not using alternative application resources effectively.
Another trick? To keep your application package file reasonably sized when targeting many devices, only include core resources needed to run the application initially, then have the application download content tailored to the device.
Android App Performance Tip #8: Use the Hierarchy Viewer Tool
The Hierarchy Viewer tool helps you debug your application layouts. It also provides valuable profiling information about how long it takes each View control within a layout to measure, render, and draw. Pinpoint problem areas and fix them easily.
Android App Performance Tip #9: Use the layoutopt Tool
The layoutopt tool is a simple command-line tool that can help you identify unnecessary nesting of controls and other ways to collapse your layout resources to make them as slim as possible. It will give you hints on what layout controls may be redundant or unnecessary. Fewer controls and shallower layouts perform better.
And finally… So you think your app is the best it can be? Now it’s time to put it to the test.
Android App Performance Tip #10: Use Traceview and Other Android Tools for Profiling
The Android SDK comes with a number of tools for profiling your applications. Perhaps the most popular tool is Traceview, a graphical tool that can help you debug and find performance bottlenecks in your apps. Check out some debugging tools discussed in the Android documentation.
Conclusion
There are many ways to speed up the performance of your Android applications. Some involve using specific algorithms and others rely upon tried and true debugging and performance monitoring techniques. Luckily, the Android platform comes with numerous free utilities to help track down and eradicate performance problems within your applications. You’ve got ‘em, now use ‘em! (Source: devoloper.com)