android jumpstart

47
Android Jump Start By Pune Google Technology User Group (Pune-GTUG)

Upload: rohit-ghatol

Post on 19-May-2015

2.076 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Android jumpstart

Android Jump Start

By Pune Google Technology User Group

(Pune-GTUG)

Page 2: Android jumpstart

Topics

• Android Basics• Android Building Blocks• Building Application

Page 3: Android jumpstart

Android Basics

• Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

Page 4: Android jumpstart

Android Basics

Features• Application framework enabling reuse and replacement of components• Dalvik virtual machine optimized for mobile devices• Integrated browser based on the open source WebKit engine• Optimized graphics powered by a custom 2D graphics library; 3D graphics

based on the OpenGL ES 1.0 specification (hardware acceleration optional)• SQLite for structured data storage• Media support for common audio, video, and still image formats (MPEG4,

H.264, MP3, AAC, AMR, JPG, PNG, GIF)• GSM Telephony (hardware dependent)• Bluetooth, EDGE, 3G, and WiFi (hardware dependent)• Camera, GPS, compass, and accelerometer (hardware dependent)• Rich development environment including a device emulator, tools for

debugging, memory and performance profiling, and a plugin for the Eclipse IDE

Page 5: Android jumpstart

Android Basics

Page 6: Android jumpstart

Topics

• Android Basics• Android Building Blocks• Building Application

Page 7: Android jumpstart

Building Blocks

Activity ServiceBroadcastReceiver

Content Provider

BackgroundProcess♫♪♫♪♫

Data Store(Playlist)

Communication is using Intents

Phone Call Comes

Page 8: Android jumpstart

Topics

• Android Basics• Android Building Blocks• Building Application

Page 9: Android jumpstart

Building Application .

Lets build a new Music Player

Page 10: Android jumpstart

Building Application .

• Step 1 – You need a Screen (Activity)

Activity

Views Layouts

Views could be• Buttons• Text Views• etc ….

Layouts could be• Linear Layout• Relative Layout• Table Layout• etc …..

Page 11: Android jumpstart

Building Application .

• Activity Life Cycle

Page 12: Android jumpstart

ForegroundLifeCycle

Visible LifeCycle

Complete LifeCycle

Building Application .

• Activity Life Cycle made easier

onCreate

onDestroy

onStart

onStop

onResume

onPause

Page 13: Android jumpstart

Building Application .

• What to do in each life cycle methods?

onCreate

onDestroy

onStart

onStop

onResume

onPause

Page 14: Android jumpstart

Building Application .

• Lets Draw the Screen using Linear Layouts

Current Playlist

Player

Page 15: Android jumpstart

Building Application .

• Lets Draw the Screen using Linear Layouts

Current Music Info

Progress Bar

Buttons

Music file 1

Music file 2

Music file 3

Music file 4

Page 16: Android jumpstart

Building Application .

• Lets Draw the Screen using Linear Layouts

Current Music Info

Progress Bar

Music file 1

Music file 2

Music file 3

Music file 4

Prev NextPlay

Page 17: Android jumpstart

Building Application .

• Lets Draw the Screen using Relative Layouts

Beautiful World

Take That

Title is aligned to the top and right of the image

Author is aligned to the bottom and right of the image

Page 18: Android jumpstart

Building Application .

• Use Layouts– Layouts can be defined in different XML files– Code can refer to these layout xml files

Page 19: Android jumpstart

Building Application .• Use Layouts

Page 20: Android jumpstart

Building Application .• Use Layouts

Page 21: Android jumpstart

Building Application .• Use Layouts

Page 22: Android jumpstart

Building Application .

• Lets write code to play an mp3 file

MediaPlayer mp = new MediaPlayer();

public void play(){

mp.setDataSource(PATH_TO_FILE);

mp.prepare();

mp.start();

}

Page 23: Android jumpstart

Building Application .• Lets attach this code to the button

public void onCreate(final Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final Button playButton = (Button)findViewById(R.id.button_play);

playButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(final View arg0)

{

play();

}

});

}

Page 24: Android jumpstart

Building Application .

• Refer following for detailed instructions http://www.helloandroid.com/node/134

Page 25: Android jumpstart

Problem with Playing .in Activity

• Phone Calls• Pressing Back Button

Will kill you Activity and your Music will stop playing

Page 26: Android jumpstart

Building Application .

• Step 2 – Delegate Playing to a Service1. Define an MusicPlayer.aidl with methods

2. Autogenerates MusicPlayer Interface

3. Extend Service Implement MusicPlayer Stub

4. Declare Service in Manifest.xml

5. Create a ServiceConnection Object

6. Bind to Service

7. Access the service methods

Page 27: Android jumpstart

Building Application .

Service

LifeCycle

Page 28: Android jumpstart

Building Application .Service lifecycle (Note)A service can be used in two ways:• It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started

by calling Context.startService() and stopped by callingContext.stopService(). It can stop itself by calling Service.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.

• It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.

The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed.

Like an activity, a service has lifecycle methods that you can implement to monitor changes in its state. But they are fewer than the activity methods — only three — and they are public, not protected:

• void onCreate() • void onStart(Intent intent)• void onDestroy()

Page 29: Android jumpstart

Building Application .

• Create an aidl Interface

Page 30: Android jumpstart

Building Application .

• Extend Service, Implement Stub

Page 31: Android jumpstart

Building Application .

• Declare in Manifest.xml

• In order to use a remote service first we must add a line to our AndroidManifest.xml file inside our application tag to define our service, here is that line:

<service class=".MusicService" android:process=":remote" />

Page 32: Android jumpstart
Page 33: Android jumpstart

Building Application .

• Refer following for detailed instructions http://www.helloandroid.com/node/140

Page 34: Android jumpstart

Building Application .

• Step 3 – Reuse using Content Provider

ExistingPlaylist

On ExistingMusic Player

Old Music Player New Music Player

Page 35: Android jumpstart

Building Application .

• Step 3 – Reuse using Content Provider

ExistingPlaylist

On ExistingMusic Player

Old Music Player New Music Player

?

Page 36: Android jumpstart

Building Application .

• Content Resolver & Content Providers

ContentProvider

ContentResolver

URI of Provider

CursorApplication

ContentProvider

Application

ContentProvider

Application

ContentProvider

Application

ContentProvider

URI matches

Page 37: Android jumpstart

Building Application .

• Content Resolver & Content Providers

ContentProvider

• Extends ContentProvider• More like a restful Service• Has a Unique URI• CRUD Methods

Page 38: Android jumpstart

Building Application .

Content Resolver & Content Providers

String[] projection = new String[] { People._ID, People._COUNT, People.NAME, People.NUMBER };

Uri contacts = People.CONTENT_URI;

Cursor managedCursor = managedQuery(contacts, projection,

// Which columns to return

null,

// Which rows to return (all rows)

null,

// Selection arguments (none)

// Put the results in ascending order by name

People.NAME + " ASC");

Page 39: Android jumpstart

Building Application .

Content Resolver & Content Providers

private void getColumnData(Cursor cur){

if (cur.moveToFirst()) {

String name;

String phoneNumber;

int nameColumn = cur.getColumnIndex(People.NAME);

int phoneColumn = cur.getColumnIndex(People.NUMBER);

String imagePath;

do {

// Get the field values

name = cur.getString(nameColumn);

phoneNumber = cur.getString(phoneColumn);

// Do something with the values. ...

} while (cur.moveToNext());

}

}

Page 41: Android jumpstart

Building Application .

• Step 4 – Keeping informed - Broadcast Receivers

Phone Call Comes

Pause the Music Player

Phone rings

Conversationends

Page 42: Android jumpstart

Building Application .

• Understanding Intents

Intents

Explicit Implicit

Need• Class Name

Need • ACTION• CATEGORY• DATA

Page 43: Android jumpstart

Building Application .

• Understanding Intents

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample“ android:versionCode="1“ android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloWorld" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /></manifest>

Program launcher shows all the activities which have MAIN Action and LAUNCHER category

Page 44: Android jumpstart

Building Application .

• Understanding Intents

So what happens two activities have the exact same intent filter and an intent is fired.

Simple you choose one application, and you have an option to tell to se that application as the default application hence forth

Page 45: Android jumpstart

Building Application .

• Understanding Intents

Intent to launch an Activity

• Context.startActivity(intent)

• ContextstartActivityForResult(intent)

Intent to launch an Service

• Context.startService(intent)

Intent to send a broadcast

• Context.sendBroadCast(intent)

Page 46: Android jumpstart

Building Application .

• Step 4 – Keeping informed - Broadcast Receivers– Add BroadCast Receiver– Check Phone status and pause or play the

music using service

• <receiver android:name=".PhoneReceiver" android:enabled="true">      <intent-filter>        <action android:name=          "android.intent.action.PHONE_STATE" />      </intent-filter>    </receiver>

Page 47: Android jumpstart

Building Application .

• Step 4 – Keeping informed - Broadcast Receivers

public class Receiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

if (intentAction.equals(ACTION_PHONE_STATE_CHANGED)) {                        pstn_state = intent.getStringExtra("state");                       //Depending on the pstn state pause or play music using the

//MusicService

                }

}

}