lec 06 asynctask local services intentservice broadcast receivers

12
lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Upload: jessica-shepherd

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

lec 06

AsyncTask

Local Services

IntentService

Broadcast Receivers

Page 2: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

In this example, MyTask extends AsyncTask<String, Integer, Bitmap>

Params: 1/ (String) passed into doInBackground(String... strUrls)

2/ (Integer) The value passed into onProgressUpdate(Integer... intUpdates)

3/ (Bitmap) returned from doInBackground() and used as input to onPostExecute(Bitmap bmp)

AsyncTask

Page 3: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

An Activity can start a Service via either the startService() method or stop the service via the stopService() method.

A Service will not automatically run in its own thread, though it can. You can specify that your Service runs in a separate process via the android:process=":process_description" attribute.The colon means it's private to the app, if the colon is missing then it's global to everything.

<serviceandroid:name=".WordService"android:process=":my_process" android:icon="@drawable/icon"android:label="@string/service_name">

</service>

Without the process attribute, the service runs in the main thread of the hosting process. Therefore you should run performance intensive tasks in the background. You can use IntentService for this.

Service

Page 4: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Services

>A first-class component in Android that must be declared in the the manifest>Has no graphical user-interface>Used for long-running background tasks; typically networking, I/O, interaction with content providers, multimedia

>There are two classes of Services;

Service

Started (local) Bound (remote)

Used within the same app

Across apps

Page 5: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

onStartCommand() returns a flag which tells the OS that the service is either sticky or not_sticky.

Both codes are only relevant when the phone runs out of memory and kills the service before it finishes executing.

START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent.

START_NOT_STICKY tells the OS to not bother recreating the service again. There is also a third code

START_REDELIVER_INTENT that tells the OS to recreate the service AND redeliver the same intent to onStartCommand().

Service

Page 6: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

An IntentService:

1/ running in a separate background thread 2/ once done, it terminates itself.

The IntentService class offers the onHandleIntent() method which will be asynchronously called by the Android system. This is similar to doInBackground() for AsyncTask.

IntentService

Page 7: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

If the Activity want to interact with the Activity it can use the bindService() method of the service. This requires an ServiceConnection object which allows to connect to the Service and which return a IBinder object. This IBinder object can be used by the activity to communicate with the Service.

Android Interface Definition Language (AIDL) for remote services

Will cover in more detail in a future lecture.

Bound and Advanced Services

Page 8: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Broadcast Reievers

>You can't trigger the system Broadcasts>You can define intent filters and also your own broadcast types

Broadcast Receivers

Page 9: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Broadcast Receivers (aka Receivers)

>You must specify the intent filter in the Android Manifest file (or programmatically).

The first is the constant defined as a String, the second is the value. Examples:

ACTION_AIRPLANE_MODE_CHANGEDandroid.intent.action.AIRPLANE_MODE

ACTION_BATTERY_LOWandroid.intent.action.BATTERY_LOW

ACTION_BOOT_COMPLETEDandroid.intent.action.BOOT_COMPLETED

ACTION_TIMEZONE_CHANGEDandroid.intent.action.TIMEZONE_CHANGED

Page 10: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Broadcast Receivers

1/ extend BroadcastReceiver

2/ override onRecieve()

http://developer.android.com/reference/android/content/Intent.html

<receiver android:name=".MyPhoneReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" > </action> </intent-filter> </receiver> <receiver android:name=".SillyMessageReceiver" > <intent-filter> <action android:name="edu.uchicago.cs.gerber.sillymessage" /> </intent-filter> </receiver>

Page 11: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

Broadcast Receivers

1/ extend BroadcastReceiver

2/ override onRecieve()

http://developer.android.com/reference/android/content/Intent.html

<receiver android:name=".MyPhoneReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" > </action> </intent-filter> </receiver> <receiver android:name=".SillyMessageReceiver" > <intent-filter> <action android:name="edu.uchicago.cs.gerber.sillymessage" /> </intent-filter> </receiver>

Page 12: Lec 06 AsyncTask Local Services IntentService Broadcast Receivers

A: AsyncTask: How to get/parse JSON data (weather data) from RESTful web service (with recipe)

B: MediaStore: Taking pictures and videos and storing them for use in an app (not the standard gallery) (with recipe)

C: Voice Recognition: How to use voice recognition inside an app (with recipe)

D: Tracking locations (using GPS) for a jog or a race. Data capture so that I can map where I went and how far I traveled, avg speed (with recipe)

Student presentations: