《Training:Receiving Simple Data from Other Apps》

Keywords: Android Google network

Note: I am a non-professional translator. I only write this blog for self-study. If you have any questions, please refer to the official documents.



Just as your application can send data to other applications, it can easily receive data from applications. Consider how the user interacts with the application and the type of data to be received from other applications. For example, a social network application might be interested in receiving text content from another application, such as an interesting Web site. The Google+ Android application accepts both text and one or more images. With this application, users can easily start new Google + information from photos of the Android Gallery application.



Update your manifest file

Intention filters inform system application components what intentions they are willing to accept. Similar to the intent to build action ACTION_SEND in the Send Simple Data to Other Applications course, you can create an intent filter so that you can receive the intent of this operation. You can use the < intent-filter > element to define an intent filter in the list. For example, if your application processes received text content, a single image of any type, or multiple images of any type, the list will look like the following:

<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>

Note: For more information about intent filters and intent resolutions, please read intent filters and intent filters.


When another application tries to share these things by building an intention and passing it to startActivity (), your application will be listed as an option in the intent selector. If the user chooses your application, the corresponding activity (in the example above. ui.MyActivity) will be started. It's up to you to properly handle content in your code and UI.



II. Handling incoming content

To handle the content provided by Intent, first call getIntent () to get the Intent object. Once you have an object, you can check its content to determine what to do next. Keep in mind that if you can start this activity from other parts of the system, such as starters, you need to consider this when checking intentions.

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
    }
}

Note: Please check the incoming data. You never know what other applications might send you. For example, an incorrect MIME type may be set, or the image being sent may be very large. Also, remember to process binary data in separate threads rather than in main ("UI") threads.


Updating UI can be as simple as filling EditText, or as complex as applying interesting photo filters to images. This is really specific to what happens next in your application.

Posted by joejoejoe on Tue, 18 Dec 2018 04:54:04 -0800