136
Introduction to Android Programming (CS5248 Fall 2015) Aditya Kulkarni ([email protected]) August 26, 2015 *Based on slides from Paresh Mayami (Google Inc.)

Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

  • Upload
    others

  • View
    29

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Introduction to Android

Programming(CS5248 Fall 2015)

Aditya Kulkarni ([email protected])

August 26, 2015

*Based on slides from Paresh Mayami (Google Inc.)

Page 2: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Contents• Introduction

• Android OS

• Environment Setup

Page 3: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Contents• Introduction

• Android OS

• Environment Setup

• Creating and Building Android App• Create an Android Project

• Building and Running

• Debugging

Page 4: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Contents• Introduction

• Android OS

• Environment Setup

• Creating and Building Android App• Create an Android Project

• Building and Running

• Debugging

• General Topics• Android Application Components

• Misc: Media Recorder, MediaCodec, HTTP Post, MP4Parser

Page 5: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Contents• Introduction

• Android OS

• Environment Setup

• Creating and Building Android App• Create an Android Project

• Building and Running

• Debugging

• General Topics• Android Application Components

• Misc: Media Recorder, MediaCodec, HTTP Post, MP4Parser

• Sample Applicationhttp://developer.android.com/guide/index.html

Page 6: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Fundamentals• Android apps are written in Java

Page 7: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Fundamentals• Android apps are written in Java

• The Android OS is a multi-user Linux system

Page 8: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Fundamentals• Android apps are written in Java

• The Android OS is a multi-user Linux system

• Android app lives in its own security sandbox• System assigns each app a unique Linux user ID

• Each process has its own virtual machine

• Every application runs in its own Linux process

Page 9: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Fundamentals• Android apps are written in Java

• The Android OS is a multi-user Linux system

• Android app lives in its own security sandbox• System assigns each app a unique Linux user ID

• Each process has its own virtual machine

• Every application runs in its own Linux process

• The Android system implements the principle of least privilege

http://developer.android.com/guide/components/fundamentals.html

Page 10: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Environment Setup

•Four basic steps to get an App running• Step 1: Install JDK

Page 11: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Environment Setup

•Four basic steps to get an App running• Step 1: Install JDK• Step 2: Configure the SDK

Page 12: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Environment Setup

•Four basic steps to get an App running• Step 1: Install JDK• Step 2: Configure the SDK• Step 3: Create your first Android App

Page 13: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Environment Setup

•Four basic steps to get an App running• Step 1: Install JDK• Step 2: Configure the SDK• Step 3: Create your first Android App• Step 4: Build and Run your App

Page 14: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Step-1: Install JDK

Page 15: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

JDK Installation• JDK and JRE are different!• Can download the JDK for your OS at

http://java.oracle.com• Alternatively, for OS X, Linux:

–OS X: • Open /Applications/Utilities/Terminal.app• Type javac at command line• Install Java when prompt appears

–Linux: • Type sudo apt–get install default–jdk at

command line (Debian, Ubuntu)• Other distributions: consult the documentation

Page 16: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Install!

16

Page 17: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Step-2: Configure the SDK

Page 18: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

SDK Configuration

•Download Android SDK from http://developer.android.com

Page 19: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

SDK Configuration

•Download Android SDK from http://developer.android.com

Or

•Simplest: Download and install Android Studio bundle (including Android SDK) for your OS

Page 20: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Install!

20

Page 21: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Step-3: Create your first Android App

Page 22: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Your first Android App

Page 23: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Long time ago, in a galaxy far, far away …

Page 24: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

• Eclipse IDE- Official IDE for Android development

• ADT plugin

• Apache Ant for building projects

Page 25: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 26: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 27: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

… so Google said …

Page 28: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 29: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 30: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 31: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 32: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 33: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 34: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 35: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 36: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 37: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 38: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 39: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 40: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 41: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 42: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 43: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 44: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 45: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 46: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 47: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 48: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 49: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 50: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 51: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 52: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 53: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 54: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 55: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 56: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 57: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 58: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 59: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 60: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 61: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 62: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 63: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 64: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Install

Page 65: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Run

Page 66: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

• Recommended: Install Android 2.2, 2.3.3 APIs and 4.x API

• Do not worry about Intel x86 Atom, MIPS system images

Settings

Run Android SDK Manager

Page 67: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Now you are ready for Android development!

Page 68: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Create new Android project

Page 69: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 70: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Create Activity

Page 71: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 72: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in
Page 73: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

The Manifest File

• Identify any user permissions the application requires

Page 74: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

The Manifest File

• Identify any user permissions the application requires

•Declare the minimum API level required by the application

Page 75: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

The Manifest File

• Identify any user permissions the application requires

•Declare the minimum API level required by the application

•Declare hardware and software features used or required by the application

Page 76: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

The Manifest File

• Identify any user permissions the application requires

•Declare the minimum API level required by the application

•Declare hardware and software features used or required by the application

• API libraries the application needs to be linked

Page 77: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

The Manifest File

Page 78: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Step-4: Building & Running your App

Page 79: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Building Process

Running

Page 80: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

No Android Device?

Page 81: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

No Android Device?

Install USB Driver !!!

Page 82: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

• Install OEM USB Driver from ASUS website.http://support.asus.com/download/ and search by Transformer

• Update Driver Software from Device Manager

• Locate USB Driver folder

• Enable USB Debugging at Transformer

USB Driver Installation

Update Driver software

Page 83: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Enabling USB Debugging

Page 84: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Running Sample App

Page 85: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Running Sample App

Page 86: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Running Sample App

Page 87: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Sample “Hello World” Code

Page 88: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Editing Sample Code

Add id to main.xml

“@+id/helloId”

Page 89: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Editing Sample Code

Add id to main.xml

Edit HelloWorldActivity.java

“@+id/helloId”

Page 90: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

A Sample Code

Page 91: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

A Sample Code

Important points

1. UI Element has an Id

2. Variables in our code linkto UI elements

3. Update UI element content from the program

1

2

3

Page 92: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

App Failure

Page 93: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Let us debug the code …

Page 94: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Always, look at DDMS !!!

Page 95: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Correction

Page 96: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Debugging via Dalvik Debug Monitor Service (DDMS)

Log.e("Hello, World", "Hello, BJ");

Page 97: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Android Application Components

Page 98: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Components

• Android Application• .apk : Android package

Page 99: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Components

• Android Application• .apk : Android package

• Four Application Components• Activity

• Service

• Content Provider

• Broadcast Receiver

Page 100: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Application Components

• Android Application• .apk : Android package

• Four Application Components• Activity

• Service

• Content Provider

• Broadcast Receiver

• Communication among components except Content Provider• Intent

Page 101: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Activity

• Activity Lifecycle • Implement Lifecycle Callbacks

Page 102: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Activity

• Hierarchical View Architecture• ViewGroup (Layout)

• View

• View

• ViewGroup (Layout)• View

• View

• …

• View

• ViewGroup• …

Page 103: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Activity

• Hierarchical View Architecture• ViewGroup (Layout)

• View

• View

• ViewGroup (Layout)• View

• View

• …

• View

• ViewGroup• …

• Do not forget to declare Activity in the Manifest

Page 104: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Activity• Start an Activity

• Start an Activity for a Result

• Caller Activity

• Callee Activity

• finish

Page 105: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Activities and Stack

Page 106: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Services

• A service is a component that runs in the background to perform long-running operations

Page 107: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Services

• A service is a component that runs in the background to perform long-running operations

• A service does not provide a user interface

Page 108: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Services

• A service is a component that runs in the background to perform long-running operations

• A service does not provide a user interface

• Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it

Page 109: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Services

• A service is a component that runs in the background to perform long-running operations

• A service does not provide a user interface

• Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it

e.g a service might play music in the background while the user is in a different app

Page 110: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Broadcast Receivers

• A broadcast receiver is a component that responds to system-wide broadcast announcements

Page 111: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Broadcast Receivers

• A broadcast receiver is a component that responds to system-wide broadcast announcements

• Apps can also initiate broadcasts

Page 112: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Broadcast Receivers

• A broadcast receiver is a component that responds to system-wide broadcast announcements

• Apps can also initiate broadcasts

• Although broadcast receivers don't display a UI, they may create a status bar notification to alert the user when a broadcast event occurs

Page 113: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Content Providers

•A content provider manages a shared set of app data

Page 114: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Content Providers

•A content provider manages a shared set of app data

•Through the content provider, other apps can query or even modify the data

Page 115: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Content Providers

•A content provider manages a shared set of app data

•Through the content provider, other apps can query or even modify the data

•Any app with the proper permissions can query part of the content provider

Page 116: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Intent

An intent is an abstract description of an operation to be performed

Page 117: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Intent

An intent is an abstract description of an operation to be performed

• It can be used with startActivity to launch an Activity

Page 118: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Intent

An intent is an abstract description of an operation to be performed

• It can be used with startActivity to launch an Activity

• broadcastIntent to send it to any interested BroadcastReceiver components

Page 119: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Intent

An intent is an abstract description of an operation to be performed

• It can be used with startActivity to launch an Activity

• broadcastIntent to send it to any interested BroadcastReceiver components

• startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service

Page 120: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

UI – Declaring Layout• Initiated when called setContentView() on

onCreate()

Page 121: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

UI – Declaring Layout• Initiated when called setContentView() on

onCreate()

• Use Visual Layout Editor for initial layout design

Page 122: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

UI – Declaring Layout• Initiated when called setContentView() on

onCreate()

• Use Visual Layout Editor for initial layout design

• Edit XML file extensively

Page 123: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

UI – Handling UI Events

• onClick, onLongClick, onKey, onTouch, …

Page 124: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Thread• UI Thread

• “Main” thread per application is responsible for interacting with UI components

Page 125: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Thread• UI Thread

• “Main” thread per application is responsible for interacting with UI components

• “Application Not Responding” problem• If UI thread is blocked more than several seconds, ANR

dialog appears

• Do not block the UI thread

• Do not access UI components outside UI thread

Page 126: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Thread• UI Thread

• “Main” thread per application is responsible for interacting with UI components

• “Application Not Responding” problem• If UI thread is blocked more than several seconds, ANR

dialog appears

• Do not block the UI thread

• Do not access UI components outside UI thread

modify

Page 127: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Use AsyncTask, Instead

Page 128: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Misc - Media Recorder

• Modify CameraPreview to see the video during recording

Page 129: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Misc – Media Codec

• To access low-level media codecs, i.e. encoder/decoder components

Page 130: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Misc - HTTP Post

• Use HttpClient and HttpPost

• Use “multi-part/form-data” to encapsulate segment

• Do not excessively use memory

Page 131: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Misc – Permission Issue

• Add following permissions to the Manifest file

Page 132: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Sample Implementation

MainActivity

CaptureActivity

Upload Service

PlayerActivity

Option menu Context menu

Capture

Capture Button Click

Play click

Background Upload

Page 133: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Misc - Integration with MP4Parser• Adding a jar file

• Create its jar file

• Add jar file to the app• Build Path > Configure Build Path > Libraries > Add JARs

• If you include source files directly,

• Put Isoparser-default.properties to assets folder

• Change getResourceAsStream(“isoparser-default.properties”) in PropertyBoxParserImpl to “/assets/isoparser-default.properties”

https://code.google.com/p/mp4parser/

https://github.com/sannies/mp4parser

Page 134: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Final Comment: Use ApiDemo !!!

Page 135: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

General References• How to work with Android GUI layouts

https://www.youtube.com/watch?v=xn9KYnwIoBE

• Building and running your app http://developer.android.com/tools/building/building-eclipse.html

• Deployment of the .apk file http://stackoverflow.com/questions/3480201/how-do-you-install-an-apk-file-in-the-android-emulator

• Find the hidden developer option http://www.cnet.com/how-to/restore-the-developer-options-menu-in-android-4-2/

• Rotation lock/unlock http://www.howtogeek.com/howto/26715/how-to-make-your-android-phone-stop-rotating-the-screen-when-you%E2%80%99re-reading-sideways/

• Video tutorials http://www.youtube.com/watch?v=5RHtKIo_KDI

• Load/Open an existing package (e.g., the provided ClassExamples) into your eclipse • File --> Import ... --> General-Tab --> Existing Projects into Workspace

(and click Next)• Select root Directory: click Browse: select the folder the include the

AndroidManifest.xml or project.properties files.• Click finish

Page 136: Introduction to Android Programmingcs5248/1516S1/l03/L3-Programming… · •Android apps are written in Java •The Android OS is a multi-user Linux system •Android app lives in

Thank You!Any questions