Sei sulla pagina 1di 39

Introduction to Android Programming

Joseph Anthony C. Hermocilla


Institute of Computer Science University of the Philippines Los Banos 24 February 2012

Outline
I.Introduction to Android II.Android Programming III.Demonstration

I. Introduction to Android

What is Android?

A software stack for mobile devices

operating system+middleware+applications

Android SDK provides tools and APIs Java Programming Language Extensible Markup Language (XML)

Android Features

Application framework Dalvik virtual machine Integrated browser Optimized graphics SQLite Media Support (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) GSM Telephony Bluetooth, EDGE, 3G, WiFi Camera, GPS, compass, accelerometer

Android Architecture (1)

Applications

Email client, SMS program, calendar, maps, browser Views, Content Providers, Resource Mgr, Activity Mgr C lib, Media lib, Surface Mgr, SGL, 3D libs, FreeType Dalvik VM Version 2.6

Application Framework

Libraries

Android Runtime

Linux Kernel

Android Architecture (2)

App Fundamentals

Java compiled code+data+resources .apk (app) Each app lives in its own security sandbox

own user id, own process, own VM principle of least privilege

Possible to share data between apps and access system services

same user id, permission to access device data

App Components (1)

Activities

represents a single screen with UI subclass of Activity

Services

Component that runs in the background (no UI) subclass of Service

App Components (2)

Content Providers

manages a shared set of application data stored in the file system, SQLite db, web subclass of ContentProvider

Broadcast Receivers

Responds to system-wide broadcast announcements subclass of BroadcastReceiver and each broadcast delivered as Intent

App Components (3)

Any application can start another application's component

ex. using the camera

Apps don't have a single point of entry

no main() function

To activate a component in another app, tell system of intent to start the component

Only system can start the component

App Components (4)

Intents are used to activate components (except for ContentProvider)

Activates a specific component or component type Maybe implicit or explicit

ContentResolver for ContentProvider

App Components (5)

Pass an Intent to startActivity(), startActivityForResult() Pass an Intent to startService() Pass an Intent to sendBroadcast(), sendOrderedBroadcast() Perform a query() on a ContentResolver

Manifest File (1)

AndroidManifest.xml Tells Android that the component being activated exists in the app All components must be declared here Must be at the root of the application project directory

Manifest File (2)

Identify user permissions access contacts Declare minimum API Level Declare hardware and software features needed by app camera, accelerometer API libraries that needs to be linked Google Maps

Manifest File (3)


<?xml version="1.0" encoding="utf-8"?> <manifest ... > <application android:icon="@drawable/app_icon.png" ... > <activity android:name="com.example.project.ExampleActivity" android:label="@string/example_label" ... > </activity> ... </application> </manifest>

Manifest File (4)

<activity> <service> <receiver> <provider>

Manifest File (5)

intent actions describe the type of action to be performed and allow the system to find a component on the device intent filters identifies the components that can respond to an intent

<intent-filter> inside component declaration

Manifest File (6)

Device characteristics

Screen size and density: <supports-screens> Input configurations: <uses-configuration> Device features: <uses-feature> Platform version: <uses-sdk>

App Resources

Images, audio files, visual presentation Uses XML Referencing a resource:

res/drawable/logo.png Res ID: R.drawable.logo

II. Application Development

Intro (1)

Android SDK Eclipse ADT or command line Steps

Setup Development Debugging and Testing Publishing

Intro (2)

Intro (3)

Command line tools

android

Create and update Android projects and create, move, and delete AVDs. Run your Android applications on an emulated Android platform. Interface with your emulator or connected device

Android Emulator

Android Debug Bridge

Others: Ant, Keytool, Jarsigner

Virtual Devices (1)

Android Virtual Device (AVD)

Hardware profile Mapping to a system image Other options: skin, SD card Dedicated storage

Virtual Devices (2)


$./android list targets $./android create avd -n <name> -t <targetID> [-<option> <value>] ... ex. $./android create avd -n UPLB_2.3.3 -t 1

Projects (1)

Android Projects,Test Projects,Library Projects src/ - stub Activity file bin/ output directory of build jni/ native code sources gen/ generated by ADT assets/ textures/game data

Projects (2)

res/ app resources

anim/ - XML compiled to animation objects color/ - XML files that describe colors drawable/ - bitmaps layout/ - XML files compiled to screen layouts menu/ - application menus raw/ - assets values/ - others xml/ - misc XML files

Projects (2)

libs/ private libraries AndroidManifest.xml project.properties local.properties ant.properties build.xml

Creating a Project (1)


$./android create project \ --target <target_ID> \ --name <your_project_name> \ --path path/to/your/project \ --activity <your_activity_name> \ --package <your_package_namespace>

Creating a Project (2)


$./android create project \ --target 1 \ --name MyAndroidApp \ --path ./MyAndroidAppProject \ --activity MyAndroidAppActivity \ --package com.example.myandroid

Building your App (1)

Build process Android projects are compiled and packaged into an .apk file

.dex - .class converted to Dalvik byte code Binary version of AndroidManifest.xml resources.arsc compiled resources uncompiled resources

Debug Mode and Release Mode

Building your App (2)

Building your App (3)

Building your App (4)


$ant debug - this command will create a debug .apk file in the bin/ directory

Running your App


$./emulator -avd UPLB_2.3.3 $./adb install <path_to_your_bin>-debug.apk

Hello World App


package org.jachermocilla.android; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class JACHAndroidAppActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android! --JACH"); setContentView(tv); } }

Resources

http://developer.android.com

Demonstration

Potrebbero piacerti anche