1 つのアプリから別のアプリへデータを送信できるのと同様に、他のアプリからデータを受信することもできます。ユーザーがアプリケーションを操作する仕組みと、 受信するメッセージの種類を指定します。たとえば、ソーシャル ネットワーキング アプリの場合、興味深いウェブ URL など、テキスト コンテンツを他のアプリから受信できると便利です。
他のアプリのユーザーは、Android Sharesheet やインテント リゾルバを通じてアプリにデータを送信することがよくあります。アプリにデータを送信するアプリは、 そのデータの MIME タイプ。アプリは別のアプリから送信されたデータを受信できます。 できます。
- マニフェスト内の
intent-filter
タグに合致するActivity
- アプリが公開する共有ショートカット。
直接共有ターゲットは、アプリ内の特定のアクティビティへのディープリンクです。 多くの場合、これらは個人やグループを表し、Android Sharesheet に表示されます。 たとえば、メッセージ アプリの場合、各ユーザーを対象とするダイレクト シェア ターゲットを提供すると便利です。ダイレクト シェア ターゲットをタップすると、ディープリンクの機能により、直接そのユーザーとのスレッドを開始できます。詳細な手順については、直接共有ターゲットを提供するをご覧ください。
MIME タイプをサポートする
理想的には、アプリは可能な限り幅広い範囲の MIME タイプを受け取れる必要があります。
たとえば、テキスト、画像、動画の送信に設計されたメッセージ アプリは、text/*
、image/*
、video/*
の受信をサポートするのが理想的です。Android でシンプルなデータを送受信する際の一般的な MIME タイプを以下に示します。
レシーバーが登録する | 送信者が送信 |
---|---|
text/* |
|
`image/*` |
|
video/* |
|
サポートされているファイル拡張子 | application/pdf |
MIME メディアタイプの IANA 公式レジストリをご覧ください。
最適な共有ターゲットを作成する
ユーザーが特定のアクティビティに関連付けられた共有ターゲットをタップすると、 は、共有コンテンツを使用する前に、その内容を確認、編集できる必要があります。この機能は、特にテキストデータの場合に重要になります。
アクティビティとともにデータを受信する
アクティビティでデータを受信するには、マニフェストの更新、受信コンテンツの処理、ユーザーがアプリを認識できるようにすることが必要になります。
マニフェストを更新する
インテント フィルタは、アプリ コンポーネントが受け入れるインテントをシステムに通知します。
URL の 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
)が開始されます。コンテンツを適切に処理するかどうかは、デベロッパーが行います。
必要があります。
受信コンテンツを処理する
Intent
によって配信されたコンテンツを処理するには、getIntent()
を呼び出して Intent
オブジェクトを取得します。オブジェクトを取得したら
内容を調べて、次に何をすべきかを判断できます。このアクティビティが
システムの他の部分(ランチャーなど)から起動する場合は、
考慮する必要があります。
受信データをチェックする場合は特に注意してください。他のアプリから何が送信されたのかを事前に知ることは決してできません。たとえば、MIME タイプが誤って設定されている場合や、送信された画像が極端に大きい場合などがあります。必ず、メイン(「UI」)スレッドではなく、別個のスレッドでバイナリデータを処理するようにしてください。
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 } }
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 とインテント リゾルバ。これらはいずれもマニフェスト内で定義します。Google Chat では アクティビティやインテント フィルタのラベルを設定して詳細なコンテキストを提供できます。
Android 10(API レベル 29)以降、Android Sharesheet で使用されるアイコンは、
application
タグでマニフェストを宣言します。Android では、intent-filter
タグと activity
タグに設定されたアイコンは無視されます。