他のアプリからシンプルなデータを受信する

アプリは他のアプリにデータを送信できるだけでなく、他のアプリからデータを受信することもできます。 できます。ユーザーがアプリケーションを操作する仕組みと、 受信するメッセージの種類を指定します。たとえば、 アプリケーションには、外部 IP アドレスなどのテキスト コンテンツを受信 別のアプリからの興味深いウェブ URL です。

他のアプリのユーザーが、Android 経由でアプリに頻繁にデータを送信している Sharesheet またはインテント リゾルバ。アプリにデータを送信するアプリは、 そのデータの MIME タイプ。アプリは別のアプリから送信されたデータを受信できます。 できます。

  • マニフェスト内の intent-filter タグに合致する Activity
  • アプリが公開する共有ショートカット。

直接共有ターゲットは、アプリ内の特定のアクティビティへのディープリンクです。 多くの場合、これらは個人やグループを表し、Android Sharesheet に表示されます。 たとえば、メッセージ アプリは、共有している相手にダイレクト シェア ターゲットを提供できます。 会話に直接移動できます。詳しくは、 詳細については、直接共有ターゲットの指定をご覧ください。 できます。

MIME タイプをサポートする

理想的には、アプリは可能な限り幅広い範囲の MIME タイプを受け取れる必要があります。 例: テキスト、画像、動画を送信するために設計されたメッセージ アプリ text/*image/*video/* の受信をサポートするのが理想的です。たとえば 一般的な MIME タイプをサポートしています。

受取人は 送信者が送信
text/*
  • text/plain
  • text/rtf
  • text/html
  • text/json
`image/*`
  • image/jpg
  • image/png
  • image/gif
video/*
  • video/mp4
  • video/3gp
サポートされているファイル拡張子 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 使用します)。コンテンツを適切に処理するかどうかは、デベロッパーが行います。 必要があります。

受信コンテンツを処理する

Intent によって配信されるコンテンツを処理するには、以下を呼び出します。 getIntent(): Intent オブジェクトを取得します。オブジェクトを取得したら 内容を調べて、次に何をすべきかを判断できます。このアクティビティが システムの他の部分(ランチャーなど)から起動する場合は、 考慮する必要があります。

受信データを慎重にチェックするため、 送信されることがあります。たとえば、MIME タイプが誤って設定されている場合や、送信された画像が極端に大きい場合などがあります。バイナリデータの処理と 実行されていることがわかります。

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 とインテント リゾルバ。これらはいずれもマニフェスト内で定義します。Google Chat では アクティビティやインテント フィルタのラベルを設定して詳細なコンテキストを提供できます。

Android 10(API レベル 29)以降、Android Sharesheet で使用されるアイコンは、 application タグでマニフェストを宣言します。Android で設定されたアイコンが無視される intent-filter タグと activity タグ。