1 / 12

lec 06

lec 06. AsyncTask Local Services IntentService Broadcast Receivers. AsyncTask. In this example, MyTask extends AsyncTask<String, Integer, Bitmap> Params: 1/ (String) passed into doInBackground(String... strUrls)

gholson
Télécharger la présentation

lec 06

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. lec 06 AsyncTask Local Services IntentService Broadcast Receivers

  2. AsyncTask 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)

  3. Service 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. <service android: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.

  4. 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) Across apps Used within the same app

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

  6. IntentService 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.

  7. Bound and Advanced Services 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.

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

  9. 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

  10. Broadcast Receivers 1/ extend BroadcastReceiver 2/ override onRecieve() http://developer.android.com/reference/android/content/Intent.html <receiverandroid:name=".MyPhoneReceiver"> <intent-filter> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"> </action> </intent-filter> </receiver> <receiverandroid:name=".SillyMessageReceiver"> <intent-filter> <actionandroid:name="edu.uchicago.cs.gerber.sillymessage"/> </intent-filter> </receiver>

  11. Broadcast Receivers 1/ extend BroadcastReceiver 2/ override onRecieve() http://developer.android.com/reference/android/content/Intent.html <receiverandroid:name=".MyPhoneReceiver"> <intent-filter> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"> </action> </intent-filter> </receiver> <receiverandroid:name=".SillyMessageReceiver"> <intent-filter> <actionandroid:name="edu.uchicago.cs.gerber.sillymessage"/> </intent-filter> </receiver>

  12. Student presentations: 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)

More Related