アプリが他のアプリにデータを送信できるのと同様に、他のアプリからデータを受信することもできます。ユーザーがアプリをどのように操作するか、他のアプリから受け取るデータの種類について検討します。たとえば、ソーシャル ネットワーキング アプリでは、別のアプリから対象のウェブ URL などのテキスト コンテンツを受信できます。
他のアプリのユーザーは、Android Sharesheet またはインテント リゾルバを介してアプリにデータを頻繁に送信します。アプリにデータを送信するアプリは、そのデータの MIME タイプを設定する必要があります。アプリは、次の方法で別のアプリから送信されたデータを受信できます。
- マニフェスト内の
intent-filter
タグに合致するActivity
- アプリが公開する共有ショートカット。
直接共有ターゲットは、アプリ内の特定のアクティビティへのディープリンクです。多くの場合、個人やグループを表し、Android Sharesheet に表示されます。たとえば、メッセージ アプリは、そのユーザーとの会話に直接ディープリンクするダイレクト シェア ターゲットを、そのユーザーに提供できます。詳しい手順については、直接共有ターゲットを指定するをご覧ください。
MIME タイプをサポートする
理想的には、アプリは可能な限り幅広い範囲の MIME タイプを受け取れる必要があります。たとえば、テキスト、画像、動画を送信するために設計されたメッセージ アプリは、text/*
、image/*
、video/*
の受信をサポートするのが理想的です。Android で単純なデータを送受信するための一般的な MIME タイプは次のとおりです。
受取人は | 送信者が送信 |
---|---|
text/* |
|
`image/*` |
|
video/* |
|
サポートされているファイル拡張子 | application/pdf |
MIME メディアタイプの IANA 公式レジストリをご覧ください。
最適な共有ターゲットを作成する
特定のアクティビティに関連付けられた共有ターゲットをユーザーがタップしたときに、共有コンテンツを使用する前に確認して編集できるようにする必要があります。この機能は、特にテキストデータの場合に重要になります。
アクティビティとともにデータを受信する
アクティビティとともにデータを受信するには、マニフェストの更新、受信コンテンツの処理、ユーザーがアプリを認識できるようにする必要があります。
マニフェストを更新する
インテント フィルタは、アプリ コンポーネントが受け入れるインテントをシステムに通知します。他のアプリへのシンプルなデータの送信のレッスンの ACTION_SEND
アクションでインテントを作成する方法と同様に、このアクションでインテントを受信するインテント フィルタを作成します。マニフェストでインテント フィルタを定義するには、<intent-filter>
要素を使用します。たとえば、アプリがテキスト コンテンツの受信を処理する場合、任意のタイプの画像を 1 つ以上含むマニフェストは、次のスニペットのようになります。
<activity android:name=".ui.MyActivity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity>
別のアプリがインテントを作成して startActivity()
に渡して、上記のいずれかを共有しようとすると、そのアプリは Android Sharesheet またはインテント リゾルバにオプションとして表示されます。ユーザーがアプリを選択すると、対応するアクティビティ(上記の例では .ui.MyActivity
)が起動します。デベロッパーは、コードと UI 内でコンテンツを適切に処理する必要があります。
受信コンテンツを処理する
Intent
によって配信されるコンテンツを処理するには、getIntent()
を呼び出して Intent
オブジェクトを取得します。オブジェクトを取得したら、その内容を調べて次の処理を決定できます。このアクティビティをシステムの他の部分(ランチャーなど)から開始できる場合は、インテントを調べる際にそのことを考慮してください。
他のアプリがどのようなデータを送信しているかはわからないため、受信データには細心の注意を払ってください。たとえば、MIME タイプが誤って設定されている場合や、送信された画像が極端に大きい場合などがあります。また、バイナリデータは、メイン(「UI」)スレッドではなく、別のスレッドで処理してください。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { ... when { intent?.action == Intent.ACTION_SEND -> { if ("text/plain" == intent.type) { handleSendText(intent) // Handle text being sent } else if (intent.type?.startsWith("image/") == true) { handleSendImage(intent) // Handle single image being sent } } intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type?.startsWith("image/") == true -> { handleSendMultipleImages(intent) // Handle multiple images being sent } else -> { // Handle other intents, such as being started from the home screen } } ... } private fun handleSendText(intent: Intent) { intent.getStringExtra(Intent.EXTRA_TEXT)?.let { // Update UI to reflect text being shared } } private fun handleSendImage(intent: Intent) { (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let { // Update UI to reflect image being shared } } private fun handleSendMultipleImages(intent: Intent) { intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let { // Update UI to reflect multiple images being shared } }
Java
void onCreate (Bundle savedInstanceState) { ... // Get intent, action and MIME type Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) && type != null) { if ("text/plain".equals(type)) { handleSendText(intent); // Handle text being sent } else if (type.startsWith("image/")) { handleSendImage(intent); // Handle single image being sent } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { if (type.startsWith("image/")) { handleSendMultipleImages(intent); // Handle multiple images being sent } } else { // Handle other intents, such as being started from the home screen } ... } void handleSendText(Intent intent) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { // Update UI to reflect text being shared } } void handleSendImage(Intent intent) { Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri != null) { // Update UI to reflect image being shared } } void handleSendMultipleImages(Intent intent) { ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageUris != null) { // Update UI to reflect multiple images being shared } }
データの受信後の UI の更新は、EditText
を入力するのと同じ簡単なこともあれば、画像に面白い写真フィルタを適用するなど複雑なこともあります。その後の処理はアプリによって異なります。
アプリがユーザーに認識されるようにする
アプリは、Android Sharesheet とインテント リゾルバでアイコンとラベルで表されます。これらはいずれもマニフェスト内で定義します。アクティビティまたはインテント フィルタのラベルを設定すると、詳細なコンテキストを提供できます。
Android 10(API レベル 29)以降、Android Sharesheet は、マニフェストの application
タグで設定されたアイコンのみを使用します。Android では、intent-filter
タグと activity
タグに設定されているアイコンは無視されます。