A Beginner s Guide to Android

  • Upload
    ld1080

  • View
    227

  • Download
    0

Embed Size (px)

Citation preview

  • 7/31/2019 A Beginner s Guide to Android

    1/51

  • 7/31/2019 A Beginner s Guide to Android

    2/51

    Beginner's Guide to Android

    Nick Butcher9th May 2011

    @craftyhashtags: #iobootcamp #android

  • 7/31/2019 A Beginner s Guide to Android

    3/51

    Session Feedback

    http://www.speakermeter.com/talks/guide-android/http://goo.gl/XLLgk

  • 7/31/2019 A Beginner s Guide to Android

    4/51

    Agenda

    Android platform

    Writing your app Anatomy of an app Building your app Surfacing your Content Hardware Sensors

    Quality Tools

    Publishing your app Registering for Android Market Targeting

  • 7/31/2019 A Beginner s Guide to Android

    5/51

    Many Devices, One Platform

    170+ devices in 100+ countries

    Shipping 350K+ devices per day 1 year ago was 60K per day! > 1m new potential users every 3 days!!

    Any device with Google services must passthe Compatibility Test Suite

    Contract of device capabilities & APIs Support many screen sizes/resolutions Market filters on hardware availability

  • 7/31/2019 A Beginner s Guide to Android

    6/51

    Many Devices, One Platform

    New Platform/SDKreleases

    Target latest but supportolder

    Gracefully degradefunctionality

    Source: Date collected during two weeks ending on April 1, 2011

  • 7/31/2019 A Beginner s Guide to Android

    7/51

    Writing your app

  • 7/31/2019 A Beginner s Guide to Android

    8/51

    An Android application is actually acollection of several components, eachdefined in AndroidManifest.xml

  • 7/31/2019 A Beginner s Guide to Android

    9/51

    Anatomy of an App

    Activity Service Content Provider Broadcast Receiver

    Intents Manifest

  • 7/31/2019 A Beginner s Guide to Android

    10/51

    Anatomy of an AppActivity

    A single screen with auser interface

    Independent butwork together to forma cohesive whole

    Possible to invokefrom other applications

    Extends the Activityclass

  • 7/31/2019 A Beginner s Guide to Android

    11/51

    Perform long-running operations in the background Does not provide a user interface Other components can bind/interact Extends the Service class

    Anatomy of an AppService

  • 7/31/2019 A Beginner s Guide to Android

    12/51

    Anatomy of an AppContent provider

    Manages a shared set of application data Consistent interface to retrieve/store data

    RESTful model CRUD operations

    Can be backed by different stores

    e.g. File System, SQLite DB, Web Can expose your data to other applications Can consumer data from other Content Providers

    e.g. Contacts or Call Log Extend the ContentProviderclass

  • 7/31/2019 A Beginner s Guide to Android

    13/51

    Anatomy of an AppBroadcast receiver

    Respond to system wide messages Messages can be initiated by the system or an app 2 type of broadcast:

    Normal - delivered async to all receivers Ordered - delivered in priority order & can be aborted

    Can programatically register or statically register via themanifest

    Should be very light, pass any work onto a Service Extend the BroadcastReceiverclass

  • 7/31/2019 A Beginner s Guide to Android

    14/51

    Anatomy of an AppIntents

    Intents are the messages that link app components together Explicit / implicit An abstract description of an operation to be performed

    An Action to be performed The Data to operate upon

    Extra metadata Standardise on a common vocabulary of Actions

    e.g. 'View', 'Edit', 'Send' Apps register their ability to handle Actions for a given data type

    via IntentFilter

  • 7/31/2019 A Beginner s Guide to Android

    15/51

    Anatomy of an AppIntents

    Publish an 'Intent API' Specify Action & Extras to invoke your component

  • 7/31/2019 A Beginner s Guide to Android

    16/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

  • 7/31/2019 A Beginner s Guide to Android

    17/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

    Intent intent =newIntent("com.google.zxing.client.android.SCAN");

    intent.setPackage("com.google.zxing.client.android");

    intent.putExtra("SCAN_MODE","QR_CODE_MODE");

    startActivityForResult(intent,0);

    publicvoid onActivityResult(int requestCode,int resultCode,Intent intent){

    if(resultCode == RESULT_OK){

    String contents = intent.getStringExtra("SCAN_RESULT");

    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

    }elseif(resultCode == RESULT_CANCELED){

    // Handle cancel }

    }

  • 7/31/2019 A Beginner s Guide to Android

    18/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

    Intent intent =newIntent("com.google.zxing.client.android.SCAN");

    intent.setPackage("com.google.zxing.client.android");

    intent.putExtra("SCAN_MODE","QR_CODE_MODE");

    startActivityForResult(intent, 0);

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == RESULT_OK) {

    String contents = intent.getStringExtra("SCAN_RESULT");

    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

    } else if (resultCode == RESULT_CANCELED) {

    // Handle cancel}

    }

  • 7/31/2019 A Beginner s Guide to Android

    19/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

    Intent intent = new Intent("com.google.zxing.client.android.SCAN");

    intent.setPackage("com.google.zxing.client.android");

    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

    startActivityForResult(intent,0);

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == RESULT_OK) {

    String contents = intent.getStringExtra("SCAN_RESULT");

    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

    } else if (resultCode == RESULT_CANCELED) {

    // Handle cancel}

    }

  • 7/31/2019 A Beginner s Guide to Android

    20/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

    Intent intent = new Intent("com.google.zxing.client.android.SCAN");

    intent.setPackage("com.google.zxing.client.android");

    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

    startActivityForResult(intent, 0);

    publicvoid onActivityResult(int requestCode,int resultCode,Intent intent){

    if(resultCode == RESULT_OK){

    String contents = intent.getStringExtra("SCAN_RESULT");

    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

    }elseif(resultCode == RESULT_CANCELED){

    // Handle cancel }

    }

  • 7/31/2019 A Beginner s Guide to Android

    21/51

    Anatomy of an AppIntents

    Achieve complex tasks by calling other Application'sIntents, e.g. scanning a barcode

    Intent intent = new Intent("com.google.zxing.client.android.SCAN");

    intent.setPackage("com.google.zxing.client.android");

    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

    startActivityForResult(intent, 0);

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == RESULT_OK) {

    String contents = intent.getStringExtra("SCAN_RESULT");

    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

    } else if (resultCode == RESULT_CANCELED) {

    // Handle cancel}

    }

  • 7/31/2019 A Beginner s Guide to Android

    22/51

    Anatomy of an AppManifest

    Declarecomponents of yourapp

    Declare requiredfeatures for your app

    State permissionsrequired by your app

    State platform versions

    app is compatible with

    ...

    ...

    ...

    ...

  • 7/31/2019 A Beginner s Guide to Android

    23/51

    Anatomy of an AppActivity lifecycle

    Running in a multitaskingenvironment

    Users switch apps, calls

    come in, system runs low onmemory System invokes callbacks in

    your app The system will kill your app Be sure to save state!

  • 7/31/2019 A Beginner s Guide to Android

    24/51

    Anatomy of an AppActivity lifecycle

    Activitycreated

    Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more

  • 7/31/2019 A Beginner s Guide to Android

    25/51

    Anatomy of an AppActivity lifecycle

    onCreate()

    Activitycreated

    onResume()

    Activityrunning

    Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more

  • 7/31/2019 A Beginner s Guide to Android

    26/51

    Anatomy of an AppActivity lifecycle

    onPause()

    Call comesin

    onResume()

    Activity

    running

    Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more

  • 7/31/2019 A Beginner s Guide to Android

    27/51

    Anatomy of an AppActivity lifecycle

    onPause()

    Call comesin

    onResume()

    Activity

    running

    Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more

    Return toapp

  • 7/31/2019 A Beginner s Guide to Android

    28/51

    Anatomy of an AppActivity lifecycle

    onPause()

    Call comesin

    onResume()

    Activity

    running

    Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more

    Return toapp

    LowMemory

    Activitydestroyed

  • 7/31/2019 A Beginner s Guide to Android

    29/51

    Surfacing Your Content

    Android offers uniqueways to surface yourcontent

  • 7/31/2019 A Beginner s Guide to Android

    30/51

    Surfacing Your Content

    Event drivennotifications

    Optional vibration /

    audio / LED alert Ticker text / pull downdraw with more details

    Custom expandedview

    Deep link into yourapp witha PendingIntent

    Unobtrusive notifications

  • 7/31/2019 A Beginner s Guide to Android

    31/51

    Surfacing Your Content

    Event drivennotifications

    Optional vibration /

    audio / LED alert Ticker text / pull downdraw with more details

    Custom expandedview

    Deep link into yourapp witha PendingIntent

    Unobtrusive notifications

  • 7/31/2019 A Beginner s Guide to Android

    32/51

    Surfacing Your Content

    Display your app's infoon the homescreen

    Updated regularly

    App can offer multiplewidgets Great way to keep

    user engaged withyour app

    Widgets

  • 7/31/2019 A Beginner s Guide to Android

    33/51

    Surfacing Your ContentCloud to Device Messages

    Push messages from servers to app Send 'tickles' App doesn't have to be running App has full control over how it responds

    Efficient, event driven updates

    Uses existing network channel Minimal battery impact

    Android 2.2+

  • 7/31/2019 A Beginner s Guide to Android

    34/51

    Building your App

    UI

    Resource Framework Supporting Multiple Screens Hardware Sensors Background Tasks Accessibility

    Quality Tools

  • 7/31/2019 A Beginner s Guide to Android

    35/51

    Building your AppUI

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);setContentView(R.layout.main);

    }

    UI is a hierarchy ofView & ViewGroup

    Definedprogramatically or

    in XML Layout files Platform suppliescommon palette ofwidgets

    Create your own

    widgets

  • 7/31/2019 A Beginner s Guide to Android

    36/51

    Building your AppResources framework

    Resources live in the res folder Qualifiers provide specific resources for

    different device configuration e.g. layout-land, drawable-hdpi

    Resources IDs automatically generatedin R.java

    e.g. R.layout.main

    See http://developer.android.com/guide/topics/resources/index.html for more

  • 7/31/2019 A Beginner s Guide to Android

    37/51

    Building your AppSupporting multiple screens

  • 7/31/2019 A Beginner s Guide to Android

    38/51

    Building your AppSupporting multiple screens

  • 7/31/2019 A Beginner s Guide to Android

    39/51

    Building your AppSupporting multiple screens

    Use RelativeLayout Size with wrap_content, match_parent, weight Use dp units Think web style layout

    Provide density specific assets

    e.g. drawable-hdpi Provide size specific layouts e.g. layout-xlarge

    Use 9-patch drawables

  • 7/31/2019 A Beginner s Guide to Android

    40/51

    Building your App

    Many devices have access to a variety of sensors Accelerometer, Gravity, Gyroscope, Light, Magnetic Field

    Use SensorManager.getSensorList() to find out what is available SensorManager.registerListener() for sensor updates

    Hardware Sensors

    See http://developer.android.com/reference/android/hardware/Sensor.html for more

  • 7/31/2019 A Beginner s Guide to Android

    41/51

    Building your AppBackground tasks

    More than just multitasking Use background tasks to provide fresh information Use the AlarmManagerto schedule updates Use 'inexact repeating' to minimise power consumption

    Regularly sync content from the web

    Provide offline availability Great for regularly updated content e.g. news feeds

  • 7/31/2019 A Beginner s Guide to Android

    42/51

    Building your AppAccessibility

    Label controls and input widgets with android:contentDescription tag

    allows built in accessibility to work Allow navigation with trackball / d-pad where available Follow Android UI Best Practices

    http://developer.android.com/guide/practices/ui_guidelines/index.html

    Back button should always move the user back one logical

    step Try it in accessible mode!

    Enable TalkBack in Settings -> Accessibility or downloadfrom Market

    See http://developer.android.com/guide/practices/design/accessibility.html for more

  • 7/31/2019 A Beginner s Guide to Android

    43/51

    Building your AppQuality

    Work with the Activitylifecycle Offload any long running

    operation to abackground thread

    Stability, UIResponsiveness,Performance

    Listen to your users,read their feedback

    regularly Work with a designer

  • 7/31/2019 A Beginner s Guide to Android

    44/51

    Tools

    1. Download and Install the Android SDK

    developer.android.com/sdk/index.html2. Download and Install Eclipse

    http://eclipse.org3. Install the Android Development Tools (ADT) Eclipse Plugin

    http://developer.android.com/sdk/eclipse-adt.html#installing

    4. Use SDK & AVD Manager in Eclipse to install the latestAndroid packages.

    Full guide: http://developer.android.com/sdk/installing.html

  • 7/31/2019 A Beginner s Guide to Android

    45/51

    Publishing Your App

  • 7/31/2019 A Beginner s Guide to Android

    46/51

    Register For

    Create a developer profile Register at http://market.android.com/publish

    One time $25 fee

    No limit to number of apps you can publish Developer Distribution Agreement To sell apps, create a Checkout Merchant Account

  • 7/31/2019 A Beginner s Guide to Android

    47/51

    Upload Your App

    No review process

    Publish as many updates as you like Updates available right away Supply promotional assets & video Max APK size 50MB Accessible on device & on Android Web Market

    View crash reports online View application statistics

  • 7/31/2019 A Beginner s Guide to Android

    48/51

    Targeting

    Only target specific countries

    Target specific hardware e.g. Telephony or OpenGL ES 2.0

    Android Market will not show app to incompatible device

    // in AndroidManifest.xml

  • 7/31/2019 A Beginner s Guide to Android

    49/51

    Recap

    Android platform

    Writing your app Anatomy of an app Building your app Surfacing your Content Hardware Sensors

    Quality Tools

    Publishing your app Registering for Android Market Targeting

  • 7/31/2019 A Beginner s Guide to Android

    50/51

    Session Feedback

    http://www.speakermeter.com/talks/guide-android/http://goo.gl/XLLgk

  • 7/31/2019 A Beginner s Guide to Android

    51/51