传递Text数据
如果有安装过的多个APP能够匹配ACTION_SEND这个action并且能够接受MIME TYPE为text/plain,则系统会弹出选择框允许用户选择想要传递给的APP。
代码如下:
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);
如果想总是显示出多个匹配的应用的选择框,可以使用Intent.createChooser(),这样可以保证即使之前已经对这个action做出过默认选择,仍然会显示一个选择框。可以通过createChooser指定一个title。
代码如下:
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(Intent.createChooser(sendIntent,getResources().getString(R.id.send_to)));
传递二进制数据
向EXTRA_STREAM传递data的Uri即可传递二进制的数据,该方法通常用来分享Image。
代码如下:
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)));
传递多条数据集
使用ACTION_SEND_MULTIPLE这个action来传递多条数据。
代码如下:
ArrayList imageUris = new ArrayList();
imageUris.add(imageUri1);
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 .."));
接收数据(从其他APP)
Update Your Manifest
使用
Handle the incoming Content
通过调用getIntent()方法得到intent对象,根据内容的不同,做不同的处理,例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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/")) {
handSendImage(intent); // handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
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 images being shared
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// update UI to reflect multiple images being shared
}
}
Adding an Easy Share Action
使用ShareActionProvider在actionbar上添加一个快捷分享按钮.
- 在menu资源文件中定义ShareActionProvider的属性
- 代码中设置你想分享的intent
想要启动一个分享的intent,首先要找到对应的menuitem,然后调用MenuItem.getActionProvider()得到ShareActionProvider的实例,使用setShareIntent的方法关联一个intent发送。代码实例如下:
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate menu resource file
getMenuInflater().inflate(R.menu.share_menu, menu);
// locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// return true to display menu
return true;
}
// call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}