一、Intent概念
Intent 英文词义是“意图”,在Android的编程框架里面,是体现“想要做某事”的概念。它的作用是在Activity之间传递数据,或者说,通过传递数据,达到请求另一Activity执行某种操作的目的。
二、分享功能
1.分享唤起端的实现
主要通过设置Action为Intent.Action_Send,设置Type和Extra,Type为MIME类型,Extra为附带的数据。
关键代码:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "要分享的文本数据");
sendIntent.setType("text/plain");
startActivity(sendIntent);
2.响应端的实现
主要通过设置FIlter以及获取Action来处理不同的数据。
关键代码:
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
} else if (type.startsWith("image/")) {
handleSendImage(intent);
}
}
记得还要在配置文件中设置Intent-Filter字段。
<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>