ドキュメント > Androidアプリケーション解体新書

「ドキュメント/Androidアプリケーション解体新書」の編集履歴(バックアップ)一覧に戻る

ドキュメント/Androidアプリケーション解体新書 - (2007/11/20 (火) 17:17:39) のソース

*Androidアプリケーション解体新書

4つのビルディングブロックが、Androidアプリケーションにあります:

 - Activity(アクティビティ)
 - Intent Receiver(インテントレシーバー)
 - Service(サービス)
 - Content Provider(コンテンツプロバイダ)

すべてのアプリケーションが全4を持つ必要はあるというわけではありません、
アプリケーションはこれらをいくつか組合せて記述されます。

どんな構成要素をあなたのアプリケーションのために必要とするかについて決める為にそれらをAndroidManifest.xml.と呼ばれているファイルにリストしなければなりません。これは、あなたがあなたのアプリケーションの構成要素を宣言するXMLファイルと彼らの能力と要求がそうであることです。
詳細はアンドロイドマニフェストファイルdocumentationを見てください。


**Activity(アクティビティ)

アクティビティは、4つのAndroidビルディングブロックで最も一般的です。アクティビティは、通常あなたのアプリケーションの一つのスクリーンです。各々のアクティビティは、Activityベースクラスを拡張する一つのクラスとして実行されます。
あなたのクラスはViewsから成るユーザインタフェースを示して、イベントに応じます。
大部分のアプリケーションは、複数のスクリーンから成ります。

たとえば、テキストメッセージングアプリケーションは、メッセージを送るコンタクト、選ばれたコンタクトにメッセージを書く第2のスクリーンとレビュー前のメッセージまたは変化セッティングへの他のスクリーンのリストを示すひとつのスクリーンを備えているかもしれません。
これらのスクリーンの各々は、アクティビティとして実行されます。

新しいアクティビティの起動によって別のスクリーンに移る。アクティビティは前のアクティビティに値を返す場合があります。たとえば、ユーザーに写真を選ばせるアクティビティはコール元に選ばれた写真を返します。
新しいスクリーンが開くとき、前画面はヒストリースタックの上に中断されて、置かれます。


ユーザーは、ヒストリーで直前にオープンされたスクリーンによって後ろに操縦することができます。
スクリーンは、彼らが残ることが不適当であるヒストリースタックから取り外されるほうを選ぶこともできます。アンドロイドは、ホームスクリーンから起動されるアプリケーションごとに、ヒストリースタックを保持します。


Intent and Intent Filters


Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person.

There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling. An activity that is able to display contact information for a person would publish an IntentFilter that said that it knows how to handle the action VIEW when applied to data representing a person. Activities publish their IntentFilters in the AndroidManifest.xml file.

Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent). The system then looks at the intent filters for all installed applications and picks the activity whose intent filters best matches myIntent. The new activity is informed of the intent, which causes it to be launched. The process of resolving intents happens at run time when startActivity is called, which offers two key benefits:

- Activities can reuse functionality from other components simply by making a request in the form of an Intent
- Activities can be replaced at any time by a new Activity with an equivalent IntentFilter

**Intent Receiver

You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent().

**Service

A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc.

**Content Provider

Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

To get more details on content providers, see Accessing Content Providers.
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。