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"> 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">
Download presentation
Presentation is loading. Please wait.
Published byJessica Shepherd Modified over 9 years ago
1
lec 06 AsyncTask Local Services IntentService Broadcast Receivers
2
In this example, MyTask extends AsyncTask 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
3
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" > 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
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) Used within the same app Across apps
6
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
7
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
8
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
9
Broadcast Reievers >You can't trigger the system Broadcasts >You can define intent filters and also your own broadcast types Broadcast Receivers
10
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
11
Broadcast Receivers 1/ extend BroadcastReceiver 2/ override onRecieve() http://developer.android.com/reference/android/content/Intent.html
12
Broadcast Receivers 1/ extend BroadcastReceiver 2/ override onRecieve() http://developer.android.com/reference/android/content/Intent.html
13
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 :
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.