081203 Tramm Download Background Service

Embed Size (px)

Citation preview

  • 8/14/2019 081203 Tramm Download Background Service

    1/31

    1Implementing a Download Background Service

    Implementing a

    Download Background ServiceAndroid Services

    Stefan TrammPatrick Bnzli

    Android Experience Day, FHNW

    2008-12-03

  • 8/14/2019 081203 Tramm Download Background Service

    2/31

    2Implementing a Download Background Service

    Agenda

    Part I: Business Part

    the application

    some screenshots

    Part II: Technical Part

    general introduction

    our approach

  • 8/14/2019 081203 Tramm Download Background Service

    3/31

    3Implementing a Download Background Service

    Part I: Business Part

    the mission

    customers and usage scenario

    technical background

    screenshots

  • 8/14/2019 081203 Tramm Download Background Service

    4/31

  • 8/14/2019 081203 Tramm Download Background Service

    5/31

    5Implementing a Download Background Service

    Prototype Jukebox

    Information content types:

    Invitations to phone conferencesActions: set reminder/alarm, one-click dial-in, put tocalendar

    Audio/Video podcasts

    Attachments: viewing accompanying documents (e.g. PDF)

    add personal notes

    instant feedback during event/show

    uses open data format standards

  • 8/14/2019 081203 Tramm Download Background Service

    6/31

    6Implementing a Download Background Service

    Usage scenario: enlarge your CRM-capabilities

    push newsto subscribersonto mobile phones

    provide well known end users with the right information atthe right time:

    invite to telephone conferences (or meetings)

    allow easy joining at the right time

    provide attachments (PDF, Word, Excel, Video)

    link to a specific website

    provide a podcast afterwards

    visually indicate updates and changes feedback: reading confirmation, voting

  • 8/14/2019 081203 Tramm Download Background Service

    7/31

    7Implementing a Download Background Service

    Technical background info

    Android application

    based on standard ATOM file format (RSS-reader onsteroids)

    asynchronously updating data in background

    uses internal SQLite DB and aggressive caching

    works without network connection (e.g. airplane)

  • 8/14/2019 081203 Tramm Download Background Service

    8/31

  • 8/14/2019 081203 Tramm Download Background Service

    9/31

    9Implementing a Download Background Service

    Jukebox: Detailview

  • 8/14/2019 081203 Tramm Download Background Service

    10/31

    10Implementing a Download Background Service

    Part II: Technical Part

    Android Services

    download manager

  • 8/14/2019 081203 Tramm Download Background Service

    11/31

    11Implementing a Download Background ServiceQuality Software Engineering

    Goals

    Understand reasons for background services

    Know the Android Service lifecycle

    See service IPC possibilities by theory and examples

    Understand our download manager approach

  • 8/14/2019 081203 Tramm Download Background Service

    12/31

    12Implementing a Download Background Service

    Starting Point

    multiple Activities, triggered by Intents

    each Activity needs to download many media entities

    network bandwidth scarce resource bottleneck

  • 8/14/2019 081203 Tramm Download Background Service

    13/31

    13Implementing a Download Background Service

    Managing Network Resources

    More reasons to manage network resources carefully: optimize application performance longer battery lifetime by minimizing network activity conserve data volume limits

    Boils down to two problems: asynchronous working classic resource scheduling problem

    Our approach:Uncoupling network from Activity using a queue.

  • 8/14/2019 081203 Tramm Download Background Service

    14/31

    14Implementing a Download Background Service

    Asynchronous Working Problem

    Instruments for solving this problem with a queue:

    Parallel processing to remove waiting times

    using threads and services

    Indicating finished work

    using events or notifications

  • 8/14/2019 081203 Tramm Download Background Service

    15/31

    15Implementing a Download Background Service

    Resource Access Scheduling Problem

    Instruments for solving this problem with a queue:

    Control access to the resource:

    resource allocations only over a central entity

    by limiting number of parallel connections

    Prioritize, to determine the order of access:

    using a priority queue

    Optimize resource access:

    active caching (memory and disk)

  • 8/14/2019 081203 Tramm Download Background Service

    16/31

    16Implementing a Download Background Service

    Android Application Building Blocks

    AndroidManifest.xml

    Activities

    Views

    Layouts

    Intents & IntentReceivers

    Services

    Notifications

    ContentProviders

  • 8/14/2019 081203 Tramm Download Background Service

    17/31

    17Implementing a Download Background Service

    Android Toolbox

    Android offers:

    synchronous downloading: OpenHTTPConnection

    parallel processing using threads: java.lang.Thread

    thread pools: java.util.concurrent.ThreadPoolExecutor

    communication with threads: android.os.Handler

    http request queues: android.net.http.RequestQueue

    background services: android.Service

  • 8/14/2019 081203 Tramm Download Background Service

    18/31

    18Implementing a Download Background Service

    Architecture Overview

  • 8/14/2019 081203 Tramm Download Background Service

    19/31

    19Implementing a Download Background Service

    Android Service

    Service is an Android application component running in

    background:

    class extends android.Service

    not directly interacting with the user

    started by a hosting process

    runs in the main thread of their hosting process

    uses a different address space than the hosting process

    service can be made publicly available

  • 8/14/2019 081203 Tramm Download Background Service

    20/31

    20Implementing a Download Background Service

    Services Lifecycle I

    services are started by a hosting process

    two possibilities to start a service:

    startService(): starts a service

    bindService(): starts and binds service to host life cycle

  • 8/14/2019 081203 Tramm Download Background Service

    21/31

    21Implementing a Download Background Service

    Services Lifecycle II

    Starting a service using startService()

  • 8/14/2019 081203 Tramm Download Background Service

    22/31

    22Implementing a Download Background Service

    Services Lifecycle III

    Starting a service using bindService()

  • 8/14/2019 081203 Tramm Download Background Service

    23/31

    23Implementing a Download Background Service

    Services IPC

    How to share data among multiple concurrent processes?

    Persistent:

    files (e.g. flash drive)

    databases (SQLite), Bonus: offers transactions

    Android properties

    Non-persistent:

    shared memory

    network sockets

    IDL (interface description language) the Android way: AIDL

  • 8/14/2019 081203 Tramm Download Background Service

    24/31

    24Implementing a Download Background Service

    Android Interface Description Language - AIDL

    IDL used to describe interfaces in language-neutral way

    AIDL is the Android way of doing IPC

    command line tool available

    Important AIDL properties:

    calls are synchronous

    objects are reference counted across processes

    no inter process exception forwarding

  • 8/14/2019 081203 Tramm Download Background Service

    25/31

    25Implementing a Download Background Service

    Coding Example: The Service

    Service definition

    public class MyService extends Service {

    @Override

    protected void onCreate() {

    super.onCreate();

    Log.i(getClass().getSimpleName(), Service started.");

    }

    @Override

    protected void onDestroy() {

    super.onDestroy();

    Log.i(getClass().getSimpleName(), Service stopped.");

    }

    }

    Service start

    public class SomeActivity extends Activity {

    ...

    startService(new Intent("com.example.android.apis.app.MyService"));

    ...

    }

  • 8/14/2019 081203 Tramm Download Background Service

    26/31

    26Implementing a Download Background Service

    Coding Example: AIDL I

    Interface declaration

    interface IRemoteDownloadService {

    void startDownload(in Uri uri, String type, IRemoteDownloadServiceCallback cb);

    }

    Interface implementation

    public class RemoteDownloadService extends Service {

    private final IRemoteDownloadService.Stub mBinder = new IRemoteDownloadService.Stub() {

    public void startDownload(Uri uri, String type, IRemoteDownloadServiceCallback cb) {ResourceHandler handler = new ResourceHandler(uri, type, cb);

    completionService.handleResource(handler);

    }

    };

    }

  • 8/14/2019 081203 Tramm Download Background Service

    27/31

    27Implementing a Download Background Service

    Coding Example: AIDL II

    Communicating with Service

    public class SomeClass {

    private ServiceConnection connection = new ServiceConnection() {

    public void onServiceConnected(ComponentName name, IBinder service) {

    downloader = IRemoteDownloadService.Stub.asInterface(service);

    }

    public void onServiceDisconnected(ComponentName name) {

    }

    };

    ...bindService(new Intent("ch.netcetera.REMOTE_DOWNLOAD_SERVICE"), connection,

    BIND_AUTO_CREATE);

    ...

    downloader.startDownload(uri, type, downloadCallback);

    }

  • 8/14/2019 081203 Tramm Download Background Service

    28/31

    28Implementing a Download Background Service

    Downloading Manager

  • 8/14/2019 081203 Tramm Download Background Service

    29/31

    29Implementing a Download Background Service

    Download Manager Error Handling

    We need to handle special states separately:

    telephone going to sleep

    save current downloading state persistently

    resume download on application startup

    low battery

    possibility to switch to offline mode

    low memory

    resize in memory cache

    network connection down working in offline mode

    resume downloads if network comes up again

  • 8/14/2019 081203 Tramm Download Background Service

    30/31

    30Implementing a Download Background Service

    Q & A

    Any questions?

  • 8/14/2019 081203 Tramm Download Background Service

    31/31

    31Implementing a Download Background Service

    Conclusions

    Android allows background services

    background services must follow a well definedlifecycle

    the implementation of a background data loader is

    feasible experience on real hardware and under day-to-day

    conditions is needed