{"id":465,"date":"2012-11-27T22:56:32","date_gmt":"2012-11-27T21:56:32","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=465"},"modified":"2012-11-27T22:56:32","modified_gmt":"2012-11-27T21:56:32","slug":"fast-user-friendly-and-power-efficient-network-communication-on-android","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2012\/11\/27\/fast-user-friendly-and-power-efficient-network-communication-on-android\/","title":{"rendered":"Fast, user-friendly and power-efficient network communication on Android"},"content":{"rendered":"<p>Notes from Erik Hellman\u2019s talk about &#8220;Fast, user-friendly and power-efficient network communication on Android&#8221; on the second day of DroidCon UK 2012.<\/p>\n<p>Doing network communication the right way isn&#8217;t all that hard, just keep a couple of recommendations in mind. There is no need to &#8220;re-invent the wheel&#8221;, 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.<\/p>\n<p>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:<\/p>\n<ul>\n<li>\u00a0HTTP Client handling<\/li>\n<li>Background network I\/O<\/li>\n<li>Playing server<\/li>\n<\/ul>\n<p>Generally speaking <a href=\"http:\/\/developer.android.com\/reference\/java\/net\/HttpURLConnection.html\" target=\"_blank\">HttpUrlConnection<\/a> is the best client to use, except for Eclair and Froyo, for these API versions the Apache HTTP client has fewer bugs.<br \/>\nMake sure to wrap the <a href=\"http:\/\/developer.android.com\/reference\/java\/net\/HttpURLConnection.html\" target=\"_blank\">HttpUrlConnection<\/a> streams in buffers and always exhaust the streams.<\/p>\n<p>Enable caching, it will save a lot of network traffic.<\/p>\n<p>To avoid blocking the UI thread, always run your network related tasks on a different thread using a Service with a Handler implementation.<br \/>\nIf you need to poll a URL on a regular basis, use the <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/AlarmManager.html\" target=\"_blank\">AlarmManager<\/a> to schedule requests at a set (slightly inexact) interval.<\/p>\n<p>An Android device could also serve as a server (think multiplayer gaming). This could be quite complex to create, but luckily the <a href=\"https:\/\/netty.io\/\" target=\"_blank\">Netty.io<\/a> library will probably meet most of your needs.<\/p>\n<p>And lastly, don&#8217;t use a splash-screen, there should be no need for it. Give the user as much control as possible.<\/p>\n<p>You can <a href=\"http:\/\/uk.droidcon.com\/sessions\/fast-user-friendly-and-power-efficient-network-communication-on-android\/\" target=\"_blank\">watch the full presentation on the Droidcon UK website<\/a> or <a href=\"http:\/\/developer.sonymobile.com\/downloads\/documentation\/fast-user-friendly-and-power-efficient-network-communication-on-android\/\" target=\"_blank\">read it in pdf format on Sony&#8217;s site<\/a>.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Notes from Erik Hellman\u2019s talk about &#8220;Fast, user-friendly and power-efficient network communication on Android&#8221; on the second day of DroidCon UK 2012. Doing network communication the right way isn&#8217;t all that hard, just keep a couple of recommendations in mind. &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2012\/11\/27\/fast-user-friendly-and-power-efficient-network-communication-on-android\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[114,113,118,115],"class_list":["post-465","post","type-post","status-publish","format-standard","hentry","category-android","tag-droidcon","tag-droidconuk","tag-network","tag-uk"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":5,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":470,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/465\/revisions\/470"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}