Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.

Similar presentations


Presentation on theme: "COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen."— Presentation transcript:

1 COMP 365 Android Development

2  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen  Promotes multitasking, as multiple activities can be conducted while services run

3  A component can also interact with a service to perform interprocess communication (IPC)  A service might download information for an activity, perform file input/output (I/O), or play music entirely from the background

4  A service can take two forms:  Started  Bound

5  When an activity or other application component starts a service using startService(), it is considered to be started and can run in the background indefinitely even if the component that started it is stopped. These services usually perform one function and stop themselves.

6  A service is considered to be bound when an application component binds it using bindService(). This allows a client-server interaction between the service and the application component, but once the application bound to the service ends, the service is destroyed.

7  In order to create a service you must extend the Service class or a subclass of the Service class. You will need to override the following abstract classes:  onStartCommand()  onBind()  onCreate()  onDestroy()

8  Android calls this method when the service is started using the startService() method and the service will run indefinitely until stopped with the stopSelf() or the stopService() methods.

9  This method is called when an application component asks to be bound to the service by calling bindService(). If you choose to let your service be bound to an application component, it must be have an interface that returns an IBinder.

10  onCreate() - Called when the service is first created.  onDestroy() - Called when the service is being destroyed.

11 ......

12  To create a service, you can extend either the Service or the IntentService class.  The IntentService class has many advantages as it creates default functionality for required methods like onStartCommand(), onHandleIntent(), stopSelf(), and onBind().

13  There are two kinds of broadcasts:  Normal broadcasts (sent using Context.sendBroadcast) are asynchronous; meaning that broadcasts are run and delivered in an undefined order.  Ordered broadcasts (sent using Context.sendOrderedBroadcast) which means broadcasts are sent one at a time.

14  You can dynamically register an instance of this classs with Context.registerReceiver() or statically publish an implementation through the tag in your manifest file  only valid for the duration of the call to onReceive (Context, Intent), after that Broacast Receivers are no longer considered active

15 public class MyBroadcastReceiver extends BroadcastReciever { @Override public void onReceive (Context context, Intent intent) { //TODO Auto-generated method stub //Extract data included in the Intent CharSequence intentData = intent.getCharSequenceExtra(“message”); Toast.makeText(content, “Received the global broadcast message: “+intentData,Toast.LENGTH_LONG).show(); }

16 //name of Android program //name of the intent


Download ppt "COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen."

Similar presentations


Ads by Google