WHO WE AREEugeniu Arbuleac                          Andrei Catinean@arbuleac                                            @e...
Activities and UI
Activities and UI                    Intents
Activities and UI                     Intents          Services
Activities and UI                                 IntentsBroadcast Receivers                      Services
Activities and UI
Activities and UI                    A screen                    Application = Σ activity
Activity Lifecycle
Activity Lifecycle                     Managed by ActivityManager
Activity Lifecycle         $     ¢               Managed by ActivityManager               Developer says what happens at e...
Activity LifecycleFirst time runD/MyActivity( 1146): onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146):onResume Open ...
Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActiv...
Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActiv...
Activity Lifecycle        package ro.gdgcluj.demoapp;        import android.app.Activity;        import android.os.Bundle;...
@Override    protected void onPause() {        super.onPause();        Log.d(TAG, "onPause");    }    @Override    protect...
Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml  <manifest ... >    <appl...
Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml  <manifest ... >    <appl...
Building Android UI XML  Declare UI in XML  Inflate XML in Java files
Building Android UI XML                                          Programmatically  Declare UI in XML           VS.        ...
Building Android UI XML                                          Programmatically  Declare UI in XML           VS.        ...
Layouts and views hierarchy
Intents
Intents  Used to start activities, start/stop services, or send broadcasts
Using IntentsstartActivity(Intent activity);startService(Intent service);stopService(Intent service);sendBroadcast(Intent ...
Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));
Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));...
Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));...
Intent Filters                    Activity           Action   Service                    Receiver
Intent Filters                                                      Activity                               Action         ...
Services
Services  Run in background  Don’t have UI  Run on the UI thread
Services  Run in background                  UI Activity  Don’t have UI          startService();         stopService();  R...
Service Lifecycle              Service starts and "runs" until it              gets a request to stop              To offl...
Service Example        package ro.gdgcluj.demoapp;        import   android.app.Service;        import   android.content.In...
Declaring the Service   Called via its class name   <service android:name=".ServiceDemo"></service>   Called via action   ...
Broadcast Receivers
Broadcast Receivers     Intent based publish-subscribe mechanism   Listening system events: incoming calls, SMS messages a...
Broadcast Receivers     Intent based publish-subscribe mechanism   Listening system events: incoming calls, SMS messages a...
Broadcast Receiver Example        package ro.gdgcluj.demoapp;        import   android.content.BroadcastReceiver;        im...
Registering the Broadcast Receiver  Declaring it in AndroidManifest.xml  <receiver android:name=".ReceiverDemo">    <inten...
Registering the Broadcast Receiver  Registering Programmatically   @Override   protected void onCreate(Bundle savedInstanc...
That’s all!Questions
THANK YOUEugeniu Arbuleac                         Andrei Catinean@arbuleac                                           @elec...
Introduction to android
Upcoming SlideShare
Loading in …5
×

Introduction to android

197 views

Published on

GDG Bucharest first meetup 2013

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
197
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to android

  1. 1. WHO WE AREEugeniu Arbuleac Andrei Catinean@arbuleac @electrycarbuleac.ev@gmail.com andrei.catinean@gmail.com
  2. 2. Activities and UI
  3. 3. Activities and UI Intents
  4. 4. Activities and UI Intents Services
  5. 5. Activities and UI IntentsBroadcast Receivers Services
  6. 6. Activities and UI
  7. 7. Activities and UI A screen Application = Σ activity
  8. 8. Activity Lifecycle
  9. 9. Activity Lifecycle Managed by ActivityManager
  10. 10. Activity Lifecycle $ ¢ Managed by ActivityManager Developer says what happens at each state
  11. 11. Activity LifecycleFirst time runD/MyActivity( 1146): onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146):onResume Open another activity, then Back buttonD/MyActivity( 1146):onClickAnotherActivityD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onRestartD/MyActivity( 1146):onStartD/MyActivity( 1146): onResume
  12. 12. Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActivity( 1146):onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146): onResume
  13. 13. Activity LifecycleRotate screenD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStopD/MyActivity( 1146):onDestroyD/MyActivity( 1146):onCreateD/MyActivity( 1146):onStartD/MyActivity( 1146): onResumeHome ButtonD/MyActivity( 1146):onPauseD/MyActivity( 1146):onStop
  14. 14. Activity Lifecycle 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"); }
  15. 15. @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"); }}
  16. 16. Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml <manifest ... > <application ... > <activity android:name=".MyActivity" /> ... </application ... > ... </manifest >
  17. 17. Declaring the ActivityLet your application know about your Activity into the AndroidManifest.xml <manifest ... > <application ... > <activity android:name=".MyActivity" /> ... </application ... > ... </manifest >For your main activity use Intent Filters <manifest ... > <application ... > <activity android:name=".MyActivity" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> <intent-filter /> <activity /> </application ... > ... </manifest >
  18. 18. Building Android UI XML Declare UI in XML Inflate XML in Java files
  19. 19. Building Android UI XML Programmatically Declare UI in XML VS. Initialize new widgets Inflate XML in Java files Customize properties for each
  20. 20. Building Android UI XML Programmatically Declare UI in XML VS. Initialize new widgets Inflate XML in Java files Customize properties for each Use them both
  21. 21. Layouts and views hierarchy
  22. 22. Intents
  23. 23. Intents Used to start activities, start/stop services, or send broadcasts
  24. 24. Using IntentsstartActivity(Intent activity);startService(Intent service);stopService(Intent service);sendBroadcast(Intent intent);
  25. 25. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));
  26. 26. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));Implicit IntentsstartService(new Intent("example.intent.action.IntentService"));sendBroadcast(new Intent("example.intent.action.Receiver"));
  27. 27. Explicit IntentsstartActivity(new Intent(this, TargetActivity.class));startService(new Intent(this, TargetService.class));Implicit IntentsstartService(new Intent("example.intent.action.IntentService"));sendBroadcast(new Intent("example.intent.action.Receiver"));AndroidManifest.xml<service android:name=".IntentService"> <intent-filter> <action android:name="example.intent.action.IntentService" /> </intent-filter></service><receiver android:name=".Receiver"> <intent-filter> <action android:name="example.intent.action.Receiver" /> </intent-filter></receiver>
  28. 28. Intent Filters Activity Action Service Receiver
  29. 29. Intent Filters Activity Action Service Receiver AndroidManifeset.xml <intent-filter> <action android:name="any.action.you.want" /> </intent-filter>
  30. 30. Services
  31. 31. Services Run in background Don’t have UI Run on the UI thread
  32. 32. Services Run in background UI Activity Don’t have UI startService(); stopService(); Run on the UI thread Service
  33. 33. Service Lifecycle 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.
  34. 34. Service Example 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"); } }
  35. 35. Declaring the Service Called via its class name <service android:name=".ServiceDemo"></service> Called via action <service android:name=".IntentService"> <intent-filter> <action android:name="example.intent.action.IntentService" /> </intent-filter> </service>
  36. 36. Broadcast Receivers
  37. 37. Broadcast Receivers Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o.
  38. 38. Broadcast Receivers Intent based publish-subscribe mechanism Listening system events: incoming calls, SMS messages a.o. Register for certain intents Get notified when intent happens
  39. 39. Broadcast Receiver Example 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() ); } }
  40. 40. Registering the Broadcast Receiver Declaring it in AndroidManifest.xml <receiver android:name=".ReceiverDemo"> <intent-filter> <action android:name="example.intent.action.Receiver" /> </intent-filter> </receiver>
  41. 41. Registering the Broadcast Receiver Registering Programmatically @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); }
  42. 42. That’s all!Questions
  43. 43. THANK YOUEugeniu Arbuleac Andrei Catinean@arbuleac @electrycarbuleac.ev@gmail.com andrei.catinean@gmail.com

×