Presentation is loading. Please wait.

Presentation is loading. Please wait.

Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.

Similar presentations


Presentation on theme: "Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated."— Presentation transcript:

1 Developing Android Services

2 Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated tasks in a service Having an activity and a service to communicate 2

3 Android Service Application component that runs (in background) without needing to interact with the user E.g., playing background music, logging geographical coordinates => no need to present a UI to the user Q: Other components of Android apps? 3

4 Creating Service As a subclass of android.app.Service Override key framework methods including: int onStartCommand(Intent i, int flags, int startId) IBind onBind(Intent i) void onCreate() void onDestroy() Declare the service in AndroidManifest.xml, e.g., 4

5 5 int onStartCommand(Intent intent, int flags, int startId) Called when the service is started by startService() startId: unique Id generated by system flags: additional info supplied by the system Return START_STICKY to make the service continue to run until explicitly stopped ( START_NOT_STICKY). call stopSelf() or stopService() to stop the service IBind onBind(Intent intent) Enable to bind an activity (by calling bindService()) to a service so as to access public elements such as fields and methods void onCreate() Called when the service is first created void onDestroy() Called when the service is stopped by calling stopService()

6 Starting/Stopping Service startService(new Intent(getBaseContext(), MyService.class)); startService(new Intent("edu.utep.cs.cs4330.MyService"));  Need to make the service available to other applications *Service runs on the same thread that started it (e.g., UI thread). stopService(new Intent(getBaseContext(), MyService.class)); 6

7 Long-Running Tasks In Service Perform in a new thread, but why? How? Thread class AsyncTask class void onPreExecute() Invoked on the UI thread before doInBackground void doInBackground(Params...) Invoked on the background thread after onPreExecute void onProgressUpdate(Progress...) Invoked on UI thread after a call to publishProgress(Progress...) void onPostExecute(Result) Invoked on the UI thread after the background task finishes 7

8 Repeated Tasks In Service Performing repeated tasks in a service, e.g., alarm clock service that runs persistently in background Use the Timer class providing several methods such as void scheduleAtFixedRate(TimerTask task, long delay, long period) 8 new Timer().scheduleAtFixedRate( new TimerTask() { public void run() { /*... */ } }, 0, // no delay 1000); // at every sec To cancel, call the Timer.cancel() method.

9 IntentService Subclass of Service to execute an asynchronous task on a new thread Automatically stop the service when completed (i.e., no need to call the stopSelf() method) Just need to override: protected void onHandleIntent(Intent i) that performs a task on a new thread Also need to add the service to manifest, e.g., 9

10 Invoking Activity From Service Use the BroadCastReceiver 10 In service: Intent broadcastIntent = new Intent(); broadcastIntent.setAction("MyAction"); getBaseContext().sendBroadcast(broadcastIntent); In activity: IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("MyAction"); registerReceiver(new BroadcastReceiver() { public void onReceive(Context context, Intent intent) {... } }, intentFilter);

11 Binding Activities To Services For an activity to access public members of a service Use the bindService() method in Activities Override the onBind() method in Services Refer to API document of the Service class 11

12 In-Class (Pair): Music app 12 Create an app that, given an URI of a music file, plays the music as a background service. When the playback of the music file is completed, the service should notify to the main activity to display a toast message indicating the completion of the playback. (Hint: use the MediaPlayer class) MediaPlayer public static MediaPlayer create(Context, Uri) public void start() public void setOnCompletionListener( MediaPlayer.OnCompletionListener)


Download ppt "Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated."

Similar presentations


Ads by Google