从其他应用程序接收数据 - android
Receiving data from other apps - android
我阅读了关于接收简单数据的documentation。我想收到一个 URL,即text/plain 来自其他应用。
所以,我只声明了这个 intent 过滤器:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
在我的MainActivity.class:
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
}
}
}
我将收到的文本处理为:
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
textBox.setText(sharedText);
}
}
但是,文档上写着我应该小心处理其他类型的 MIME 类型。
1) 但是,由于我只注册了 plain/text,我还需要更多类型处理代码吗?
2) 此外,引用文档:
Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the intent.
MainActivity.java也是由LAUNCHER启动的。我该如何处理?
3) 一旦用户从对话框中选择了我的应用程序,它会在所有情况下打开该应用程序吗?我不需要打开我的应用程序。我可以规避吗?
编辑: 我不需要打开我的应用程序的 UI。但是我想收到短信。
1. 是的,您需要为要共享的所有文件类型添加 mimeType
s。
2. 我认为问题可能是起始 Activity
也会有
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />.
在其清单声明中。所以当你打电话给
intent.getAction()
返回哪个动作?? ACTION_SEND
还是 MAIN
?我相信这就是他们正在谈论的问题。
3.如果你不想让你的应用显示在分享应用列表中,那你为什么添加这个动作
<action android:name="android.intent.action.SEND" />
清单中的这个 Activity
首先 ??因为,向 intent-filter 添加操作的目的恰恰是 Activity
或 Service
或 BroadcastReceiver
可以通过发送 隐式意图从另一个应用程序启动。如果您不希望这种情况发生,那么您打算如何 "share text" 呢??
正在从其他应用程序(例如图库应用程序图像共享到您的应用程序)接收所有数据(如图像、文本)
Kotlin
activity_receiving_data_from_other_app.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReceivingDataFromOtherAppActivity">
<TextView
android:id="@+id/receivingDataTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:contentDescription="@string/todo"
android:layout_marginTop="8dp"
android:id="@+id/receivingSingleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingDataTxt" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingSingleImageView"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewMain"
android:contentDescription="@string/todo">
</ImageView>
ImageUsPagerAdapter.kt
import android.content.Context
import android.net.Uri
import android.view.*
import androidx.viewpager.widget.PagerAdapter
import com.materialsouk.allcodeapp.R
import java.util.*
import kotlin.collections.ArrayList
class ImageUsPagerAdapter(context: Context, private val urlImage: ArrayList<String>?) :
PagerAdapter() {
private val mLayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
override fun getCount(): Int {
return urlImage!!.size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView: View = mLayoutInflater!!.inflate(R.layout.image_item, container, false)
val imageView: ImageView= itemView.findViewById(R.id.imageViewMain)
imageView.setImageURI(Uri.parse(urlImage!![position]))
Objects.requireNonNull(container).addView(itemView)
return itemView
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View?)
}
}
ReceivingDataFromOtherAppActivity.kt
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
class ReceivingDataFromOtherAppActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receiving_data_from_other_app)
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
}
}
}
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
findViewById<TextView>(R.id.receivingDataTxt).text = it
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
// Update UI to reflect image being shared
findViewById<ImageView>(R.id.receivingSingleImageView).setImageURI(it)
}
}
private fun handleSendMultipleImages(intent: Intent) {
intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
// Update UI to reflect multiple images being shared
val mViewPager = findViewById<ViewPager>(R.id.viewPager)
val imageArrayList : ArrayList<String> = ArrayList()
for (i in it){
imageArrayList.add(i.toString())
}
val mViewPagerAdapter = ImageUsPagerAdapter(this, imageArrayList)
mViewPager.adapter = mViewPagerAdapter
}
}
}
AndroidManifest.xml
<application ...>
<activity
android:name=".ReceivingDataFromOtherAppActivity"
android:exported="true">
<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>
</application>
我阅读了关于接收简单数据的documentation。我想收到一个 URL,即text/plain 来自其他应用。
所以,我只声明了这个 intent 过滤器:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
在我的MainActivity.class:
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
}
}
}
我将收到的文本处理为:
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
textBox.setText(sharedText);
}
}
但是,文档上写着我应该小心处理其他类型的 MIME 类型。
1) 但是,由于我只注册了 plain/text,我还需要更多类型处理代码吗?
2) 此外,引用文档:
Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the intent.
MainActivity.java也是由LAUNCHER启动的。我该如何处理?
3) 一旦用户从对话框中选择了我的应用程序,它会在所有情况下打开该应用程序吗?我不需要打开我的应用程序。我可以规避吗?
编辑: 我不需要打开我的应用程序的 UI。但是我想收到短信。
1. 是的,您需要为要共享的所有文件类型添加 mimeType
s。
2. 我认为问题可能是起始 Activity
也会有
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />.
在其清单声明中。所以当你打电话给
intent.getAction()
返回哪个动作?? ACTION_SEND
还是 MAIN
?我相信这就是他们正在谈论的问题。
3.如果你不想让你的应用显示在分享应用列表中,那你为什么添加这个动作
<action android:name="android.intent.action.SEND" />
清单中的这个 Activity
首先 ??因为,向 intent-filter 添加操作的目的恰恰是 Activity
或 Service
或 BroadcastReceiver
可以通过发送 隐式意图从另一个应用程序启动。如果您不希望这种情况发生,那么您打算如何 "share text" 呢??
正在从其他应用程序(例如图库应用程序图像共享到您的应用程序)接收所有数据(如图像、文本)
Kotlin
activity_receiving_data_from_other_app.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReceivingDataFromOtherAppActivity">
<TextView
android:id="@+id/receivingDataTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:contentDescription="@string/todo"
android:layout_marginTop="8dp"
android:id="@+id/receivingSingleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingDataTxt" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/receivingSingleImageView"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewMain"
android:contentDescription="@string/todo">
</ImageView>
ImageUsPagerAdapter.kt
import android.content.Context
import android.net.Uri
import android.view.*
import androidx.viewpager.widget.PagerAdapter
import com.materialsouk.allcodeapp.R
import java.util.*
import kotlin.collections.ArrayList
class ImageUsPagerAdapter(context: Context, private val urlImage: ArrayList<String>?) :
PagerAdapter() {
private val mLayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
override fun getCount(): Int {
return urlImage!!.size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView: View = mLayoutInflater!!.inflate(R.layout.image_item, container, false)
val imageView: ImageView= itemView.findViewById(R.id.imageViewMain)
imageView.setImageURI(Uri.parse(urlImage!![position]))
Objects.requireNonNull(container).addView(itemView)
return itemView
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View?)
}
}
ReceivingDataFromOtherAppActivity.kt
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
class ReceivingDataFromOtherAppActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receiving_data_from_other_app)
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
}
}
}
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
findViewById<TextView>(R.id.receivingDataTxt).text = it
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
// Update UI to reflect image being shared
findViewById<ImageView>(R.id.receivingSingleImageView).setImageURI(it)
}
}
private fun handleSendMultipleImages(intent: Intent) {
intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
// Update UI to reflect multiple images being shared
val mViewPager = findViewById<ViewPager>(R.id.viewPager)
val imageArrayList : ArrayList<String> = ArrayList()
for (i in it){
imageArrayList.add(i.toString())
}
val mViewPagerAdapter = ImageUsPagerAdapter(this, imageArrayList)
mViewPager.adapter = mViewPagerAdapter
}
}
}
AndroidManifest.xml
<application ...>
<activity
android:name=".ReceivingDataFromOtherAppActivity"
android:exported="true">
<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>
</application>