Beruflich Dokumente
Kultur Dokumente
source
Processes in Android
When an application is launched and no other
component of the application is running, a new process
is spawned from the zygote
If at least one component is running, then the
application is launched within the same process
Not only same process, but also same execution thread,
called main
Android components
Activities
Services
BroadcastReceivers
ContentProvider
Processes in Android
The manifest entry for each type of component element
<activity>, <service>, <receiver>, and <provider>
supports an android:process attribute that can specify a
process in which that component should run.
Components can share processes
Apps can share processes IF they share the signature
certificate
source
Because
Android cleans up by killing
processes that take up a lot of
resources
Split your app into edible
components, and have the
lighter ones run for longer
source
Process importance
Most important
1.
2.
3.
4.
5.
Foreground process
Visible process
Service process
Background process
Empty process
Least important
Service process
Service process
Service processes are not directly
tied to anything the user sees, but
they are generally doing things that
the user cares about (such as
playing music in the background or
downloading data on the network)
source
Background process
Empty process
Threads
source
Threads
All processes have one thread called main
It is responsible with everything in the
eventloop, including drawing stuff
Often referred to as the UI thread
Jamming this one blocks the UI
Main thread
All components of a process share this
thread
System calls from are dispatched from this
thread
Methods that respond to system callbacks
run on this thread
Main thread
Bad programmers always
did network IO on main
thread
IDE now throws compile
exception when you do
such a horrible thing!
source
Main thread
Background threads
Background threads
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
}).start();
}
Async tasks
AsyncTask allows you to perform
asynchronous work on your user interface.
It performs the blocking operations in a
worker thread and then publishes the results
on the UI thread, without requiring you to
handle threads and/or handlers yourself.
Async tasks
Async tasks
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}