一、Sending simple data to other apps:发送数据到其他APP
当你创建一个Intent时,你必须指定一个你想要触发的Action。Android本身定义了一些Action,比如ACTION_SEND:可以发送数据给其他应用,我称它为内置分享。
1、Send text content:发送文本
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(sendIntent)
系统会搜索符合条件的应用,如果有多个,会弹窗提示选择。当然也可以设置一开始就打开弹窗。
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(Intent.createChooser(sendIntent, dialogTitle))
参数字段不仅只有EXTRA_TEXT,还有EXTRA_EMAIL
, EXTRA_CC
, EXTRA_BCC
, EXTRA_SUBJECT。其中EXTRA_EMAIL
, EXTRA_CC
, 传参使用的是String[]:putExtra(Intent.EXTRA_EMAIL, String[])
2、
Send binary content:发送二进制内容
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uriToImage)
type = "image/jpeg"
}
startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to)))
分享本地图片,如果想要分享网络图片,则需要下载到本地才能分享。
3、Send multiple pieces of content:发送多条内容
val imageUris: ArrayList<Uri> = arrayListOf(
// Add your image URIs here
imageUri1,
imageUri2
)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND_MULTIPLE
putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
type = "image/*"
}
startActivity(Intent.createChooser(shareIntent, "Share images to.."))
把Uri改成String(Intent.EXTRA_TEXT)同样适用。不过能发送多条内容的应用相对较少。
二、Receiving simple data from other apps:接受其他应用发送的数据
上一节说的是发送数据到其他应用,那既然有发送,肯定得有应用接收才行,下面就开始说明怎么接收。
1、Update your manifest:在manifest中注明
我们知道,在manifest的activity节点里面,有时候需要添加<intent-filter <action/>/>,注明该Activity可以接收哪些Action。
<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>
2、Handle the incoming content:处理接收的数据
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
}
}
三、Adding an easy share action