lec 06 Async Task Local Services Intent Service

  • Slides: 12
Download presentation
lec 06 Async. Task Local Services Intent. Service Broadcast Receivers

lec 06 Async. Task Local Services Intent. Service Broadcast Receivers

Async. Task In this example, My. Task extends Async. Task<String, Integer, Bitmap> Params: 1/

Async. Task In this example, My. Task extends Async. Task<String, Integer, Bitmap> Params: 1/ (String) passed into do. In. Background(String. . . str. Urls) 2/ (Integer) The value passed into on. Progress. Update(Integer. . . int. Updates) 3/ (Bitmap) returned from do. In. Background() and used as input to on. Post. Execute(Bitmap bmp)

Service An Activity can start a Service via either the start. Service() method or

Service An Activity can start a Service via either the start. Service() method or stop the service via the stop. Service() 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. <service android: name=". Word. Service" 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 Intent. Service for this.

Services >A first-class component in Android that must be declared in the manifest >Has

Services >A first-class component in Android that must be declared in 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) Used within the same app Bound (remote) Across apps

Service on. Start. Command() returns a flag which tells the OS that the service

Service on. Start. Command() 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 on. Start. Command() 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 on. Start. Command().

Intent. Service An Intent. Service: 1/ running in a separate background thread 2/ once

Intent. Service An Intent. Service: 1/ running in a separate background thread 2/ once done, it terminates itself. The Intent. Service class offers the on. Handle. Intent() method which will be asynchronously called by the Android system. This is similar to do. In. Background() for Async. Task.

Bound and Advanced Services If the Activity want to interact with the Activity it

Bound and Advanced Services If the Activity want to interact with the Activity it can use the bind. Service() method of the service. This requires an Service. Connection 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.

Broadcast Receivers Broadcast Reievers >You can't trigger the system Broadcasts >You can define intent

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

Broadcast Receivers (aka Receivers) >You must specify the intent filter in the Android Manifest

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_CHANGED android. intent. action. AIRPLANE_MODE ACTION_BATTERY_LOW android. intent. action. BATTERY_LOW ACTION_BOOT_COMPLETED android. intent. action. BOOT_COMPLETED ACTION_TIMEZONE_CHANGED android. intent. action. TIMEZONE_CHANGED

Broadcast Receivers 1/ extend Broadcast. Receiver 2/ override on. Recieve() http: //developer. android. com/reference/android/content/Intent.

Broadcast Receivers 1/ extend Broadcast. Receiver 2/ override on. Recieve() http: //developer. android. com/reference/android/content/Intent. html <receiver android: name=". My. Phone. Receiver" > <intent-filter> <action android: name="android. provider. Telephony. SMS_RECEIVED" > </action> </intent-filter> </receiver> <receiver android: name=". Silly. Message. Receiver" > <intent-filter> <action android: name="edu. uchicago. cs. gerber. sillymessage" /> </intent-filter> </receiver>

Broadcast Receivers 1/ extend Broadcast. Receiver 2/ override on. Recieve() http: //developer. android. com/reference/android/content/Intent.

Broadcast Receivers 1/ extend Broadcast. Receiver 2/ override on. Recieve() http: //developer. android. com/reference/android/content/Intent. html <receiver android: name=". My. Phone. Receiver" > <intent-filter> <action android: name="android. provider. Telephony. SMS_RECEIVED" > </action> </intent-filter> </receiver> <receiver android: name=". Silly. Message. Receiver" > <intent-filter> <action android: name="edu. uchicago. cs. gerber. sillymessage" /> </intent-filter> </receiver>

Student presentations: A: Async. Task: How to get/parse JSON data (weather data) from RESTful

Student presentations: A: Async. Task: How to get/parse JSON data (weather data) from RESTful web service (with recipe) B: Media. Store: 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)