第1节 Sharing Simple Data
1.1 Sending Simple Date to Other Apps
When you construct an intend, you must specify the action you want the intent to "trigger",Android defines serveral action,
including ACTION_SENDING which is sending data from one activity to anther,even across process boundaries.
All you need to do is specify the data and its type, the system will identify compatible receiving activities and display themor
immediately start the activity. You can also advertise the data types that you avtivities support recerving from other applicaion
by specifying them in you manifest.
We can use intents to share information quickly and easily.Sending and receiving data between applications with intents
is most commonly used for social sharing of content.
The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became avaible in API
level 14 .
Send text content
The most staightforward and common use of the ACTION_SEND action is sending text content from one activity to another.
here is the code to implement this type of sharing:
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(sendIntent);
step 1 : new an intent object;
step 2 : set the action for the intent;
step 3 : set the content;
step 4 : set the MIME type
step 5 : start the activity
If there is an installed application with a filter that matches ACTION_SEND and MIME type text/plain, the Android system will
run it; if more than one ,it will be displayed in a dialog that allows the user to choose an app.
Note : You can also call Intent.createChooser() for the intent in step 5, There are some advantages;
Here is the updated code:
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Send binary content
Binary data is shared using the ACTION_SEND action combined with setting the appropriate MIME type and placing
the URI to the data in an extra named EXTRA_STREAM. This is commonly used to share an image but can be uses
to share any type of binary data:
Here is the code to implement this type of sharing
Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
You can also send multiple pieces of content by using the ACTION_SEND_MULTIPLE action together with a list of
URIs pointing to the content . the MIME type varies according to the mix of content you are sharing.
Here is an example:
ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.add(imageUri2); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share images to.."));
1.2 Receiving Simple Data From Other Apps
This class will tell you how you application receive simple data from other applications, For example, a user can easily
start a new baidu post with photos from the Android Gallery app .
Update you mamifest
You define an intent filter in your manifest using<intent-filter>element in order to be able to receive intents which you
constructed in the previous lesson.
Here is an example:
<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: This example define your application handles receiving text content , a single image of any type, or multiple
images of any type.
When another application tries to share any of these things by constructing an intent and pasing it to starActivity()
, your application will be listed as an option in the intent chooser.If the user selects your application, the corresponding
activity will be started, It is tnen up to you to handle the content appropriately within you code and UI.
Handle the incoming content
To start by calling getIntent() to get Intent object, as you get the object, you can examine its contents to determine what to do next.
and this step need you to take into consideration when examining the intent.
Here is the example:
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: Remember to process binary data in a seperate thread rather than the UI thread .
1.3 Adding an Easy Share Action
By using ActionProvider (API Level 14), you can implement an effective and user friendly share action in you ActionBar easily.
ActionProvider handles both the apprearance and behavior of the item which are attached to a menu item in the ActionBar
You just need to provide a share intent and the ShareActionProvider does the rest.