Fast, user-friendly and power-efficient network communication on Android

Notes from Erik Hellman’s talk about “Fast, user-friendly and power-efficient network communication on Android” on the second day of DroidCon UK 2012.

Doing network communication the right way isn’t all that hard, just keep a couple of recommendations in mind. There is no need to “re-invent the wheel”, other fields have struggled with this problem before, we can learn from them. There are also a lot of open source projects that tackle various network related problems, quite often one of those will meet your requirements.

That being said, handling the network on a mobile phone can be difficult. The connection can drop out of nowhere, the quality can change or you might not even be online at all. There are a couple of things that developers can do to make the experience better:

  •  HTTP Client handling
  • Background network I/O
  • Playing server

Generally speaking HttpUrlConnection is the best client to use, except for Eclair and Froyo, for these API versions the Apache HTTP client has fewer bugs.
Make sure to wrap the HttpUrlConnection streams in buffers and always exhaust the streams.

Enable caching, it will save a lot of network traffic.

To avoid blocking the UI thread, always run your network related tasks on a different thread using a Service with a Handler implementation.
If you need to poll a URL on a regular basis, use the AlarmManager to schedule requests at a set (slightly inexact) interval.

An Android device could also serve as a server (think multiplayer gaming). This could be quite complex to create, but luckily the library will probably meet most of your needs.

And lastly, don’t use a splash-screen, there should be no need for it. Give the user as much control as possible.

You can watch the full presentation on the Droidcon UK website or read it in pdf format on Sony’s site.

Device Fragmentation by Robin Puthli

Notes from Robin Puthli’s talk about Device Fragmentation among Android devices on the 1st day of DroidCon UK 2012.

Device fragmentation starts to become a big part of Android development as there are more and more Android devices available. As a result there are a lot of different resolutions and screen sizes that need to be taken into account. has a very interesting report on their blog that illustrates this fragmentation.

Luckily (device) fragmentation is a very old problem. It had to be solved on operation systems (Window, Mac, Linux, etc), browsers (Chrome, FireFox, Internet Explorer, etc), Java (ME, SE, FX, etc) and more before.

There are roughly nine strategies you can pick from to deal with device fragmentation.
Multi-manual: Phone vs tablet, writing separate code for each.
Derive multi: QT or J2ME Polish, Mono Touch, writing pseudo code and then letting the framework generate the rest.
One (size) fits all: HTML5, PhoneGap. This approach is based on coding for the lowest common denominator.
Single adapt
: Writing adaptive layouts, using 9-patches, fragments.

“There is no silver bullet. You have to pick and mix [from the available solutions].”

The full presentation can be found at Itude Mobile’s blog.

OpenCV for Android by Erik Hellman

Notes from Erik Hellman’s talk about OpenCV for Android on the 1st day of DroidCon UK 2012.

Open source Computer Vision library, containing loads of modules and image (related) algorithms.

The code is very well documented, but most samples are in C++.

A couple of examples of what you can do with OpenCV are:

  • Object detection, i.e. face detection.
  • Salient feature detection, i.e. hand or a door frame.
  • Motion tracking.

The talk mainly covered demonstrations of what you can do with OpenCV, rather than going into more detail.

Library: Download OpenCV (for Android)
Documentation:  OpenCV API Reference

AllJoyn by Mitch Williams

Notes from Mitch Williams’ talk about AllJoyn on the 1st day of DroidCon UK 2012.

“AllJoyn™ is an open-source application development framework that enables ad hoc, proximity-based device-to-device communication that is OS agnostic.”

There are other frameworks like DLNA, UPnP and Bonjour as well. DLNA would be a good choice if you only wanted to do one-to-one device communication. UPnP and Bonjour are missing a lot of the higher level communication compared to AllJoyn.

AllJoyn is multi-platform as a result of the native implementation being written in C++. The currently supported platforms are:

  • Android 2.1 and above
  • iOS
  • Window XP, 7 and 8
  • Linux
  • OS X

Source code: AllJoyn @ Github.
Documentation: AllJoyn Java docs.

Introduction to Polaris by Cyril Mottier

Notes from Cyril Mottier’s talk about Polaris on the 1st day of DroidCon UK 2012.

Polaris is a mapping library for Android. It was created because with the current Google Maps API it is painful to even do simple stuff.

Some features of Polaris include:

Gesture support

  • Simple tap (opens a call out)
  • Double tap (zoom in or zoom to the maximum zoom level for the location)
  • Long press

(Map) Annotations with a “variable anchor” (the arrow/pointer of the annotation is dynamically placed based on the location’s position on screen).

Demo: Polaris Sample @ Google Play Store.
Source code: Polaris @ Github.

NOTE: Polaris may become obsolete as Google is working on a new Maps API for Android.

DroidCon UK – day 2

Another quick summary of the talks of the day.

9.45 The Fragment transition
Who: Corey Leigh Latislaw
What: Introduction on using Fragments.
Summary: Start using Fragments now. There is no need to convert a whole app at once, but you can start using Fragments with the new Activities you are building.

10.30 Writing games for an Android console.
Who: All Sutton – Ouya
What: Some considerations to keep in mind when developing for a console.
Summary: Developing for a console is wildly different from mobile.

11.30 Android reverse engineering
Who: David Tellebaum – apkudo
What: Reverse engineering Android applications.
Summary: Hacking Zynga’s “Words With Friends” using (bak)smali and ViewServer.

12.15 Interfacing hardware with Android and Arduino
Who: Fei Manheche – Robobo
What: Connecting Android apps to Arduino hardware.
Summary: A brief introduction on how to make Android and Arduino talk to each other.

14.00 Memory Analyzer
Who: Felipe Ferraz – CESAR
What: Memory analyzing to resolve bugs and optimize performance.
Summary: Using a timeline of memory dumps and the MAT Eclipse plugin to resolve bugs and optimize performance.

14.45 Deep dive into Android custom components
Who: Chui-Ki Chan – Monkey Write
What: Creating your own custom components.
Summary: Easily create your own custom components by either encapsulating or extending existing widgets. Slides:

15.45 Optimized network communication
Who: Erik Hellman – Sony Mobile
What: How to optimize your networking logic to provide user-friendly and power-efficient experiences.
Summary: A set of tips and best practices to avoid annoying the user and not drain the battery with networking activities.

16.30 Dynamic Animations
Who: Anders Ericsson –
What: Dynamic animations.
Summary: By extending a View, adding a custom class to calculate position (using spring and damper physics) and using a Runnable you can quickly add animations to any View.

17.15 AndroVM
Who: Daniel Fages – Genymobile
What: An Android VM that runs on VirtualBox.
Summary: An explanation about how AndroVM is built followed by a demo.

18.00 Developing accessible applications
Who: Gary Readfern-Gray – RNIB
What: Developing accessible applications for Android.
Summary: It takes very little effort to add basic accessibility optimizations by using content descriptions for widgets and making the app navigable by keyboard (or at least D-pad).

DroidCon UK – day 1

A quick summary of the talks of the day.

10.05 AllJoyn
Who: Mitch Williams – Qualcomm
What: AllJoyn P2P framework
Summary: An ad-hoc and proximity based p2p framework.

10.40 Device fragmentation
Who: Robin Puthli – Itude Mobile
What: Mobile device fragmentation
Summary: Fragmentation is an old problem. “There is no silver bullet. You have to pick and mix [from the available solutions].”

11.15 Unify VoIP / MSG Core
Who: Martyn Davis – Voxygen
What: Unify VoIP / MSG Core
Summary: A paid for VoIP library written in C, using JSON messaging. Available for Android and iOS.

11.50 Polaris
Who: Cyril Mottier – Prixing
What: An introduction to Polaris
Summary: A (Google) maps library for Android. Adding a lot of useful features.

13.25 Gradle
Who: Lukas Jarosh – And labs ?
What: Gradle
Summary: Gradle, a less verbose Maven alternative.

14.30 OpenCV
Who: Eric – Sony
What: OpenCV library
Summary: Open source Computer Vision library, containing loads of modules and image (related) algorithms.