Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Programming Lecture 9

Similar presentations


Presentation on theme: "Android Programming Lecture 9"— Presentation transcript:

1 Android Programming Lecture 9
Testing notes Service and Broadcast Receiver

2 Sample App: Music Player

3 Sample App (1/7) Click on the icon and get the startup
Launcher activity is started by the sending of a special intent – the Launcher intent Application Manifest declares which Java class accepts the launcher intent

4 Sample App (2/7) Left side is a menu for different views
The activity has a few fragments (sub- activity) for navigation Youtube tutorial Click on My Library

5 Sample App (3/7) Click Albums to get into the Albums sub-activity (fragment) Tab UI can be easily achieved using Tab Host ViewGroup Click an album name

6 Sample App (4/7) Shows the album in a different activity
Click on a track for immediate playback Click adds it to the task bar Launches a service to play Click on the back button to return to the previous activity Android maintains a stack of activities for “back” Refer to:

7 Sample App (5/7) Playback continues, even if the user navigates away
Even if the user locks the phone, as long as the user does not shut down the app

8 Sample App (6/7) Service can be controlled by a widget when locked
Also from the notification bar By clicking the notification bar, we can get back to the music player

9 Sample App (7/7) Service interrupted when a call come
A Broadcast Event that all the (registered) apps would receive! Resumes after the call hangs up

10

11 App Components Activity Intent Service ContentProvider
Application= Set of Android Components Intent Activity UI Component typically corresponding to one screen. BroadcastReceiver Responds to notifications or status changes. Can wake up your process. Service Faceless task that runs in the background. ContentProvider Enable applications to share data.

12 App Components Activities Application= Set of Android Components
Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class

13 Connected with Intents
App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Services No User Interface Runs in Background to handle long running operations Connected with Intents Faceless task that runs in the background for long-running operations

14 Intent/Broadcast Receiver
App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Connected with Intents Responds to notifications /status changes/events Incoming phone call Battery low Alarm Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity

15 Connected with Intents
App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Connected with Intents Content Provider Makes application data available to other apps Data stored in SQLite database Enable applications to share data Retrieve icon, songs from other music app Retrieve contact books to recommend music to friends

16 Intent/Broadcast Receiver
App Components Application= Set of Android Components Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extends the Activity Base class Services No User Interface Runs in Background to handle long running operations Connected with Intents Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity Content Provider Makes application data available to other apps Data stored in SQLite database

17 Service

18 Service An application component that can perform long- running operations in the background and does not provide a user interface Load music to play Download, decode and show an Download a html from Internet to display Why Service? Put the time-consuming job in the background

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40 What a good should provide?
Desirable Functions Attractive UI Good user experience Quick response and reliable (never crash) Secure and safe to use

41 Using Service Step 1: Create custom class extended from Service class
Reason to use custom class We need to override the callback functions to specify the behavior of app

42 Using Service Step 2: Use the custom in the Activity to launch service
public class MainActivity extends Activity { ... // Inside the method where you need to launch the service // Create the service object MyService myService = new MyService(); // Create the Intent for launching the service Intent intent = new Intent(MainActivity.this, MediaPlayerService.class); // Call startService() method to start the service MainActivity.this.startService(intent); MainActivity.this.stopService(intent); }

43 Using Service Step 3: Register the Service class in AndroidManifest.xml <manifest > . . . <application . . .> <service android:name=".MyService" > </service> </application> </manifest>

44 Lifecycle of Service Android provides different callback functions at different states of a service Developer can override the methods to specify the app behaviors like what we do in Activity Android provides two type of services: Started services Bound services

45 MainActivity.this.startService(intent);
MainActivity.this.stopService(intent);

46 Bound Service A bound service offers a client-server interface that allows components to interact with the service Using custom class Using a messenger Using AIDL (Android Interface Definition Language)

47 Broadcast Receiver

48 Broadcast Receiver Responds to system-wide Broadcast announcements.
Many broadcasts originate from the system—for example, screen has turned off, the battery is low, or a picture was captured or an SMS is received. Applications can also initiate broadcasts. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs.

49 Using Broadcast Receiver
Step 1: Create custom class extended from the Broadcast Receiver class public class MyReciever extends BroadcastReceiver { // callback function called when the broadcast event is captured @Override public void onReceive(Context context, Intent intent) { // Override the method } Reason to use custom class We need to override the callback functions to specify the behavior of app

50 Using Broadcast Receiver
Step 2: Register as the broadcast receiver in Java or declare in AndroidManefist.xml public class MainActivity extends Activity { . . . // Create an IntentFilter object (in Java) IntentFilter filter = new IntentFilter(); // Specify the Action of the Intent Filter filter.addAction(Intent.ACTION_CAMERA_BUTTON); // Create the receiver object reciever = new MyReciever(MainActivity.this); // Register the receiver MainActivity.this.registerReceiver(reciever, filter); // Unregister Register the receiver MainActivity.this.unregisterReceiver(reciever); }

51 Using Broadcast Receiver
Step 2: Register as the broadcast receiver in Java or declare in AndroidManefist.xml <manifest > . . . <application . . .> <receiver android:name=".MyReceiver" > </receiver> </application> </manifest>

52 Summary What is an Activity? What is a Service?
What is a Broadcast Receiver? What is a Content Provider? How to enable a service inside an activity? How to enable a broadcast receiver inside an activity?

53 Recommended Reading Vogella tutorial on Services, BroadcastReceiver and NotificationManager Android Developer Site: Services: ces.html Content Provider: ontent-providers.html


Download ppt "Android Programming Lecture 9"

Similar presentations


Ads by Google