Processes and Threads | Android Developers


Processes and Threads

When an application component starts and the application does not have any other components running,the Android system starts a new Linux process for the application with a single thread of execution.

By default, all components of the same application run in the same process and thread (called the "main" thread).

If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution.

However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process.







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.

To use it, you must subclass AsyncTask and implement the doInBackground() callback method, which runs in a pool of background threads.

To update your UI, you should implement onPostExecute(), which delivers the result from doInBackground() and runs in the UI thread, so you can safely update your UI.

You can then run the task by calling execute() from the UI thread.


public void onClick(View v) {
    new DownloadImageTask().execute("");

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {

※バックグラウンドで実行されるのはdoInBackground (Params... params)のみです。

doInBackground (Params... params)内でpublishProgress (Progress... values)を呼び出します。
なお、この際にはonProgressUpdate (Progress... values)がUIスレッドで実行されます


screen orientation, keyboard availability, and language

※この際にはアクティビティがリスタートされ,onDestroy()が呼び出されます。 ※onSaveInstanceState()でアプリの状態を保持しておきます。
※onCreate() or onRestoreInstanceState()で保持しておいたものを復活させます

  • アクションが破棄されたときに、正常にwokerスレッドを破棄する


An Intent is a messaging object you can use to request an action from another app component.

Although intents facilitate communication between components in several ways, there are three fundamental use-cases:

  • To start an activity:
    An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

If you want to receive a result from the activity when it finishes, call startActivityForResult().

Your activity receives the result as a separate Intent object in your activity's onActivityResult() callback.

  • To start a service:
    A Service is a component that performs operations in the background without a user interface.

You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService().

The Intent describes the service to start and carries any necessary data.

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

  • To deliver a broadcast:
    A broadcast is a message that any app can receive. (ex:the system boots up or the device starts charging.)

You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

Intents and Intent Filters | Android Developers