Android Tutorial - PowerPoint PPT Presentation

About This Presentation

Android Tutorial


Android Classes in Mumbai.. for more visit or – PowerPoint PPT presentation

Number of Views:325


Transcript and Presenter's Notes

Title: Android Tutorial


Vibrant Technologies Computers

Make Career with us !!!
K1/ 4, Second floor, Sector-15/ 16 Market, Vashi,
Navi Mumbai.
Contact 9892900103 / 9892900173 www.vibranttechn
Android Terminology
  • Component Activities, content providers,
    broadcast receivers, and services that together
    make up an application
  • Activity A single user-based task, usually, but
    not always, containing views
  • Content Provider A component that serves data to
    other applications
  • Broadcast receiver Component receiving
    notifications from other activities
  • Service Background process responding local or
    remote application requests
  • View Base class for most layout components
  • UI Control textView, EditText, CheckBox,
    RadioButton, Spinner, etc.
  • Layout Visual arrangement of containers and
  • Context Object containing the global state of an
    application environment
  • Intent Asynchronous launching (or communication)
  • Intent Filter Criteria an Android device uses to
    find matching activities

Android Terminology (cont.)
  • Preference A key/value pair, which defines a
    user option
  • Context Menu A menu executing after a long click
  • Long Click Analogous to right clicks Hold click
    for approximately two seconds
  • Modal Dialog requiring a response before
    continuing an activity
  • Localize Separate data from an application, so
    changes don't require changing source. Refer to
    the data by an identifier name.
  • Faded Edge An edge of a scrollable area that is
    faded to indicate that more information exists
  • Fragment An independent module tightly bound to
    an application
  • Bundle A set of key/value data pairs for
    persisting an application state

Android Terminology (cont.)
  • Gravity Determine alignment of text within a
    view of different size. Values can include top,
    bottom, left, center, fill
  • Content Provider Wrapper for data, exposing data
    (SQL for example) to multiple applications, using
    a standard API.
  • Cursor An object containing rows extracted from
    an SQL table
  • Projection The columns of an SQL table of
  • Live Folder Deprecated mechanism to display data
    on home page
  • Developers now use Widgets
  • Live updates display automatically
  • Widgets Miniature icon-based application view
    (clock, battery charge percent) embedded on the
    home screen or application
  • Live Wallpaper Dynamic interactive home screen

Creating an Android Application
  • Perform the following steps
  • Create a new Android project in Eclipse with the
    wizard icon
  • Register activities, specify Content Providers,
    Services, and security specifications in
  • Create localized literals in the res/values
  • Create other resources in various res directories
  • Create UI layouts in the res/layouts directory
  • Code the activity java class in the package folder

Step 1 Eclipse Android Project Wizard
  • First Screen
  • Name the project
  • Use workspace or browse to a new location
  • Second Screen Pick an Android version
  • The oldest version has the most compatibility
  • Newer versions obviously have more features
  • Some newer features are backwards compatible
  • Third Screen Pick a package name
  • Example com.acorns.lesson or
  • Click finish to complete the process

Android Application Project Structure
Automatically Generated Files
  • package com.paad.helloworldpublic final class
  • public final static boolean DEBUG true
  • package com.paad.helloworldpublic final class R
  • public static final class attr
  • public static final class drawable
  • public static final int ic_launcher0x7f020000
  • public static final class layout
  • public static final int main0x7f030000
  • public static final class string
  • public static final int app_name0x7f040001
  • public static final int hello0x7f040000

Step 2 Create the Manifest
  • The new project wizard
  • Automatically Creates a default
    AndroidManifest.xml file
  • Creates a default activity in the project
  • Alter the manifest
  • Register activities with intents, permissions,
    content providers, services, etc.
  • There are two possibilities
  • Edit the manifest directly
  • Use the Manifest Editor that displays when
    clicking the AndroidManifest.xml file

HelloWorld Manifest
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltmanifest xmlnsandroid"http//
    m/apk/res/android" package"com.paad.helloworld"
    androidversionCode"1" androidversionName"1
    .0" gt
  • ltuses-sdk androidminSdkVersion"1" // User
    cannot install if less (1default)
  • androidtargetSdkVersion"15" /gt // Verify
    features between min target
  • ltapplication androidicon"_at_drawable/ic_launc
  • androidlabel"_at_string/app_name" gt
  • ltactivity androidlabel"_at_string/app_name"
  • androidname".MyActivity" gt
  • ltintent-filter gt
  • ltaction androidname"android.intent.action.MAI
    N" /gt
  • ltcategory androidname"android.intent.category
    .LAUNCHER" /gt
  • lt/intent-filtergt
  • lt/activitygt
  • lt/applicationgt
  • lt/manifestgt

Step 2 Register Activities
Definition A focused single task
  • How? Alter the manifest!
  • Create additional activities
  • Create and/or fragments with intents,
    permissions, content providers, services, etc.
  • There are two possibilities
  • Edit the manifest directly
  • Use the GUI that displays when clicking the
    AndroidManifest.xml file(Double click
    AndroidManifest.xml, click on application, scroll
    down and click on add, Choose activity from the
    dropdown, give it a name, and click finish to
    start coding)

Definition Messages that asynchronously launch
or communicate with other applications/activities
  • Android Intent Resolver
  • Applications sends intents to the Android system
  • The Android intent resolver searches all
    application intent filters to find the most
    suitable matching activity
  • Intents can target a single application or
    multiple applications broadcast for sequential
  • Intent Filters
  • Intent fields contain criteria fields defining
    whether an activity is appropriate
  • Criteria fields include actions, a category, path
    to data, MIME type, a handling class, and
    security restrictions

More Details later
Manifest Intent Filter Example
Note An intent launches an activity
  • ltintent-filtergt
  • ltaction androidname"android.inte
    nt.action.MAIN" /gt
  • ltcategory androidname"
    tent.category.LAUNCHER" /gt
  • lt/intent-filtergtltintent-filtergt
  • ltaction androidname"android.inte
    nt.action.VIEW" /gt
  • ltaction androidname"android.inte
    nt.action.EDIT" /gt
  • ltaction androidname"android.inte
    nt.action.PICK" /gt
  • ltcategory androidname"
    tent.category.DEFAULT" /gt
  • ltdata androidmimeType"vnd.androi
    d.cursor.dir/" /gt
  • lt/intent-filtergtltintent-filtergt
  • ltaction androidname"android.inte
    nt.action.GET_CONTENT" /gt
  • ltcategory androidname"
    tent.category.DEFAULT" /gt
  • ltdata androidmimeType"vnd.androi
    d.cursor.item/" /gt
  • lt/intent-filtergt

The Main Entry when a user launches the
Launched by other applications for list access
Launched by other applications for single item
Providing Access to NotePad Data
  • ltintent-filtergt
  • ltaction androidname"android.intent.action.VIEW"
  • ltaction androidname"android.intent.action.EDIT"
  • ltaction androidname"android.intent.action.PICK
    " /gt
  • ltcategory androidname"android.intent.category.D
    EFAULT" /gt
  • ltdata androidmimeType"" /gt
  • lt/intent-filtergt
  • Declares legal activity operations on a note
  • URI, ,
    retrieves a cursor of zero or more items
    ( with note pad data
  • Legal actions view or edit the directory of data
    (VIEW and EDIT ), or pick and return a particular
    note (PICK).
  • Activities launched without a explicit package
    name require the DEFAULT category, or they will
    not be resolved

Another Android Manifest
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltmanifest xmlnsandroid"http//
  • package"com.bikerolas"
    androidversionCode"30" androidversionName"1.2
  • ltuses-permission androidname"android.permiss
  • ltuses-permission androidname"android.permiss
  • ltuses-permission androidname"android.permiss
    ion.ACCESS_GPS" /gt
  • ltuses-permission androidname"android.permiss
    ion. ACCESS_CELL_ID /gt
  • ltapplication androidicon"_at_drawable/flingicn1"
  • androidlabel"_at_string/app_name"
  • ltactivity androidname".Fling"
  • ltintent-filtergtltaction
    androidname"android.intent.action.MAIN" /gt
  • ltcategory androidname"android.intent.category.
    LAUNCHER" /gt lt/intent-filtergt
  • lt/activitygt
  • ltservice androidname".FlingService" /gt
  • ltreceiver androidname".FlingServiceManager"
  • ltintent-filtergtltaction androidname"
    ent.action.BOOT_COMPLETED" /gt lt/intent-filtergt
  • lt/receivergtlt/applicationgtltuses-sdk
  • lt/manifestgt

Note ACCESS_LOCATION and ACCESS_GPS were used by
earlier Android versions and are now deprecated
Localize Strings
How? Create XML files in the res/values
directory Why? Promotes Internationalize and
easier maintenance
  • Example (Strings.xml)
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltresourcesgt
  • ltstring name"hello"gtHello World,
  • ltstring name"app_name"gtPA4AD_Ch02_Hello_Worldlt/s
  • lt/resourcesgt
  • Build Android creates gen/ltpackageNamegt/
    automatically from the information in
  • Java Access setTitle(getText(R.string.hello))
  • Manifest XML Access androidlabel"_at_string/app_na

Layout Example (main.xml)
How? Store xml text files in the res/layouts
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltLinearLayout xmlnsandroid"http//schemas.androi" androidorientation"verti
    cal" androidlayout_width"fill_parent"
    androidlayout_height"fill_parent"gt ltTextView
    androidtext"_at_string/hello" /gt
  • lt/LinearLayoutgt
  • Notes
  • fill_parent (or match_parent Android 2.0 and
    later) fill the entire space of the parent layout
  • Wrap_content uses as little space as needed
  • LinearLayout is a vertical or horizontal list of
    items (like BoxLayout in Swing)

The Java Code
  • package com.paad.helloworld
  • import
  • import android.os.Bundle
  • public class MyActivity extends Activity
  • / Called when the Activity is created or
    restarted. /
  • _at_Override public void onCreate(Bundle
  • super.onCreate(savedState)

A Bundle is a bunch of key/value pairs for
Applications to restore their state after an
Android termination
Android Emulator
  • Start the Emulator from Eclipse
  • Right click on the application
  • Select run as and then Android application
  • To debug Select debug as and then Android
  • After the first time, just click the run icon at
    the top of the Eclipse window
  • Important notes
  • The emulator takes quite a bit of time to launch
    be patient
  • Dont shut down the emulator between application
    runs. Closing leads to long start up times, and
    sometimes confuses Eclipse
  • Every Eclipse run reinstalls and re-launches the

Debugging Applications
  • Set debuggable"true" in the ltapplicationgt tag
  • This is automatic when debugging in the emulator
  • Manually explicitly set preference to debug on a
    real device
  • Eclipse views
  • DDMS Dalvik Debug Monitor Server
  • Simulate Event Global Positioning Service (GPS),
    phone calls, Short Message System (SMS), etc.
  • Debug Start with "Debug as"
  • Execute emulator to get a terminal window and
    execute Linux commands to connect to SQL, execute
    queries, etc.
  • ADB (Android Debug Bridge) Debug on actual
    devices (if debugging enabled) using Eclipse
    Note ADB is part of the SDK platform tools
  • Techniques Logcat displays console output, Trace
    , Breakpoints, Strict mode

Enable Device Debugging
  • Eclipse windowgtopen perspective-gtothergtDDMS
  • Enable debugging on device
  • Older devices settingsgtapplicationsgtdevelopment
  • Newer devices settingsgtabout phonegt tap build
    seven timesgt back

Debugging Techniques
  • Tracing the execution
  • Start android.os.Debug.startmethodTracing("foo")
  • Stop android.os.Debug.stopMethodTracing()
  • Writes output to foo.trace on the emulator or
  • Log class
  • Analogous to Java System.out.println or
    Javascript Console.log
  • Example Log.v("category string", "message")
  • Asynchronous Alert Dialogs AlertDialog.Builder
    build new AlertDialog(context)build.setTitle("
    Some Message")AlertDialog ad
  • Breakpoints Set Eclipse breakpoint and use
    "Debug as" option

Strict Mode (Android 2.3 and newer)
  • Report violations and IO Operations Disk reads,
    disk writes, etc.
  • Common options when detection occurs Write a Log
    message, display a dialog message, or crash the
  • ExampleStrictMode.setVMPolicy(new
    StrictMode.VMPolicy.builder() .detectDiskWrites()
    .detectLeakedSqlLiteObjects() .penaltyLog().penal
  • Note The build retuns a ThreadPolicy object to
    the VM Policy builder
  • Detect if in debug modeApplicationInfo info
    getContext().getApplicationInfo()if (info.flags
    ApplicationInfo.FLAG_DEBUGGABLE) ! 0) /
    code here /
Write a Comment
User Comments (0)