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

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

ドキュメント/Androidアプリケーション解体新書 - (2007/11/16 (金) 15:16:25) の最新版との変更点

追加された行は緑色になります。

削除された行は赤色になります。

*Androidアプリケーション解体新書 There are four building blocks to an Android application: - Activity - Intent Receiver - Service - Content Provider Not every application needs to have all four, but your application will be written with some combination of these. Once you have decided what components you need for your application, you should list them in a file called AndroidManifest.xml. This is an XML file where you declare the components of your application and what their capabilities and requirements are. See the Android manifest file documentation for complete details. **Activity Activities are the most common of the four Android building blocks. An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity. Moving to another screen is accomplished by a starting a new activity. In some cases an activity may return a value to the previous activity -- for example an activity that lets the user pick a photo would return the chosen photo to the caller. When a new screen opens, the previous screen is paused and put onto a history stack. The user can navigate backward through previously opened screens in the history. Screens can also choose to be removed from the history stack when it would be inappropriate for them to remain. Android retains history stacks for each application launched from the home screen. 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.
*Androidアプリケーション解体新書 Androidアプリケーションには以下の4つの構成ブロックがあります。 -Activity(アクティビティ) -Intent Receiver(インテントレシーバー) -Service(サービス) -Content Provider(コンテンツプロバイダ) すべてのアプリケーションが上記の4つ全てが必要なわけではなく、いくつかを組合せてアプリケーションを作成します。 まず、あなたのアプリケーションにどんなコンポーネントが必要かを決めて、AndroidManifest.xmlと呼ばれているファイルに記述する必要があります。 これは、あなたのアプリケーションの能力と必要とするコンポーネントを宣言するXMLファイルです。 詳細は[[AndroidManifest.xml>ドキュメント/AndroidManifest.xml]]を見てください。 **Activity(アクティビティ) アクティビティは、4つのAndroidの構成ブロックの中で最も一般的です。アクティビティは、通常あなたのアプリケーションの一つの画面を意味します。各々のアクティビティは、[[Activity>ドキュメント/Activity]]ベースクラスを拡張した単独のクラスとして実装されます。 あなたのクラスは[[Views>ドキュメント/Views]]から成るユーザインタフェースを表示することや、イベントに対応しています。 大部分のアプリケーションは、複数の画面から構成されています。 たとえば、テキストメッセージングアプリケーションは、送信先のコンタクトリスト画面、メッセージ作成画面、メッセージ閲覧画面、設定変更画面を持っているかもしれません。 これらのスクリーンの各々は、アクティビティとして実行されます。 新しいアクティビティの起動によって別のスクリーンに移る。アクティビティは前のアクティビティに値を返す場合があります。たとえば、ユーザーに写真を選ばせるアクティビティはコール元に選ばれた写真を返します。 新しいスクリーンが開くとき、前画面はヒストリースタックの上に中断されて、置かれます。 ユーザーは、ヒストリーで直前にオープンされたスクリーンによって後ろに操縦することができます。 スクリーンは、彼らが残ることが不適当であるヒストリースタックから取り外されるほうを選ぶこともできます。アンドロイドは、ホームスクリーンから起動されるアプリケーションごとに、ヒストリースタックを保持します。 **Intent and Intent Filters(インテントとインテントフィルター) アンドロイドは、スクリーンからスクリーンへ移るインテントと呼ばれている特別なクラスを使用します。インテントは、アプリケーションが何がされることを望むかについて述べます。インテントデータ構造の2つの最も重要な部分は、作用する行動とデータです。行動のための典型的価値はMAIN(アクティビティの前のドア)、VIEW、PICK、EDIT、その他です。そして、データはURIとして表されます。たとえば、人への連絡先を見るために、あなたはその人を代表しているURIに、VIEWアクションとデータセットでインテントをつくるでしょう。 IntentFilterと呼ばれている関連したクラスが、あります。インテントが効果的に何かする要請である間、インテントフィルタはインテントアクティビティ(または、インテントレシーバ、下記参照)が取り扱うことができるものの説明です。人への連絡先を示すことができるアクティビティは、人を代表しているデータに適用されるとき、それがアクションVIEWを取り扱う方法を知っていると言ったインテントフィルタを発行します。アクティビティは、彼らのIntentFiltersをAndroidManifest.xmlファイルで発表します。 映るためにスクリーンから操縦することは、インテントを解決することによって達成されます。前方へ操縦するために、アクティビティはstartActivity(myIntentな)を呼びます。システムは、それからすべてのインストールされたアプリケーションのためにインテントフィルタを見て、試合が最もインテントフィルタをmyIntentする活動を選択します。新しいアクティビティは意図を知らされます。それはそれがラウンチされる原因になります。startActivityが呼ばれるとき、インテントを分解するプロセスは実行時で起こります。そして、それは2つの鍵となるベネフィットを提供します: - アクティビティは、単にインテントのリクエストフォームの中にを作ることによって、他の構成要素から機能を再利用することができます - アクティビティは、等しいインテントフィルターでいつでも新しいアクティビティと取り替えられることができます **Intent Receiver(インテントレシーバー) 例えば、電話が着信した時、ネットワークが利用可能になった時、真夜中になった時といった外部のイベントに反応するアプリケーションを作成したい時には、IntentReceiverを使えます。インテントレシーバーはUIを表示しません。ただし、注意を引くことが発生したらユーザーに知らせるために、インテントレシーバーはNorificationManagerを使うでしょう。インテントレシーバーは、AndroidManifest.xmlに登録されますが、Context.registerReceiver()を使うことによって、コード中で登録することもできます。アプリケーションは、コールされる時に、そのアプリケーションのインテントレシーバーが動いている必要はありません; システムは、必要であればそのアプリケーションを起動し、インテントレシーバーが開始されます。アプリケーションは、Context.broadcastIntent()を使うことによって、そのアプリケーション独自のインテントを他のインテントレシーバーにブロードキャストすることもできます。 **Service(サービス) サービスは、永続的に、UIなしで動くコードです。サービスのよい例として、プレイリストから曲を再生するメディアプレイヤーがあります。メディアプレイヤーアプリケーションでは、多分、ユーザーに曲を選択させたり、選択された曲の再生を開始させたりするための、1個かそれ以上のアクティビティがあるでしょう。しかし、ユーザーは、新たな画面に移動した後でも、音楽を再生し続けることを期待するでしょうから、音楽再生自体がアクティビティによって制御されるべきではありません。このケースでは、メディアプレイヤーアクティビティはバックグラウンドで音楽を再生し続けるために、Context.startService()を使うことによって、サービスを起動します。システムは音楽が終わるまで再生サービスを走らせつづけます。(システムでサービスにプライオリティを与える方法についての詳細は、[[Androidアプリケーションのライフサイクル>ドキュメント/Androidアプリケーションのライフサイクル]]を参照してください。)Context.bindService()メソッドでサービスに接続できる(そしてサービスがまだ走っていなければ、起動できる)ことに注意してください。サービスに接続中は、そのサービスによって公開されたインタフェースを介して、あなたはそれと通信できます。音楽サービスにおいてこのことは、あなたに音楽の一時停止や巻き戻し等の余地を与えるかもしれないことを示します。 **Content Provider アプリケーションは、それらのデータをファイル、SQLiteデータベースまたは意味をなす他のどのメカニズムにも格納することができます。 アプリケーションのデータが他のアプリケーションと共有したい場合、コンテントプロバイダーが使えます。 コンテントプロバイダーは、他のアプリケーションをそのコンテントプロバイダーによって取り扱われるデータの型を格納・読み出しさせるために方法の標準的なセットをインプリメントするクラスです。 コンテントプロバイダーに関するより多くの詳細を得るために、Accessing Content Providersを見てください。

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

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