1 / 53

Android Programming Lecture 9

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

sedwards
Download Presentation

Android Programming Lecture 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Programming Lecture 9 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 • Youtubetutorial • 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: http://developer.android.com/guide/components/tasks-and-back-stack.html

  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. 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.

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Service

  17. 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

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

  19. 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

  20. 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); ... }

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

  22. 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

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

  24. 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

  25. Broadcast Receiver

  26. 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.

  27. 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

  28. 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); . . . }

More Related