530 likes | 543 Views
Android Programming Lecture 9. Service and Broadcast Receiver. Sample App: Music Player. 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
E N D
Android Programming Lecture 9 Service and Broadcast Receiver
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
Sample App (2/7) • Left side is a menu for different views • The activity has a few fragments (sub-activity)for navigation • Youtubetutorial • Click on My Library
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
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: http://developer.android.com/guide/components/tasks-and-back-stack.html
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
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
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
App Components 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.
Activities • Provides User Interface • Usually represents a Single Screen • Can contain one/more Views • Extendsthe Activity Base class App Components Application= Set of Android Components
Activities • Provides User Interface • Usually represents a Single Screen • Can contain one/more Views • Extendsthe Activity Base class • Services • NoUser Interface • Runs in Backgroundto handle long running operations App Components Application= Set of Android Components Connected with Intents Faceless task that runs in the background for long-running operations
Activities • Provides User Interface • Usually represents a Single Screen • Can contain one/more Views • Extendsthe Activity Base class App Components Application= Set of Android Components • 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 Connected with Intents
Activities • Provides User Interface • Usually represents a Single Screen • Can contain one/more Views • Extendsthe Activity Base class App Components Application= Set of Android Components • Enable applications to share data • Retrieve icon, songs from other music app • Retrieve contact books to recommend music to friends Connected with Intents • Content Provider • Makes application data available to other apps • Data stored in SQLite database
Activities • Provides User Interface • Usually represents a Single Screen • Can contain one/more Views • Extendsthe Activity Base class • Services • NoUser Interface • Runs in Backgroundto handle long running operations App Components Application= Set of Android Components • Intent/Broadcast Receiver • Receives and Reacts to broadcast Intents • No UI but can start an Activity Connected with Intents • Content Provider • Makes application data available to other apps • Data stored in SQLite database
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 email • Download a html from Internet to display • Why Service? • Put the time-consuming job in the background
What a good should provide? • Desirable Functions • Attractive UI • Good user experience • Quick response and reliable (never crash) • Secure and safe to use
Step 1: Create custom class extended from Service class Using Service Reason to use custom class We need to override the callback functions to specify the behavior of app
Step 2: Use the custom in the Activity to launch service Using Service publicclassMainActivityextends Activity { ... // Inside the method where you need to launch the service // Create the service object MyServicemyService = newMyService(); // Create the Intent for launching the service Intent intent =newIntent(MainActivity.this, MediaPlayerService.class); // Call startService() method to start the service MainActivity.this.startService(intent); ... ... // Inside the method where you need to launch the service MyServicemyService = newMyService(); // Create the Intent for launching the service Intentintent = newIntent(MainActivity.this, MediaPlayerService.class); // Call startService() method to start the service MainActivity.this.stopService(intent); ... }
Step 3: Register the Service class in AndroidManifest.xml Using Service <manifest . . . > . . . <application . . .> <service android:name=".MyService" > </service> . . . </application> </manifest>
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
MainActivity.this.startService(intent); startService(intent) MainActivity.this.stopService(intent);
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) http://developer.android.com/guide/components/bound-services.html
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.
Step 1: Create custom class extended from the Broadcast Receiver class Using Broadcast Receiver publicclassMyRecieverextendsBroadcastReceiver { // callback function called when the broadcast event is captured @Override publicvoidonReceive(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
Step 2: Register as the broadcast receiver in Java or declare in AndroidManefist.xml Using Broadcast Receiver publicclassMainActivityextends Activity { . . . // Create an IntentFilter object (in Java) IntentFilterfilter = newIntentFilter(); // Specify the Action of the Intent Filter filter.addAction(Intent.ACTION_CAMERA_BUTTON); // Create the receiver object reciever= newMyReciever(MainActivity.this); // Register the receiver MainActivity.this.registerReceiver(reciever, filter); . . . . . . // Unregister Register the receiver MainActivity.this.unregisterReceiver(reciever); . . . }