Communicating with Android devices

Who am I

  • Sr. Software Developer At RedHat
  • Georgia Native
  • Ajug Emeritus 
  • RHMAP Community Liaison (Jan 1)

About This Talk

  • How to get data to Android Devices
    • Slight bias toward stock Android APIs
  • On Demand Refresh
    • Loaders, AsyncTask, Services
  • Doing Polling Right
    • It has a bad reputation
  • FCM with RHMAP
    • If you can use FCM use FCM
  • Other Options for Real Time
    • Socket and event based

Data on Demand

  • Refresh data from a server
  • One option for older systems
  • Gives uses controls

Data on Demand

AsyncTask

  • Gets Network access off the main task
  • Good for one shot options
  • Interacts badly with the activity lifecycle

Data on Demand

Loaders

  • Gets Network access off the main thread
  • Tightly coupled with Activity lifecycle
  • More nuanced than Async Task

Data on Demand

Services

  • Gets Network access off the main Activity
  • No coupling with Activity Lifecycle
  • More exposure to Android system

Data on Demand

 

  • Cat Facts Demo
  • https://github.com/secondsun/CatFacts

Polling

  • The device will connect to a remote server on a schedule
  • Bad for battery, often unnecessary
  • Lots of cases to handle
  • Lots of libraries to help

Polling

Job Scheduler

  • Android 21 API
  • Schedules all periodic tasks based on their needs
    • Networking
    • Power
    • etc
  • Compatibility API in Firebase
    • JobDispatcher

Polling

Other Options

  • Roll your own
    • ​Be aware of battery
    • Be aware of back-off
    • Don't overload server
    • Handle retries
    • Reschedule around network/power availability
    • Won't cooperate across apps
  • Evernote

Polling

Other Options

  • Hijack AlarmManager
    • Google's engineers say just use Firebase instead
  • Hijack SyncAdapters
    • May be better if you need deeper integration with Android
    • Has great integration with Android Authentication system
    • Really complex

Polling

  • Demo - Weather Poller
  • https://github.com/secondsun/PollingDemo

Push Notifications

  • Server notifies devices when data changes
  • Asynchronous messaging via Google's services
    • Firebase Cloud Messaging
  • These messages may send up to 4k of data
  • If data is too large, use push to sync
    • Schedule a job with the JobScheduler

Push Notifications

  • If you can't use Google Play Services...
  • Amazon Kindle Devices have their own protocol
  • Requires Server support
    • Older servers may need updates
    • May need to write bridges to connect to legacy servers

Push Notifications

  • Demo!
  • Github Star Notifications

Other Options

  • For all the other ways to get data to your device

Other Options

  • Don't
  • If your app is brochure-ware just ship it
    • Cordova can help by packaging a static website into a apk

Other Options

  • [Web|BSD]Sockets
  • BSD Sockets work just like they do in Java SE
    • Might need ports open in firewalls
    • Might not work with VPS
  • WebSocket libraries vary in quality and features
    • Works over HTTP
    • Easy SSL
    • Works in browsers / cross platform
    • Netty Demo

Questions?

Communicating with Android devices

By secondsun

Communicating with Android devices

  • 861