kinvey how to android v2

Upload: fb

Post on 02-Mar-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Kinvey How to Android v2

    1/29

    Ho t Build an A:ANDROID EDITION

    http://www.kinvey.com/
  • 7/26/2019 Kinvey How to Android v2

    2/29

    Table of ContentHow to Build an App: Android Edition

    Whats All the Excitement About?

    Step 1: Decide What New Thing You Want to Do

    Step 2: Define Your MVP

    Step 3: Design Your App

    Step 4: Set up Your Android Development Environment

    Step 5: Developing Your App

    1

    3

    5

    8

    12

    16

    i

  • 7/26/2019 Kinvey How to Android v2

    3/29

    Whats All the Excitement About?

    Its never been easier or quicker to build

    and distribute an application to millions

    of devices and get paid, especially when

    you tap into Androids market leading

    features.

    We are living in the Post-PC era. That

    sentence in quotes comes up over

    14,000 times in a Google search. So what

    are so many people writing about? They

    are writing about a time when many

    computing tasks such as staying in

    touch with friends, doing online

    research, shopping, and playing games

    no longer require a laptop or desktop

    computer. Not only can these activities

    now be done on the go with a smart-phone or tablet, but they can also be

    done in new ways that enrich the

    experience.

    Android devices are a great example.

    Many are location-aware; many have

    accelerometers that know when the

    phone is moving, in which direction and

    how fast; and many include NFC (Near

    Field Communications), a technology

    that enables easy communications (such

    as by tapping) between two devices or

    between a device and a passive tag.

    1

    HOW TO BUILD AN APP: ANDROID EDITION

    NFC is useful for actions like automated

    store checkout, inventory counting,

    contact information exchange, and

    offering special deals to customers in a

    specific store aisle.

    Android also offers features not found on

    other mobile platforms like iOS or

    Windows Phone that make development

    easier. Two good examples are theAccountManager system and the

    AccountAuthenticatorActivity base class,

    both of which streamline the process of

    setting up custom accounts such as to

    register and authenticate user IDs and

    passwords. Android also offers market-

    leading capabilities for letting applica-

    tions on the same device invoke each

    others services. That way a developer is

    spared the work of implementing and

    replicating an already-existing service.

    Besides its technical advantages, Android

    also offers developers major business

    opportunities.

  • 7/26/2019 Kinvey How to Android v2

    4/29

    2

    HOW TO BUILD AN APP: ANDROID EDITION

    According to Google, there were over 1

    million apps available on Google Play as

    of July 1013, with over 50 Billion

    downloads. According to IDC, Android

    accounted for 79.3% of worldwide

    smartphone shipments in Q2 of 2013

    in a market that did not exist until 2008.

    Two recent developments in particular

    have sparked this growth: the Android

    ecosystem and the Cloud. The ecosys-

    tem consists of resources like Android

    Development Tools (to be discussed

    later) and distribution platforms (like

    Google Play) that accelerate app

    development and marketing. These

    platforms remove the friction involved

    in distributing and selling traditional

    desktop applications. Theres no physical

    packaging, no inventory stocking, and

    much less waiting for developers to get

    paid. You simply register online, pay a

    nominal fee, upload the app and youre

    in business.

    The Cloud is a general term meaning a

    service that users and applications share

    via the Internet. A cloud may be private

    i.e., the services belong to an

    organization for the benefit of its

    employees and business partners. Or a

    cloud may be public i.e., a company

    (such as Amazon or Kinvey) owns the

    services, which it offers commercially to

    the public, including app developers. In

    the Android space, Cloud services

    typically function as an apps backend.

    They may do computational tasks

    considered too heavy for a phone.

    They might also store, secure and share

    data among multiple users on different

    phones. A good example is a game app

    where users need to access the current

    state of play.

    If you are a developer and want to build

    your own backend, you certainly can do

    that. Whether you want to or not

    probably depends on whether your

    backend will set your app apart in the

    eyes of your potential customer. If its a

    service thats fairly typical across lots of

    apps like user authentication or a

    shopping cart then the answer is

    probably no. You may be better off

    hooking into a commercial backend

    provider via an API.

    Then you can focus on what counts

    Android accounted for 79.3%

    of worldwide smartphone

    shipments in Q2 of 2013

  • 7/26/2019 Kinvey How to Android v2

    5/29

    3

    DECIDE WHAT NEW THINGYOU WANT TO DO

    Ste 1

  • 7/26/2019 Kinvey How to Android v2

    6/29

    4

    HOW TO BUILD AN APP: ANDROID EDITION

    most a unique user experience and

    application-specific functionality. That

    brings us to the first step in making an

    app.

    Why do people buy apps? Why do they

    make them? The answer to both

    questions is that a successful app

    improves users lives in new ways

    typically by taking advantage of features

    like location awareness and accelerom-

    eters that are present in phones. A great

    example app is Google Now, which

    Google describes as follows:

    Google Now gets you just the right

    information at just the right time.

    It tells you todays weather before you

    start your day, how much traffic to

    expect before you leave for work, when

    the next train will arrive as youre

    standing on the platform, or your

    favorite team's score while theyre

    playing. And the best part? All of this

    happens automatically. Cards appear

    throughout the day at the moment you

    need them.

    The Google Maps API enables location

    awareness so apps can tailor the user

    experience based on location such as

    by recommending stores or restaurants

    in that area. As previously noted, NFC is

    a feature that lets users easily exchange

    data, such as photos or contact

    information, with another NFC phone

    just by tapping the two phones together.

    Other app opportunities involve

    information sharing. A prime example of

    that is Google Docs that lets different

    users on different devices work on the

    same document (in the Cloud) coopera-

    tively. A similar opportunity is context

    sharing such as when the user starts a

    movie on a smartphone, stops the

    movie, and later picks up the action at

    the very same point on a TV, PC, tablet,

    or another smartphone.

    The key, again, is identifying a function

    that improves peoples lives. That

    requires, first, identifying the value to be

    provided, second, conceptualizing a core

    function that provides that value and,

    third, researching the market to make

    sure there is nothing else out there that

    already does what your app will do. The

    research part is easy since you can

    probably find out if a similar app already

    exists just by spending a few minutes on

    Google Play.

    Does your app meet all three of these

    tests? Good, then lets proceed to the

    next step.

    http://www.google.com/landing/now/http://www.google.com/landing/now/http://www.google.com/landing/now/http://www.google.com/landing/now/
  • 7/26/2019 Kinvey How to Android v2

    7/29

    5

    DEFINE YOUR MVPSte 2

  • 7/26/2019 Kinvey How to Android v2

    8/29

    6

    HOW TO BUILD AN APP: ANDROID EDITION

    Most unsuccessful apps fail for any of

    three reasons:

    1. the app was a solution in

    search of a problem (i.e., no real need

    existed)

    2. the implementation was poor

    3. the app tried to do too much

    Keeping the app as simple as possible

    (especially in version 1) is the best way to

    avoid all three pitfalls. Most successful

    apps (indeed, most successful consumer

    tech products) are based on what is

    called an MVP, or minimum viable

    product. Its the product with the

    smallest feature set that can deliver the

    core function without any distractingbells and whistles. You know you have

    an MVP if the product would no longer

    function if you removed any single

    feature.

    The best example is the original Google

    Search. Whereas todays Google Search

    offers more than 20 special features

    beyond the original word search

    capability (e.g., synonyms, weather

    forecasts, stock quotes, maps, transla-

    tion) the original product was simply text

    search where the user types into a

    search box at the top of an otherwise

    all-but-empty web page and gets back a

    page full of text search results.

    It was enough to prove the convenience

    and accuracy of using Google.

    The same MVP approach drives the app

    market today. With each app focused on

    meeting a specific need, everyones

    needs are as different as the collection

    of apps on their various devices.

    Focusing on a specific function also

    makes for a less cluttered app one

    that is easier for the customer to

    understand and for the developer to

    build. As a result, an MVP will probably

    perform better technically and its value

    will be easier for both the developer and

    buyer to discern.

    So, in drawing up your MVP feature list,

    ask yourself what features are key to the

    experience. Possible must-haves include:

    Offline capability (i.e., ability to work

    when not connected to the Internet)

  • 7/26/2019 Kinvey How to Android v2

    9/29

    7

    HOW TO BUILD AN APP: ANDROID EDITION

    Location aware (i.e., will the app use

    GPS?)

    NFC (i.e., can devices share data by

    touching?)

    Push notification (i.e., sending

    updated information to devices as

    soon as it is available)

    Draw up your feature lists knowing

    which features are necessary and which

    are there primarily to enhance the user

    experience. Push notifications, for

    example, are not key to Instagram but

    the app has them anyway to encourage

    user activity. Features that are there

    solely to enhance the user experience

    may have to wait for version 2. (The first

    iPod only came in only one color andcould just hold 1000 songs.)

    Once you have your feature list, the next

    step is design to decide where and

    how to implement the features on the

    device, in the cloud, or in some combina-

    tion. Most app design decisions fall into

    two broad categories: user experience

    and shared information. Which takes us

    to the next step.

    MVP Takeaway MVP = Minimal Viable

    Product

    Its the product with the

    smallest feature set that

    can deliver the core func-

    tion

    You have an MVP if the

    product would no longer

    function if you removed

    any single feature

    An MVP will probably

    perform better technically

    and its value will be easier

    for both the developer and

    buyer to discern

  • 7/26/2019 Kinvey How to Android v2

    10/29

    8

    DESIGN YOUR APPSte 3

  • 7/26/2019 Kinvey How to Android v2

    11/29

    9

    HOW TO BUILD AN APP: ANDROID EDITION

    User experience (UX) decisions

    determine the apps look and feel. They

    answer questions such as: What does

    the app look like? What screens does it

    have? In the app world, onscreen objects

    like buttons, sliders, and fill-in boxes are

    called widgets. So you need to decide

    which types of widgets will reside on

    which screens. What actions will occur as

    a result of the user interacting with

    those widgets?

    Keep in mind that users will expect to

    interact differently with a mobile device

    than they do with a PC because the

    screens on mobile devices are much

    smaller. Theyll expect to use their

    fingers instead of a mouse or a trackpad.Ideally, your app can even be used with

    one hand holding the device while using

    just a thumb for scrolling and working

    the apps other controls the Path app

    is a good example, and increasingly so is

    Facebook.

    You will also need to decide which parts

    (if any) of your application to write in

    HTML5 and which to write in Java, the

    primary programming language for

    Android devices. For reasons of speed

    and programming efficiency, many apps

    (like Facebook) are designed a little like

    1960s-era TV sets where a small window

    of frequently updated content

    (the HTML5 part) sits within an applica-

    tion wrapper (the Java part) that

    implements less dynamic content such

    as the apps widgets. Which part of the

    app is HTML5 and which is Java is not

    obvious to the untrained eye (there may

    be no browser address bar, for example)

    but implementing the app this way

    enables much faster content refresh (via

    the web) and more response widgets

    (via Java). HTML5s write once, deploy

    anywhere model is also another

    advantage. Parts of the app written in

    HTML5 can be deployed across iPhones,

    iPads and Android devices without

    rewrite.

    In addition to deciding what happens onthe frontend, you also have to decide

    what happens on the backend

    specifically, what data will users share?

    For example, will users want to broad-

    cast their GPS locations to other users

    in real time (such as to enhance a

    gaming experience)? Will the app share

    or store movie or restaurant preferences

    or purchase histories with backend

    recommendation engines? If so, these

    functions will most likely call the APIs

    of backend service providers you

    wont actually have to write those

    functions yourself.

  • 7/26/2019 Kinvey How to Android v2

    12/29

    10

    HOW TO BUILD AN APP: ANDROID EDITION

    For the time being, then, set those

    backend functions aside and focus on

    the front end. Just like you want to build

    a product with the minimal viable

    number of features, you may also wish

    to build your first prototype using

    dummy data thats static rather than

    shared. Its much easier to fine-tune the

    frontend if you dont have to simultane-

    ously modify your backend too. Once

    you get the apps look-and-feel right,

    then make those backend connections.

    Why Android?

    Besides the features and design of your

    first app, theres one other decision you

    need to make before you start actual

    development. Thats whether to deploy

    on Apples iOS platform or on Googles

    Android platform. Some of the reasons

    to develop for Android include:

    1. Cross-Compatibility

    There are well over 100 million Android

    devices in use today, representing

    hundreds of different models from

    dozens of different manufacturers.

    Companies as varied as Samsung,

    Motorola, LG, HTC, etc. make devices

    that will run Android apps and these

    devices come in many different screen

    sizes and resolutions. Android-capable

    devices also come in different configura-

    tions of hardware features, such as

    camera, accelerometer, GPS, and

    Bluetooth yet they all run Android

    and all are available to you as potential

    targets for your Android app.

    WHYANDROID?

    Cross-Compatibility

    OpenPlatform

    MarketGrowth

    FreeTools

    InternationalGrowth

  • 7/26/2019 Kinvey How to Android v2

    13/29

    11

    HOW TO BUILD AN APP: ANDROID EDITION

    WHYNotANDROID?

    Customers lesswilling to pay

    High diversityof devices

    Not in theApple ecosystem

    2. Open Platform

    Androids cross-compatibility stems from

    the fact that it is an open platform,

    meaning that any company can

    download the free source code and

    build products based on that code. You

    can view and modify the code as you

    wish to create new features, or to

    handle an existing feature in a new way.

    3. Market Growth

    As noted earlier, the Android market is

    huge and growing fast thanks to its

    openness and compatibility across so

    many devices and device makers.

    Androids openness and market growth

    also make for a highly innovative

    environment where the next big thingis likely to happen. Thats a very

    attractive proposition for developers.

    4. International Growth

    In particular, Android overwhelmingly

    dominates markets outside the U.S.

    According to China Daily, Android

    accounted for 66% of the Chinese

    smartphone market in the first half of

    2013.

    5. Free Tools

    All the tools you need to develop rich

    Android apps are free of charge. Step 4

    is about installing those tools and setting

    up your Android development environ-

    ment.

    Why Not Android?

    Although Android offers many compel-ling advantages as a platform for your

    mobile app, it also has some disadvan-

    tages. Here are three:

  • 7/26/2019 Kinvey How to Android v2

    14/29

    12

    HOW TO BUILD AN APP: ANDROID EDITION

    1. Customers less willing to pay

    Perhaps it's because of Android's open

    source heritage, but whatever the

    reason, Android users pay less for apps.

    The average selling price for an Android

    app was just 6 cents in April 2013 versus

    19 cents for iPhone apps and 50 cents

    for iPad app -- according to data

    furnished by Flurry Analytics, Google

    Play and the Apple App Store.

    2. High diversity of devices

    Building an app that runs successfully on

    so many different screen sizes and

    resolutions continues to be a challenge

    despite the fact that Android has

    provided innovations such as Fragmentsand GridLayout. Android introduced

    Fragments in Android 3.0 (API level 11),

    primarily to support more dynamic and

    flexible user interface designs on large

    screens, such as tablets. A Fragment is a

    Java class that enables tablets to display

    side-by-side on one screen content that

    on a phone could not be displayed all at

    once on the same screen and so

    would be displayed on multiple screens.

    GridLayout is a class that places its

    children in a rectangular grid composed

    of a set of infinitely thin lines that

    separate the viewing area into cells.

    3. Not in the Apple Ecosystem

    Some developers and users prefer

    Apples closed and more tightly

    controlled environment to Android.

    Apples ecosystem, consisting of iCloud,

    iTunes, the App Store, the iPad, the

    iPhone, Mac computers, etc., offers avery holistic user experience with the

    ability to share data seamlessly across

    devices plus the assurance of having

    applications pre-approved for quality

    and content prior to going on sale.

  • 7/26/2019 Kinvey How to Android v2

    15/29

    13

    SET UP YOUR ANDROIDDEVELOPMENT ENVIRONMENT

    Ste 4

    Android ADT

    Java Dev Kit

    Eclipse IDE

    Android SDK

  • 7/26/2019 Kinvey How to Android v2

    16/29

    14

    HOW TO BUILD AN APP: ANDROID EDITION

    Android apps are typically written as Java

    classes. That source code is then

    compiled into bytecode that the Dalvik

    engine (a subset of a Java Virtual

    Machine) runs under the Android

    operating system. The following steps

    describe how to acquire and configure

    the tools you need to write your apps

    Java classes and then test and package

    the app for deployment. NOTE: By the

    time you read this, these instructions

    may have changed. Please refer to the

    Android Developers Websitefor current

    updates.

    For Android app development youll

    need four basic toolsets:

    1. The Java Development Kit (JDK)

    This is the Software Development Kit

    (SDK) for Java and provides the founda-

    tion for the Android SDK.

    2. Android SDK

    This consists of all the tools you need to

    develop and test your app:

    Eclipse + ADT plugin

    Android SDK Tools

    Android Platform-tools

    The latest Android platform

    The latest Android system image for

    the emulator

    3. Eclipse IDE (integrated develop-

    ment environment)

    Included in the Android SDK download,

    the Eclipse IDE provides the hands-on

    controls you need for writing your app

    using Java, the Android SDK and the

    Android ADT.

    4. Android ADT (Android Development

    Tools)This Eclipse plug-in performs

    much of the housekeeping involved in

    creating an Android app such as creating

    the needed files and overall structure.

    Installing the JDK

    To install the official Oracle Java SE SDK

    (JDK) for Windows, OS X, and Linux go to

    the Oracle Java website and follow the

    instructions.

    Installing the Android SDK

    At this point you still need to acquire the

    Java files needed to compile an Android

    app as well as some additional build

    tools and the files required to run an

    Android emulator. To access these

    add-ons, look inside the tools/ directory

    of the Android SDK you just downloaded

    and run the Android batch file or shell

    script. That opens the Android SDK

    Manager. Make sure the following items

    are checked and then click Install.

    http://developer.android.com/sdk/index.htmlhttp://developer.android.com/sdk/index.htmlhttp://www.oracle.com/us/technologies/java/overview/index.htmlhttp://www.oracle.com/us/technologies/java/overview/index.htmlhttp://developer.android.com/sdk/index.html
  • 7/26/2019 Kinvey How to Android v2

    17/29

    15

    HOW TO BUILD AN APP: ANDROID EDITION

    SDK Platform for all Android SDK

    releases you want to test against

    ARM EABI v7a System Image

    Documentation for Android SDK

    Samples for SDK

    Google APIs by Google Inc. for the

    Android SDK release youre download-

    ing

    Android SDK Tools and Platform-

    tools

    Android Support Library (in the

    Extras group at the bottom of the

    tree)

    Another Extra you can check to install is

    the Intel Hardware Accelerated Execu-

    tion Manager (HAXM), a hardware-

    assisted virtualization engine that makesyour app run faster when you run it in

    an emulator (i..e., you are not running it

    on an actual physical smartphone or

    tablet). The HAXM requires a supported

    Intel processor and Microsoft Windows

    operating system version. Check the

    HAXM installation instructionsonthe

    Intel website for further details.

    One shortcut that many developers like

    to use is to install everything with a

    single command. Just type the following

    command at the command line:

    android update sdk --no-u

    This will perform a full install of all

    Android versions and samples along

    with all system images. Obviously, this

    approach takes up the most amount of

    space on the system.

    Once all the selected items are installed

    (it may take several hours if you have a

    slow Internet connection), you are ready

    to begin making your Android app!

    Development Takeaway Android apps are written as

    Java classes

    Source code is compiled into

    bytecode that the Dalvik

    engine runs under the Android

    operating system

    Four Basic Toolsets

    1. Java Development Kit

    2. Android SDK

    3. Eclipse IDE

    4. Android ADT

    http://software.intel.com/en-us/articles/installation-instructions-for-intel-hardware-accelerated-execution-manager-microsofthttp://software.intel.com/en-us/articles/installation-instructions-for-intel-hardware-accelerated-execution-manager-microsofthttp://software.intel.com/en-us/articles/installation-instructions-for-intel-hardware-accelerated-execution-manager-microsoft
  • 7/26/2019 Kinvey How to Android v2

    18/29

    16

    DEVELOPING YOUR APPSte 5

  • 7/26/2019 Kinvey How to Android v2

    19/29

    17

    HOW TO BUILD AN APP: ANDROID EDITION

    With the development environment

    installed, the next step is to launch

    Eclipse and begin making your app. For

    this step, we will walk through two

    examples: My First App and Test

    Drive. The first app displays a button on

    the phone push the button to display

    the text, Hello World. The second app

    uses the Kinvey backend service. In that

    app there are four buttons Save,

    Load One, Load All, Load Query, and

    Delete One.

    To get started on My First App, Launch

    Eclipse and select File > New >Project to

    bring up the New Project wizard.Choose Android Application Projectand

    click Next.

    In the New Android Application panel

    enter the following information:

    For Application Name and Project

    Name enter My First App

    For Package Name enter

    com.example.myfirstapp

    For Build SDK select Android 4.2

    For Minimum Required SDK,

    choose API 9: Android 2.3

    (Gingerbread)

    Uncheck Create custom launcher

    icon

    Click Next

    This brings up the Create Activity page.

    Click BlankActivity and then Next

    This brings up the New Blank Activity

    page.

    For Activity Name enter

    MainActivity

  • 7/26/2019 Kinvey How to Android v2

    20/29

    18

    HOW TO BUILD AN APP: ANDROID EDITION

    For Layout Name enter

    activity_main

    For Title enter MainActivity

    Keep the remaining defaults as is

    Click Finish The Eclipse Package

    Explorer now appears. It shows the

    various folders containing the files that

    Eclipse will use to build your Android

    app. When you make an app you do so

    by working on the files within this

    hierarchy. Eclipse is context sensitive so

    clicking on a file brings up an appropri-

    ate wizard, graphic tool or text editor for

    working on that file.

    Eclipse just created one of these files foryou.

    AndroidManifest.xml

    This XML file is called the manifest. It is a

    configuration file that tells Android what

    your app contains. The manifest for My

    First App looks like the image below.

    The two key parts of the manifest are its

    root, the element, and that

    elements primary child, the

    element. Among other

    things, the element supplies

    the name of your apps package,

    uniquely identifying your app to the

    device running it and to the Google Play

    Store. The element

    supplies the name for the class imple-menting activity (MainActivity) and an

    describing under what

    conditions Android displays the activity.

  • 7/26/2019 Kinvey How to Android v2

    21/29

    19

    HOW TO BUILD AN APP: ANDROID EDITION

    The default intent filter, as in this case, is

    for the activity to appear in the launcher

    so users can choose to run it. Note that

    an app can have any number of activities

    an activity is the apps behavior

    associated with a particular screen.

    You can easily add or change various

    parts of the manifest, such to enable

    various permissions or screen sizes, by

    using the Eclipse manifest structurededitor. To show it, just double click

    AndroidManifest.xml in the Package

    Explorer.

    Besides the manifest, other elements in

    the application package to note are:

    res/

    Modifying this item is where much of

    your actual app making work is done. It

    holds resources such as drawable icons

    and GUI layouts that are packaged with

    the compiled Java in the application. For

    example, the layout folder contains XML

    files that determine the positioning and

    size of text boxes, buttons and other

    layout elements for a particular activity

    and also attach onClick methods to

    them. Double clicking on a layout (in our

    case, thats activity_main.xml) brings up

    the Eclipse graphical layout editor. As

    you drag and drop items into place the

    editor generates the appropriate XML.

    The drawable files, meanwhile, contain

    static images sized appropriately for

    particular ranges of screen sizes, such as

    drawable-mdi, which contains images

    sized for medium size screens.

    libs/

    This folder contains third-party Java JARs,

    such as those required to communicate

    with the Kinvey backend. You can use

    the Eclipse Navigate and Searchfunctions to find and import these JARs

    to your libs/ directory.

    scr/

    Holds the applications Java source code

    (which you can edit directly).

    gen/

    Where Eclipse build tools place the Java

    source code (in R.java) they generate (for

    example, after you import a JAR or

    modify a layout, close and then reopen

    the project).

    bin/

    Holds the compiled application.

  • 7/26/2019 Kinvey How to Android v2

    22/29

    20

    HOW TO BUILD AN APP: ANDROID EDITION

    To complete My First App, we need to

    add the button to the layout and also the

    text to be displayed when the user taps

    the button. To do that, click on

    activity_main.xml within layout in the

    Package Explorer. Eclipse opens the

    layout editor, which has both a graphical

    mode and an XML text-editing mode. In

    the graphical mode, follow these steps:

    Draw a button and a text box in the

    layout.

    On the button, type the word

    Button.

    Select the button youve just drawn

    and click the On Click item in the

    Properties palette.

    In the blank space just to the right of

    On Click in the Properties Pallet type

    the name of a method you wish to

    attach to the button (an on click

    listener that responds when the

    button is clicked). Lets type the

    method name: showHelloWorld.

    On the text box, type the string:

    @string/hello_world. This references a

    constant (Hello World!) in strings.xml

    in the values folder.

    Using a relative reference rather than a

    constant means if we ever wish tochange the displayed text, we need only

    make the change in one place rather

    than everywhere that string might be

    used.

    Switching to text-editing shows us the

    resulting XML (except for the missing

    text, in red):

    --

  • 7/26/2019 Kinvey How to Android v2

    23/29

    21

    HOW TO BUILD AN APP: ANDROID EDITION

    Type in the missing code manually. Now

    the Hello World! text will be hidden

    until the button is pressed.

    Finally, we will supply the logic for the

    onClick method we have just named. To

    do that open the src item in the Package

    Explorer and click the MainActivity.java

    file. Then type in the methods Java code,

    here shown in red:

    Note that the methods signature must

    be public and must include only one

    parameter, which is the view parameter.

    To run the app, click Run in the Eclipse

    toolbar. The app will run in the built-in

    Android emulator, as shown in the

    figure. If you plug your phone into your

    computer via a USB cable, you can also

    run the app on your own phone.

    Eclipse automatically detects the phone

    and asks whether you wish to run the

    app on the phone or in the emulator.

    Congratulations! Your app is done. Now

    might be a good time to toast your

    accomplishment.

    Making a Toast

    One of the convenient features that

    Android offers developers is the ability

    to build alerts, or toast messages, into

    applications without writing much code.

    Typically these alerts are displayed in a

    reserved area at the top or bottom of

    the phones screen whenever a predeter-

    mined event occurs.

  • 7/26/2019 Kinvey How to Android v2

    24/29

    22

    HOW TO BUILD AN APP: ANDROID EDITION

    For example, suppose we wanted the

    app we just built, My First App, to display

    the toast Button onClick Triggered!

    when the button is clicked. To do that we

    simply add the following code, shown

    here in red, to our Java source:

    The makeText method invokes the string

    referenced by TOAST_TEXT, which the

    show method displays.

    Connecting To a Backend as a Service

    One factor mentioned earlier thats

    fueling apps explosive growth is the

    availability of Cloud-based backend

    services. These are useful when your

    app needs more data than can be

    reasonably stored on a mobile device

    and/or the data must be accessible by

    multiple devices. Connecting your app to

    a commercial backend means you dont

    have to provide that service yourself

    effectively leveling the playing field for

    anyone who cant afford to build their

    own app backend versus larger organiza-

    tions that can.

    Backend as a Service (BaaS) providers, as

    theyre called, compete on a variety of

    features and benefits, one of which is

    how easy it is to connect to your app. So,

    chances are, it will be a pretty easy

    process including 8 steps.

    1.Download the providers SDK to

    your computer; this contains the

    software that lets your app talk to the

    backend2.Create an account on the providers

    website, usually with a credit card

    3.Follow the providers instructions to

    install the downloaded files into your

    application package (e.g., place the

    JARs in the libs folder)

    4.Add your app on the backend and

    type in your apps name where

    requested

    5.Configure your backend data model

    (i.e., a collection of objects)

    6.Locate your apps backend service

    credentials used to authenticate the

    app with the backend copy them

    into your apps source code

    7.Create a static app handle used to

    identify the app on backend calls

    8.Map your apps data to your

    backend model

    Heres how this would work with Kinvey

    and our second example app, Test Drive.

  • 7/26/2019 Kinvey How to Android v2

    25/29

    23

    HOW TO BUILD AN APP: ANDROID EDITION

    The app, as previously noted, displays

    Save, Load One, Load All, Load

    Query, and Delete One buttons.

    Tapping the Save button saves some

    static dummy data to the backend and

    displays a Save worked! message (or

    Save failed! if the save did not work).

    Tapping the Load One button loads the

    saved data from the backend to the

    device and displays a Load worked!

    message (or Load failed! if the load didnot work).

    Figure X shows the My Apps page on

    Kinvey with Test Drive already added

    and its application credentials displayed

    (by clicking the Credentials link). Figure

    Y shows the backend collection (named

    TestObjects) for the Test Drive app. Note

    the _id and name columns with the

    labels 12345 and My first data!

    respectively. (When configuring a

    collection, use the + and - buttons to

    add and delete columns.)

    Figure X

    Figure Y

    These labels tag the dummy TestObject

    data the app passes to TestObjects on

    the backend when the user taps the

    Save button. Similarly, when the user

    taps the Load button the app retrieves

    a TestObject from the backend TestOb-

    jects.

    For these operations to happen the

    Kinvey SDK must have been downloaded

    (see Kinveys Download Page) and these

    SDK contents installed into the project.

    After downloading the zip file, extract

    the contents and open the `libs`

    directory. Copy the contents of this

    directory into the `libs` directory of your

    Android project.

    http://devcenter.kinvey.com/android/downloadshttp://devcenter.kinvey.com/android/downloadshttp://devcenter.kinvey.com/android/downloadshttp://devcenter.kinvey.com/android/downloads
  • 7/26/2019 Kinvey How to Android v2

    26/29

    24

    HOW TO BUILD AN APP: ANDROID EDITION

    The complete Test Drive project can be downloaded from here. To illustrate

    steps 6 and 8 listed above, however, we will highlight some specific sections of

    the code. Lets start with step 6, authentication.

    To authenticate the app to Kinvey, the auto-generated credentials must be

    copied into the app, replacing your_app_key and your_app_secret in these

    lines:

    In step 7 we provide a private handle for calls to Kinvey:

    Step 8 is mapping the app data to the model we just configured at Kinvey. You

    can use any class that extends the GenericJson class, in this app example,

    thats a TestEntity class that has a string name. It looks like this:

    http://devcenter.kinvey.com/android/samples/testdrivehttp://devcenter.kinvey.com/android/samples/testdrivehttp://devcenter.kinvey.com/android/samples/testdrivehttp://devcenter.kinvey.com/android/samples/testdrive
  • 7/26/2019 Kinvey How to Android v2

    27/29

  • 7/26/2019 Kinvey How to Android v2

    28/29

    26

    HOW TO BUILD AN APP: ANDROID EDITION

    Welcome To Our World!

    The key takeaway from all this is that

    making an app is a worthwhile thing to

    do for anyone with a clever idea and a

    working knowledge of Java. The proof is

    in the hundreds of apps succeeding in

    the market every day, many written by

    people working on their own and who

    never wrote an app before in their lives.

    As with anything else, practice does

    make perfect. But hopefully with all the

    resources available, and the knowledge

    gained from this ebook, you have

    already started down the path toward

    joining the growing legions of Android

    app developers. If so, we welcome you!

  • 7/26/2019 Kinvey How to Android v2

    29/29

    Written by

    Designed by

    Jake McKibbenand Lauren Pedigo

    Randall Cronk

    Since 1990, Randall has helped over 250

    high-tech companies convey the value

    of what they do through white papers,

    web content, brochures, case studies

    and articles. Based today in downtown

    Boston, he was previously a vice

    president with Regis McKenna where he

    ran the Digital Equipment account.

    Morgan Bickle

    On any given day you'll find Morgan

    programming mobile SDKs, building

    backend APIs, and discussing the future

    of web and mobile technology. As part of

    the core founding team at Kinvey and

    now the CTO, he owns Kinveys

    technology vision. Prior to Kinvey, he

    wrote enterprise software for a decade.

    What is Kinvey? Kinvey makes a fully-featured Backend as a

    Service solution, offering 3rd party data integrations, multi-

    platform support, push notifications, and custom business logic on

    a platform where it's free to get started and you only pay when

    your app is successful.

    Build your backend today

    https://console.kinvey.com/https://console.kinvey.com/