comp 365 android development. perform operations in the background services do not have a user...

16
Services and Broadcast Receivers COMP 365 Android Development

Upload: rhoda-bradford

Post on 29-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

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

Services and Broadcast Receivers

COMP 365 Android Development

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

Services

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

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

Services cont.

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

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

Service Forms

A service can take two forms: Started Bound

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

Started Services

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.

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

Bound Service

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.

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

Creating Services

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()

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

onStartCommand()

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.

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

onBind()

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.

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

onCreate() and onDestroy()

onCreate() - Called when the service is first created.

onDestroy() - Called when the service is being destroyed.

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

Service in the Manifest File

<manifest ... >  ...  <application ... >      <service android:name=".ExampleService”/>      ...  </application></manifest>

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

Creating Services

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

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

Broadcast Receivers

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.

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

Broadcast Receivers

You can dynamically register an instance of this classs with Context.registerReceiver() or statically publish an implementation through the <receiver> 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

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

Broadcast Receivers in Code

public class MyBroadcastReceiver extends BroadcastReciever {

@Overridepublic void onReceive (Context context, Intent intent) {

//TODO Auto-generated method stub

//Extract data included in the IntentCharSequence intentData =

intent.getCharSequenceExtra(“message”);Toast.makeText(content, “Received the global

broadcast message: “+intentData,Toast.LENGTH_LONG).show();

}}

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

Broadcast Receivers in the Manifest File

<manifest ...>  <application ...><receiver android:name=”MyBroadcastReciever”> //name of Android program<intent-filter>//name of the intent <action android:name=”com.androiddev.android.A_CUSTOM_INTENT”></action></intent-filter></receiver></application></manifest>