【问题标题】:Receiving data from other apps - android从其他应用程序接收数据 - android
【发布时间】:2015-05-02 20:13:11
【问题描述】:

我在接收简单数据时阅读了documentation。我想收到一个 URL,即来自其他应用的文本/纯文本。

所以,我只声明了这个意图过滤器:

<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) 但是,由于我只注册了纯文本,我需要更多的类型处理代码吗?

2) 此外,引用文档:

请记住,如果此活动可以从系统的其他部分(例如启动器)启动,那么您在检查意图时需要考虑到这一点。

MainActivity.java 也由 LAUNCHER 启动。我该如何处理?

3) 一旦用户从对话框中选择了我的应用程序,它会在所有情况下打开该应用程序吗?我不需要打开我的应用程序。我可以绕过它吗?

编辑:我不需要打开我的应用程序的 UI。但我想收到短信。

【问题讨论】:

  • do I need any more type handling code?。好吧,你是那个找出答案的人。对于您不处理的类型,您的应用现在在哪些场景下启动?
  • 由于我的应用仅在意图过滤器中注册为“text/plain”,因此当用户想要共享其他类型(如图像)时,我的应用不会显示。那么,我还需要处理吗?
  • 如果您的应用没有出现。所以没有推出。那你在担心什么?如果您确实担心,那么只需显示一个 Toast() 表示您很抱歉。
  • 查看我编辑的答案

标签: java android android-intent intentfilter android-implicit-intent


【解决方案1】:

1.是的,您需要为要共享的所有文件类型添加mimeTypes。

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 ??因为,将动作添加到意图过滤器的目的正是ActivityServiceBroadcastReceiver 可以通过发送隐式意图从另一个应用程序启动。如果您不希望这种情况发生,那么您打算如何“分享文字”呢??

【讨论】:

  • 不,我补充说是因为我希望我的应用程序接收来自其他应用程序的文本。但是,当用户点击我的应用时,我不想打开我的应用 UI,但我想获取该文本。
  • 好的,我可以在清单中为服务注册那个意图过滤器,不像活动。因此,我可以避免显示 UI。
  • 那么它将开始一个Service。那是你要的吗 ?那你打算怎么办?
  • 是的,基本上文本是一个 URL。我的应用程序中有一些代码将访问该 URL 并从该页面获取一些数据。我还应该将此 URL 和获取的数据存储在数据库中。是的,我还认为不显示 UI 会很奇怪,用户可能会担心他是否执行了该操作。
  • 我最好只坚持活动。我认为这更好。你怎么看?再次感谢! :)
【解决方案2】:

从其他应用接收所有数据(如图像、文本)(如图库应用图像共享到您的应用)

科特林

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>

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2018-11-06
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    相关资源
    最近更新 更多