Optimizing Android App performance? Ten tips you must know!

Regardless of the continuous emergence of hammer or eggplant mobile phones, the mobile phone market share of the Android system is still the largest at present, so the number of apps developed based on Android is also very large. So, how to develop a higher performance Android App ? It is believed to be a major headache for software development companies and programmers. Today, I will give you a few tips to improve the performance of your Android app.

 

Efficient use of threads

1. Cancel some actions in the thread in the background

We know that all operations during the running of the App are performed in the main thread (UI thread) by default, so the response speed of the App will be affected. It will cause the program to freeze, die or even system errors.

To speed up the response, time-consuming operations (such as network requests, database operations, or complex calculations) need to be moved from the main thread to a separate thread. The most efficient way to do this is at the class level, using AsyncTask or IntentService to create background operations. If you choose to use an IntentService, it will be started when needed, and then a worker thread will handle the request (Intent).

Note the following limitations when using IntentService:

  • This class should not pass information to the UI. If you want to display the processing result information to the user, please use Activity;

  • Only one request can be processed at a time;

  • Each processing request process cannot be interrupted;


 

2. Keep the response without ANR

This way of removing time-consuming operations from the UI thread also prevents the system not responding (ANR) dialog box for user operations. All you need to do is subclass AsyncTask to create a background worker thread and implement the doInBackground() method.

Another way is to create a Thread class or HandlerThread class by yourself. Note that this will also make the app slower, because the default thread priority is the same as the main thread's priority unless you explicitly set the thread's priority.

3. Initialize the query operation in the thread

Displaying data isn't instant when the query is in the background, but you can speed it up by using the CursorLoader object, which keeps the interaction between the Activity and the user unaffected.

使用这个对象后,你的App会为ContentProvider初始化一个独立的后台线程进行查询,当查询结束后就会给调用查询的Activity返回结果。

4.其它需要注意的方面

  • 使用StrictMode来检查UI线程中可能潜在的费时操作;

  • 使用一些特殊的工具如Safe.ijiami、Systrace或者Traceview来寻找在你的应用中的瓶颈;

  • 用进度条向用户展示操作进度;

  • 如果初始化操作很费时,请展示一个欢迎界面。

 

优化设备的电池寿命

如果应用很费电,请不要责怪用户卸载了你的应用。对于电池使用来说,主要费电情况如下:

  • 更新数据时经常唤醒程序;

  • 用EDGE或者3G来传递数据;

  • 文本数据转换,进行非JIT正则表达式操作。


 

5.优化网络

  • 如果没有网络连接,请让你的应用跳过网络操作;只在有网络连接并且无漫游的情况下更新数据;

  • 选择兼容的数据格式,把含有文本数据和二进制数据的请求全部转化成二进制数据格式请求;

  • 使用高效的转换工具,多考虑使用流式转换工具,少用树形的转换工具;

  • 为了更快的用户体验,请减少重复访问服务器的操作;

  • 如果可以的话,请使用framework的GZIP库来压缩文本数据以高效使用CPU资源。


 

6.优化应用在前端的工作

  • 如果考虑使用wakelocks,尽量设置为最小的级别;

  • 为了防止潜在的bug导致的电量消耗,请明确指定超时时间;

  • 启用 android:keepScreenOn属性;

  • 除了系统的GC操作,多考虑手动回收Java对象,比如XmlPullParserFactory和BitmapFactory。还有正则表达式的Matcher.reset(newString)操作、StringBuilder.setLength(0)操作;

  • 要注意同步的问题,尽管在主线程中是安全的;

  • 在Listview中要多采用重复利用策略;

  • 如果允许的话多使用粗略的网络定位而不用GPS,对比一下GPS需要1mAh(25s * 140 mA),而一般网络只用0.1mAh(2s * 180mA);

  • 确保注销GPS的位置更新操作,因为这个更新操作在onPause()中也是会继续的。当所有的应用都注销了这个操作,用户可以在系统设置中重新启用GPS而不浪费电量;

  • 请考虑在大量数理运算中使用低精度变量并在用DisplayMetrics进行DPI任务时缓存变量值;


 

7.优化工作在前台的应用

  • 请确保service生命周期都是短暂的,因为每个进程都需要2MB的内存,而在前台程序需要内存时也会重新启动;

  • 保持内存的使用量不要太大;

  • 如果要应用每30分钟更新一次,请在设备处于唤醒状态下进行;

  • Service在pull或者sleep状态都是不好的,这就是为什么在服务结束时要使用AlarmManager或者配置属性stopSelf()的原因。


 

8.其它注意事项

  • 在进行整体更新之前检查电池的状态和网络状态,等待最好的状态在进行大幅度装换操作;

  • 让用户看到用电情况,比如更新周期,后台操作的时候;


 

实现低内存占用UI

9.找到布局显示问题

当我们为布局单独创建UI的时候,就是在创建滥用内存的App,它在UI中会出现可恶的延时。要实现一个流畅的、低内存占用的UI,第一步就是搜索 你的应用找出潜在的瓶颈布局。使用Android SDK/tools/中自带的Hierarchy Viewer Tool工具。

还有一个很好的工具就是Lint,它会扫描应用的源码去寻找可能存在的bug,并为控件结果进行优化。

10.解决问题

如果布局显示结果发现了问题,你可以考虑简化布局结构。可以把LinearLayout类型转化成RelativeLayout类型,降低布局的层级结构。

追求更加完美并不断优化

以上的每个小技巧,希望它能成为你日常代码的一部分,然后你就会看到意想不到的结果。要让Google Play看到更多杰出的、流畅的、更快速、更省电的应用,向Android走向完美的目标迈进一步。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326732974&siteId=291194637