What are the functions of an activity? 1.Interact in most case with user 2.Usually applications have several activities 3.Each is implemented as a subclass of base Activity class 4.Creates the window to place UI 1.setContentView(R.layout.home_activity); 5.Methods 1.protected void onCreate(Bundle savedInstanceState); // initialize 2.protected void onStart(); //activity visible 3.protected void onRestart(); //before activity becomes visible 4.protected void onResume(); // activity visible after being paused 5.protected void onPause(); // another activity becomes visible 6.protected void onStop(); // activity not visible 7.protected void onDestroy(); // cleanup and destroy activity
Coding Assignment (5 min) 1.Start Android Developer Tools 2.Create a new Project with one Activity 3.Define all six methods in that activity
What are the functions of a service? 1.Runs in the background – what does it mean? 2.Each is implemented as a subclass of base Service class 3.Examples – Music, TCP, REST, and other long running operations… 4.Methods 1.public void onCreate() // initialize 2.public void onStartCommand() // started 3.public void onBind() // activity to service communication 4.public void onDestroy() // cleanup resources 5.public void stopService() // stop service
Service - Function Services must be declared in AndroidManifest.xml Remember to update the string resource file!
What is the service lifecycle? 1.Does a Service have Lifecycle? 1.Arguably no, since there is no interaction with the user, internally yes. 2.Who can start a service? 1.Services, Receivers, and Activities 3.How do you start a service? – Intent i= new Intent(getApplicationContext, MyAppService.class); – i.putExtra(“key", “value"); – getApplicationContext.startService(i);
Coding Assignment (5 min) Create a Service class Add 5 methods Start that Service through the Activity and pass a key/value pair Print out value/pair using activity on console
Questions What are the four layers of the Android Architecture Stack? What is the function of an activity? What are the four states of an activity? What are the six methods that change the state of an activity? What is a Service? What are the 5 methods that implement a service? What is the difference between Activity and Service? Who can start service?
References http://developer.android.com/guide/compone nts/activities.html http://developer.android.com/guide/compone nts/activities.html http://developer.android.com/guide/compone nts/services.html http://developer.android.com/guide/compone nts/services.html Professional Android 4 Application Development (Reto Meier, 2012)