Running in Background Flashcards
The main thread
Independent path of execution in a running program
Code is executed line by line
App runs on Java thread called “main” or “UI thread”
Draws UI on the screen
Responds to user actions by handling UI events
Two rules for Android threads
- Do not block the UI thread. The Main thread must be fast
Complete all work in less than 16 ms for each screen
Run slow non-UI work on a non-UI thread
If the UI waits too long for an operation to finish, it becomes unresponsive
The framework shows an Application Not Responding (ANR) dialog
- Do not access the Android UI toolkit from outside
the UI thread
Do UI work only on the UI thread
Background threads
Execute long running tasks on a background thread
AsyncTask
The Loader Framework
Services
AsyncTask
Use AsyncTask to implement basic background tasks
Override two methods:
doInBackground()—runs on a background thread
All the work to happen in the background
onPostExecute()—runs on main thread when work done
Process results
Publish results to the UI
onPreExecute()
Runs on the main thread
Sets up the task
onProgressUpdate()
Runs on the main thread receives calls from
publishProgress() from background thread
Limitations of AsyncTask
When device configuration changes, Activity is destroyed
AsyncTask cannot connect to Activity anymore
New AsyncTask created for every config change
Old AsyncTasks stay around
App may run out of memory or crash
When to use AsyncTask
Short or interruptible tasks
Tasks that do not need to report back to UI or user
Lower priority tasks that can be left unfinished
Use AsyncTaskLoader otherwise
Loader
Provides asynchronous loading of data from a content provider or other data source
BENEFITS:
- Reconnects to Activity after configuration change. LoaderManager handles configuration changes for you
- Callbacks implemented in Activity
- Can monitor changes in data source and deliver new data
Many types of loaders available:
CursorLoader - data from a ContentProvider
Loader or AsyncTaskLoader - to load data from some other source
LoaderManager
Manages one or more Loader instances
getSupportLoaderManager().initLoader(0, null, this);
To start loading data from a loader, call either initLoader() or restartLoader(). The system automatically determines whether a loader with the same integer ID already exists and will either create a new loader or reuse an existing loader.
AsyncTaskLoader
A Loader subclass, used to load data from a data source.
DIFERENCESE with ASYNTASK :
doInBackground() loadInBackground()
onPostExecute() onLoadFinished()
Steps for AsyncTaskLoader subclass
1) Subclass AsyncTaskLoader
2) Implement constructor - to call super()
3) loadInBackground()
4) onStartLoading() - When restartLoader() or initLoader() is called, the LoaderManager invokes the onStartLoading() callback
Implement loader callbacks in Activity
onCreateLoader()
onLoadFinished()
onLoaderReset()
1) onCreateLoader() — Create and return a new Loader for the given ID: @Override public Loader> onCreateLoader(int id, Bundle args) { return new StringListLoader(this,args.getString("queryString")); }
2)onLoadFinished() — Called when a previously created loader has finished its load. Results of loadInBackground() are passed to onLoadFinished() where you can display them
3) onLoaderReset() — Called when a previously created loader is being reset making its data unavailable
Only called when loader is destroyed
Leave blank most of the time
broadcast
— Broadcast are send using Implicit Intents : sendBroadcast() method—asynchronous
sendOrderedBroadcast()—synchronously
- –Can be received by any application registered for the intent
- – Used to notify all apps of an event
broadcast receiver
——Listens for incoming intents sent by sendBroadcast().(.from In the background)
—–Intents can be sent
> By the system, when an event occurs that might change the behavior of an app
> By another application, including your own
Broadcast receiver always responds
Responds even when your app is closed
Independent from any activity
When a broadcast intent is received and delivered to onReceive(), it has 5 seconds to execute, and then the receiver is destroyed
Steps for creating a broadcast receiver
Subclass BroadcastReceiver
Implement onReceive() method
Register to receive broadcast
Statically, in AndroidManifest
Dynamically, with registerReceiver()
registerReceiver(mReceiver, mIntentFilter)