Slide 1

Slide 1 text

Introduction to Android Development

Slide 2

Slide 2 text

Eugeniu Arbuleac @arbuleac [email protected] Andrei Catinean @electryc [email protected] WHO WE ARE

Slide 3

Slide 3 text

Activities and UI Intents Services Broadcast Receivers

Slide 4

Slide 4 text

Activities and UI

Slide 5

Slide 5 text

Activities and UI A screen Application = Σ activity

Slide 6

Slide 6 text

Activity Lifecycle

Slide 7

Slide 7 text

Managed by ActivityManager Activity Lifecycle

Slide 8

Slide 8 text

Developer says what happens at each state $ ¢ Activity Lifecycle Managed by ActivityManager

Slide 9

Slide 9 text

D/MyActivity( 1146): onCreate D/MyActivity( 1146): onStart D/MyActivity( 1146): onResume First time run D/MyActivity( 1146): onClickAnotherActivity D/MyActivity( 1146): onPause D/MyActivity( 1146): onStop D/MyActivity( 1146): onRestart D/MyActivity( 1146): onStart D/MyActivity( 1146): onResume Open another activity, then Back button Activity Lifecycle

Slide 10

Slide 10 text

D/MyActivity( 1146): onPause D/MyActivity( 1146): onStop D/MyActivity( 1146): onDestroy D/MyActivity( 1146): onCreate D/MyActivity( 1146): onStart D/MyActivity( 1146): onResume Rotate screen Activity Lifecycle

Slide 11

Slide 11 text

D/MyActivity( 1146): onPause D/MyActivity( 1146): onStop Home Button D/MyActivity( 1146): onPause D/MyActivity( 1146): onStop D/MyActivity( 1146): onDestroy D/MyActivity( 1146): onCreate D/MyActivity( 1146): onStart D/MyActivity( 1146): onResume Rotate screen Activity Lifecycle

Slide 12

Slide 12 text

package ro.gdgcluj.demoapp; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MyActivity extends Activity { static final String TAG = MyActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Log.d(TAG, "onCreate"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } Activity Lifecycle

Slide 13

Slide 13 text

@Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } }

Slide 14

Slide 14 text

Declaring the Activity ... ... Let your application know about your Activity into the AndroidManifest.xml

Slide 15

Slide 15 text

For your main activity use Intent Filters ... ... ... Let your application know about your Activity into the AndroidManifest.xml Declaring the Activity

Slide 16

Slide 16 text

Building Android UI XML Declare UI in XML Inflate XML in Java files

Slide 17

Slide 17 text

Building Android UI Programmatically Initialize new widgets Customize properties for each VS. XML Declare UI in XML Inflate XML in Java files

Slide 18

Slide 18 text

Use them both Programmatically Initialize new widgets Customize properties for each VS. XML Declare UI in XML Inflate XML in Java files Building Android UI

Slide 19

Slide 19 text

Layouts and views hierarchy

Slide 20

Slide 20 text

Intents

Slide 21

Slide 21 text

Used to start activities, start/stop services, or send broadcasts Intents

Slide 22

Slide 22 text

startActivity(Intent activity); startService(Intent service); stopService(Intent service); sendBroadcast(Intent intent); Using Intents

Slide 23

Slide 23 text

startActivity(new Intent(this, TargetActivity.class)); startService(new Intent(this, TargetService.class)); Explicit Intents

Slide 24

Slide 24 text

startService(new Intent("example.intent.action.IntentService")); sendBroadcast(new Intent("example.intent.action.Receiver")); Implicit Intents startActivity(new Intent(this, TargetActivity.class)); startService(new Intent(this, TargetService.class)); Explicit Intents

Slide 25

Slide 25 text

startService(new Intent("example.intent.action.IntentService")); sendBroadcast(new Intent("example.intent.action.Receiver")); Implicit Intents startActivity(new Intent(this, TargetActivity.class)); startService(new Intent(this, TargetService.class)); Explicit Intents AndroidManifest.xml

Slide 26

Slide 26 text

Intent Filters Activity Service Receiver Action

Slide 27

Slide 27 text

Intent Filters Action Activity Service Receiver AndroidManifeset.xml

Slide 28

Slide 28 text

Services

Slide 29

Slide 29 text

Run in background Don’t have UI Run on the UI thread Services

Slide 30

Slide 30 text

UI Activity Service startService(); stopService(); Services Run in background Don’t have UI Run on the UI thread

Slide 31

Slide 31 text

Service starts and "runs" until it gets a request to stop To offload work from main thread, use intent service. Intent service uses worker thread, stops when done with work. Service Lifecycle

Slide 32

Slide 32 text

package ro.gdgcluj.demoapp; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { static final String TAG = MyService.class.getSimpleName(); @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { Log.d(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return START_STICKY; } @Override public void onDestroy() { Log.d(TAG, "onDestroy"); } } Service Example

Slide 33

Slide 33 text

Called via its class name Declaring the Service Called via action

Slide 34

Slide 34 text

Broadcast Receivers

Slide 35

Slide 35 text

Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o. Broadcast Receivers

Slide 36

Slide 36 text

Register for certain intents Get notified when intent happens Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o. Broadcast Receivers

Slide 37

Slide 37 text

package ro.gdgcluj.demoapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class Receiver extends BroadcastReceiver { static final String TAG = Receiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive action: "+intent.getAction() ); } } Broadcast Receiver Example

Slide 38

Slide 38 text

Declaring it in AndroidManifest.xml Registering the Broadcast Receiver

Slide 39

Slide 39 text

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // Create the receiver receiver = new Receiver(); filter = new IntentFilter( ANY_INTENT_ACTION ); } protected void onResume() { super.onResume(); super.registerReceiver(receiver, filter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } Registering the Broadcast Receiver Registering Programmatically

Slide 40

Slide 40 text

Questions That’s all!

Slide 41

Slide 41 text

Eugeniu Arbuleac @arbuleac [email protected] Andrei Catinean @electryc [email protected] THANK YOU