10 Tips for Efficient Android App Development

If you want to be the worst case of failure on Google Play, the best secret is that the interface is extremely slow, power-hungry, and memory-hungry. Then you will get negative comments from users, and finally your reputation will stink. It doesn't matter if your app is well designed and creative.

  Every issue that affects product efficiency, such as power consumption or memory usage, affects the success of the app. That's why it's crucial to ensure that development is optimized, runs smoothly, and doesn't crash Android. There's no need to discuss efficient programming here, because we don't care whether the code you write will stand up to testing. Even efficient code takes time to run. In this article today, we will talk about how to shorten the running time as much as possible and how to develop apps that users like.

 

  Efficient use of threads

  Suggestion 1: How to cancel actions in some threads 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;

  Suggestion 2: How to keep the response from 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.

  Suggestion 3: How to initialize query operations in threads

  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.

  After using this object, your App will initialize a separate background thread for the ContentProvider to query, and when the query ends, it will return the result to the Activity that called the query.

  Recommendation 4: Other aspects to pay attention to

  • Use StrictMode to check for potentially time-consuming operations in the UI thread;
  • Use special tools like Systrace or Traceview to find bottlenecks in your application;
  • Use a progress bar to show the user the progress of the operation;
  • If the initialization operation is time-consuming, show a welcome screen.

  Optimize your device's battery life

  If your app is power hungry, don't blame the user for uninstalling your app. For battery usage, the main electricity consumption is as follows:

  • Wake up the program frequently when updating data;
  • Use EDGE or 3G to transmit data;
  • Text data conversion for non-JIT regular expression operations.

  Recommendation 5: How to optimize the network

  • If there is no network connection, please let your application skip network operations; only update data when there is a network connection and no roaming;
  • Select a compatible data format, and convert all requests containing text data and binary data into binary data format requests;
  • Use efficient conversion tools, consider using streaming conversion tools more, and use less tree-shaped conversion tools;
  • For a faster user experience, please reduce repeated access to the server;
  • If you can, use the framework's GZIP library to compress text data for efficient use of CPU resources.

  Suggestion 6: How to optimize the work of the application in the front end

  • If you consider using wakelocks, try to set it to the smallest level;
  • In order to prevent power consumption caused by potential bugs, please specify the timeout period explicitly;
  • Enable android:keepScreenOn attribute;
  • In addition to the system GC operation, consider manually recycling Java objects, such as XmlPullParserFactory and BitmapFactory. There are also regular expression Matcher.reset(newString) operations, StringBuilder.setLength(0) operations;
  • Be aware of synchronization issues, although it is safe to do so in the main thread;
  • 在Listview中要多采用重复利用策略;
  • 如果允许的话多使用粗略的网络定位而不用GPS,对比一下GPS需要1mAh(25s * 140 mA),而一般网络只用0.1mAh(2s * 180mA);
  • 确保注销GPS的位置更新操作,因为这个更新操作在onPause()中也是会继续的。当所有的应用都注销了这个操作,用户可以在系统设置中重新启用GPS而不浪费电量;
  • 请考虑在大量数理运算中使用低精度变量并在用DisplayMetrics进行DPI任务时缓存变量值;

  建议七:怎么优化工作在前台的应用

  • 请确保service生命周期都是短暂的,因为每个进程都需要2MB的内存,而在前台程序需要内存时也会重新启动;
  • 保持内存的使用量不要太大;
  • 如果要应用每30分钟更新一次,请在设备处于唤醒状态下进行;
  • Service在pull或者sleep状态都是不好的,这就是为什么在服务结束时要使用AlarmManager或者配置属性stopSelf()的原因。

  建议八:其它注意事项

  • 在进行整体更新之前检查电池的状态和网络状态,等待最好的状态在进行大幅度装换操作;
  • 让用户看到用电情况,比如更新周期,后台操作的时候;

  实现低内存占用UI

  建议九:怎么找到布局显示问题

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

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

  建议十:如何解决问题

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

  做到更加完美并不断优化

  尽管以上的每条建议看起来都是很小的改进,但是如果它能成为你日常代码的一部分,那么你就会看到意想不到的结果。要让Google Play看到更多杰出的、流畅的、更快速、更省电的应用,向Android走向完美的目标迈进一步。

 

Guess you like

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