You are on page 1of 60

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

Android Apps 101


David Marques | March 11th, 2010
About Me
Lots of mobile fun :)

● Developer tools for Java ME


● Mobile Device Emulator

● Eclipse Foundation Committer


Lots of mobile fun :)
What is Android?
“Android is a software stack for mobile devices that includes
an operating system, middleware and key applications”
Android App Anatomy
An android application is an island of components

Intent Receiver
● Application Package
● Android Manifest
Content
Provider ●Defaults to 1 process
per application
Activity Service ●Defaults to 1 thread
Linux Process per application

Application
How do we tell the
system what we
have to offer?
Meet the Manifest

<manifest> <application>
<uses-permission> <activity>
<receiver> <service> <provider>
Meet the Main Thread

Intent Receiver ● Managed Life Cycle


Content ● System events, life
Provider
cycle management
Android System

Looper
Service
callbacks and UI
updates happen on
the Main Thread
Message
Queue Activity ● Get away from it :)
Main Thread

Linux Process
What does he mean
with GET AWAY FROM
THE MAIN THREAD
??????
Android Security Model

Proven security model

Process isolation by UID

Permission based model
Basic Building Blocks
Activities Services

Content Intent
Providers Receivers
Activities Services

Content Intent
Providers Receivers
Activities
IS
a way to represent a particular
operation

IS NOT
necessarily a screen element
What is a Task ?
If Activities are
managed components
there must be a
life cycle?
How does an Activity look like?

Activity.java + layout.xml
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>
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>
Activities Services

Content Intent
Providers Receivers
Content Providers
Basic Concepts
● 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


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>
Activities Services

Content Intent
Providers Receivers
Services
A Service is an application component that
can perform long-running operations in the
background;

It does not provide an user interface


element;
Started
Services
When the operation
is done, the service
should stop itself.
Started
Services
When the operation
is done, the service
should stop itself.
Started
Services
When the operation
is done, the service
must stop itself.
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
Well well well,
long-running operations
+
background

And he said GET


AWAY FROM THE
MAIN THREAD
??????
“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
“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
“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
I__E__ _E__I_E
INTENT SERVICE
______ _______
● 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
Bound
Service
●Allows application
components to bind
to it
●Expose application's
functionality
Hows does it looks like ?
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
}
}

Android Interface Definition Language (AIDL)

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

Content Intent
Providers Receivers
Intent Receivers
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
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
DOs
● 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
DON'Ts
● 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
Development environment
Eclipse + ADT =
http://eclipse.org http://developer.android.com/sdk/eclipse-adt.html

edit+debug+log+compile+package+
navigate file system+analyze memory+
edit user interface+emulate device+
localize+analyze performance+...
Questions ?
Lets Code :)
Android Apps 101
David Marques | March 11th, 2010

You might also like