Android101

60
Grupo de Usuários de Tecnologia Google de São Paulo

description

Android 101 talk at the GTUG meeting in São Paulo/Brazil.

Transcript of Android101

Page 1: Android101

Grupo de Usuários de Tecnologia Google de São Paulo

Page 2: Android101

Android Apps 101David Marques | March 11th, 2010

Page 3: Android101

About Me

Page 4: Android101

Lots of mobile fun :)

● Developer tools for Java ME● Mobile Device Emulator

● Eclipse Foundation Committer

Page 5: Android101

Lots of mobile fun :)

Page 6: Android101

What is Android?

Page 7: Android101

“Android is a software stack for mobile devices that includes an operating system, middleware and key applications”

Page 8: Android101

Android App Anatomy

Page 9: Android101

An android application is an island of components

Application

Linux Process

Activity

Content Provider

Service

Intent Receiver● Application Package

● Android Manifest

● Defaults to 1 process per application

● Defaults to 1 thread per application

Page 10: Android101

How do we tell the system what we have to offer?

Page 11: Android101

Meet the Manifest

<manifest> <application> <uses-permission> <activity>

<receiver> <service> <provider>

Page 12: Android101

Meet the Main Thread

● Managed Life Cycle

● System events, life cycle management callbacks and UI updates happen on the Main Thread

● Get away from it :)

Linux Process

Main Thread

Looper

Content Provider

Service

Intent Receiver

ActivityMessage

QueueAndr

oid

Syst

em

Page 13: Android101

What does he mean with GET AWAY FROM

THE MAIN THREAD??????

Page 14: Android101
Page 15: Android101

Android Security Model

Page 16: Android101

● Proven security model

● Process isolation by UID

● Permission based model

Page 17: Android101

Basic Building Blocks

Page 18: Android101

Activities Services

IntentReceivers

Content Providers

Page 19: Android101

Activities Services

IntentReceivers

Content Providers

Page 20: Android101

Activities

Page 21: Android101

IS a way to represent a particular

operation

IS NOT necessarily a screen element

Page 22: Android101

What is a Task ?

Page 23: Android101

If Activities are managed components

there must be a life cycle?

Page 24: Android101
Page 25: Android101

How does an Activity look like?

Activity.java + layout.xml

Page 26: Android101

Activity.java

public class Hello extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}

● AndroidManifest.xml

<activity android:name=".Hello" android:label="@string/app_name"><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter> </activity>

Page 27: Android101

layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>

Page 28: Android101

Activities Services

IntentReceivers

Content Providers

Page 29: Android101

Content Providers

Page 30: Android101

● Content providers store and retrieve data and make it accessible to all applications;

● They're the only way to share data across applications;

● There's no common storage area that all Android packages can access.

● How a content provider actually stores its data under the covers is up to its designer!

audio - video - images - personal contact information

Basic Concepts

Page 31: Android101

Hows does it looks like ?

public class ExampleProvider extends ContentProvider {

public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {// Returns data to the caller via a Cursor object

}public Uri insert (Uri uri, ContentValues values) {

// Inserts new data into the content provider}public int update (Uri uri, ContentValues values, String selection,

String[] selectionArgs) {// Updates existing data in the content provider

}public int delete (Uri uri, String selection, String[] selectionArgs) {

// Deletes data from the content provider}

}

<provider android:name="com.example.autos.AutoInfoProvider" android:authorities="com.example.autos.autoinfoprovider"

. . . /></provider>

Page 32: Android101

Activities Services

IntentReceivers

Content Providers

Page 33: Android101

Services

Page 34: Android101

A Service is an application component that can perform long-running operations in the

background;

It does not provide an user interface element;

Page 35: Android101

Started Services

When the operation is done, the service should stop itself.

Page 36: Android101

Started Services

When the operation is done, the service should stop itself.

Page 37: Android101

Started Services

When the operation is done, the service must stop itself.

Page 38: Android101

Hows does it looks like ?

public class ExampleService extends Service {

public void onCreate() { // The service is being created } public int onStartCommand(Intent intent, int flags, int startId) { // The service is starting, due to a call to startService() return mStartMode; } public void onDestroy() { // The service is no longer used and is being destroyed }}

<service android:name=".ExampleService" />

http://developer.android.com/guide/topics/manifest/service-element.html

Page 39: Android101

Well well well, long-running operations

+ background

And he said GET AWAY FROM THE MAIN THREAD

??????

Page 40: Android101
Page 41: Android101

“Caution: A service runs in the main thread of its hosting process the service does not create its own thread and does not run in a separate process (unless you specify otherwise).”

“If your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work.”

http://developer.android.com/guide/topics/fundamentals/services.html

Page 42: Android101

“Caution: A service runs in the main thread of its hosting process the service does not create its own thread and does not run in a separate process (unless you specify otherwise).”

“If your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work.”

http://developer.android.com/guide/topics/fundamentals/services.html

Page 43: Android101

“Caution: A service runs in the main thread of its hosting process the service does not create its own thread and does not run in a separate process (unless you specify otherwise).”

“If your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you must create a new thread within the service to do that work.”

http://developer.android.com/guide/topics/fundamentals/services.html

Page 44: Android101

I _ _ E _ _ _ E _ _ I _ E

Page 45: Android101

I N T E N T S E R V I C E_ _ _ _ _ _ _ _ _ _ _ _ _

● Creates a default worker thread;

● Creates a work queue, so you never have to worry about multi-threading;

● Stops the service for you;

http://developer.android.com/reference/android/app/IntentService.html

Page 46: Android101

Bound Service

●Allows application components to bind to it

●Expose application's functionality

Page 47: Android101

public class ExampleService extends Service { IBinder mBinder; // interface for clients that bind

public IBinder onBind(Intent intent) { // A client is binding to the service with bindService() return mBinder; } public boolean onUnbind(Intent intent) { // All clients have unbound with unbindService() return mAllowRebind; } public void onRebind(Intent intent) { // A client is binding to the service with bindService(), // after onUnbind() has already been called }}

Hows does it looks like ?

http://developer.android.com/guide/topics/fundamentals/bound-services.html

Android Interface Definition Language (AIDL)

Page 48: Android101

Activities Services

IntentReceivers

Content Providers

Page 49: Android101

Intent Receivers

Page 50: Android101

Its all about Intents!

“Intents is the mechanism by which the applications talk to each other and with the system”

ACTION_BOOT_COMPLETED - ACTION_BATTERY_CHANGED - ACTION_POWER_CONNECTED – ACTION_SHUTDOWN –

ACTION_MEDIA_MOUNTED – ACTION_NEW_OUTGOING_CALL

● Super Fast Life-Cycle: ONLY ONE CHANCE :)

● Permissions can be enforced either by the sender or receiver

Page 51: Android101

Hows does it looks like ?

public class ExampleReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {// Thats your ONLY CHANCE to do anything!// Once it returns GAME OVER

}}

● AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> ...<receiver android:name=".SMSReceiver" android:enabled="true">

<intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED" />

</intent-filter></receiver>

http://developer.android.com/reference/android/content/BroadcastReceiver.html

Page 52: Android101

DOs

Page 53: Android101

● DO run on a background thread if its not UI related;

● DO consider multiple form factors;

● DO respect the user preferences;

● DO consider that you are not running alone on the system;

● DO hire a designer;

● DO collaborate with other applications;

● DO support landscape and portrait;

● DO consider publishing on the Android Market

Page 54: Android101

DON'Ts

Page 55: Android101

● DONT replace the back, home, search and menu features;

● DONT remove the status bar unless you need to;

● DONT allocate objects unless you need to;

● DONT hold Context object references;

● DONT update Widgets toooooooo often;

● DONT wake up the system to do nothing;

● DONT log sensitive user information;

● DONT use non public APIs =X

Page 56: Android101

Development environment

Page 57: Android101

Eclipse + ADT = http://developer.android.com/sdk/eclipse-adt.htmlhttp://eclipse.org

edit+debug+log+compile+package+navigate file system+analyze memory+

edit user interface+emulate device+ localize+analyze performance+...

Page 58: Android101

Questions ?

Page 59: Android101

Lets Code :)

Page 60: Android101

Android Apps 101David Marques | March 11th, 2010