Running in Background Flashcards

1
Q

The main thread

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Two rules for Android threads

A
  1. 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

  1. Do not access the Android UI toolkit from outside
    the UI thread
    Do UI work only on the UI thread
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Background threads

A

Execute long running tasks on a background thread

AsyncTask
The Loader Framework
Services

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

AsyncTask

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Limitations of AsyncTask

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When to use AsyncTask

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Loader

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

LoaderManager

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

AsyncTaskLoader

A

A Loader subclass, used to load data from a data source.

DIFERENCESE with ASYNTASK :
doInBackground() loadInBackground()
onPostExecute() onLoadFinished()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Steps for AsyncTaskLoader subclass

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Implement loader callbacks in Activity

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

broadcast

A

— 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

broadcast receiver

A

——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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Broadcast receiver always responds

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Steps for creating a broadcast receiver

A

Subclass BroadcastReceiver
Implement onReceive() method
Register to receive broadcast
Statically, in AndroidManifest
Dynamically, with registerReceiver()
registerReceiver(mReceiver, mIntentFilter)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Controlling permission sender

Other apps can send broadcasts to your receiver—use permissions to control this

A
void sendBroadcast (Intent intent, 
                    String receiverPermission)

Receivers must request permission with in AndroidManifest.xml

17
Q

Controlling permission receiver

Other apps can respond to broadcast your app sends

A

registerReceiver(BroadcastReceiver,
IntentFilter, String, android.os.Handler)
or in tag

Senders must request permission with in AndroidManifest.xml

18
Q

Local Broadcast Manager

A

For broadcasts only in your app
No security issues since no cross-app communication

LocalBroadcastManager.sendBroadcast()
LocalBroadcastManager.registerReceiver()

19
Q

Service

A

A Service is an application component that can perform long-running operations in the background and does not provide a user interface

20
Q

What are services good for?

A

Network transactions
Play music
Perform file I/O
Interact with a content provider

21
Q

Characteristics of services

A

Started with an Intent
Can stay running when user switches applications
Lifecycle—which you must manage
Other apps can use the service—manage permissions
Runs in the main thread of its hosting process

22
Q

Forms of services: started

A

Started with startService()
Runs indefinitely until it stops itself
Usually does not update the UI

23
Q

Forms of services: bound

A

Offers a client-server interface that allows components to interact with the service
Clients send requests and get results
Started with bindService() with an explicit Intent
Ends when all clients unbind

Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

24
Q

Creating a service

A

Manage permissions

Subclass IntentService or Service class
Implement lifecycle methods
Start service from activity
Make sure service is stoppable

25
Q

Stopping a service
A started service
A bound service
IntentService

A

A started service must manage its own lifecycle
If not stopped, will keep running and consuming resources
The service must stop itself by calling stopSelf()
Another component can stop it by calling stopService()

Bound service is destroyed when all clients unbound

IntentService is destroyed after onHandleIntent() returns

26
Q

IntentService

A

Simple service with simplified lifecycle
Uses worker threads to fulfill requests
Stops itself when done

Ideal for one long task on a single background thread

27
Q

IntentService Limitations

A

Cannot interact with the UI
Can only run one request at a time
Cannot be interrupted

28
Q

What is Job Scheduler

A

Used for intelligent scheduling of background tasks

29
Q

Job Scheduler components

A

JobService—Service class where the task is initiated
JobInfo—Builder pattern to set the conditions for the task
JobScheduler—Schedule and cancel tasks, launch service

30
Q

JobService

A

Runs on the main thread

JobService subclass
Override
&raquo_space; onStartJob() –Implement work to be done here.
Called by system when conditions are met
&raquo_space; onStopJob() –Called if system has determined execution of job must stop… because requirements specified no longer met

31
Q

JobScheduler - Scheduling the job

A
Obtain a JobScheduler object form the system
Call schedule() on JobScheduler, with JobInfo object