Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Android Develpment

Introduction to Android Develpment

Me and my friend Eugeniu Arbuleac had the pleasure to present an Introduction to Android development course at Google Developers Group Bucharest first 2013 meetup.

Andrei Catinean

March 03, 2013
Tweet

More Decks by Andrei Catinean

Other Decks in Programming

Transcript

  1. Introduction to Android
    Development

    View Slide

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

    View Slide

  3. Activities and UI
    Intents
    Services
    Broadcast Receivers

    View Slide

  4. Activities and UI

    View Slide

  5. Activities and UI
    A screen
    Application = Σ activity

    View Slide

  6. Activity Lifecycle

    View Slide

  7. Managed by ActivityManager
    Activity Lifecycle

    View Slide

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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. @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");
    }
    }

    View Slide

  14. Declaring the Activity



    ...

    ...

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

    View Slide

  15. For your main activity use Intent Filters









    ...




    ...

    ...

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

    View Slide

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

    View Slide

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

    View Slide

  18. 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

    View Slide

  19. Layouts and views hierarchy

    View Slide

  20. Intents

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. 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

    View Slide











  25. 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

    View Slide

  26. Intent Filters
    Activity
    Service
    Receiver
    Action

    View Slide




  27. Intent Filters
    Action
    Activity
    Service
    Receiver
    AndroidManifeset.xml

    View Slide

  28. Services

    View Slide

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

    View Slide

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

    View Slide

  31. 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

    View Slide

  32. 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

    View Slide


  33. Called via its class name





    Declaring the Service
    Called via action

    View Slide

  34. Broadcast Receivers

    View Slide

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

    View Slide

  36. 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

    View Slide

  37. 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

    View Slide






  38. Declaring it in AndroidManifest.xml
    Registering the Broadcast Receiver

    View Slide

  39. @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

    View Slide

  40. Questions
    That’s all!

    View Slide

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

    View Slide