Transcript PPT

CSE2102 Introduction to
Software Engineering
Lab 2
Sept/4/2013
Today
1.
App Fundamentals and Skeleton (last week)
1.
2.
Activity (today)
1.
2.
3.
3.
Function
Lifecycle
Coding Assignment
Services (today)
1.
2.
3.
4.
5.
6.
7.
8.
9.
Architecture Diagram (today)
Function
Lifecycle
Coding Assignment
Content Providers (week 37/3)
Intent and Intent Filters (week 38/4)
Processes and Threads (week 39/5)
Permission (week 40/6)
App Widgets (week 40/6)
Android Manifests (week 40/6)
Architecture Diagram
http://developer.android.com/images/system-architecture.jpg
What are the functions of an activity?
1.
2.
3.
4.
Interact in most case with user
Usually applications have several activities
Each is implemented as a subclass of base Activity class
Creates the window to place UI
1.
5.
setContentView(R.layout.home_activity);
Methods
1.
2.
3.
4.
5.
6.
7.
protected void onCreate(Bundle savedInstanceState); // initialize
protected void onStart(); //activity visible
protected void onRestart(); //before activity becomes visible
protected void onResume(); // activity visible after being paused
protected void onPause(); // another activity becomes visible
protected void onStop(); // activity not visible
protected void onDestroy(); // cleanup and destroy activity
What is the lifecycle of an 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.
2.
3.
4.
5.
public void onCreate() // initialize
public void onStartCommand() // started
public void onBind() // activity to service communication
public void onDestroy() // cleanup resources
public void stopService() // stop service
Service - Function
Services must be declared in AndroidManifest.xml
<service
android:name=“path.to.MyAppService"
android:label="@string/service_name">
</service>
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/services.html
• Professional Android 4 Application
Development (Reto Meier, 2012)