android - action.PROCESS_TEXT Activity 未在 Oreo 的 EditText 中列出

标签 android

我正在使用 action_PROCESS_TEXT Intent 提供自定义文本选择操作。在 Marshmallow 上,我的操作显示在只读文本和 EditText 控件上,我的代码成功显示/返回结果。

在 Oreo (Pixel 2 XL) 上,我的操作成功显示在只读文本上,但我的操作没有显示在 EditText 控件上,无论是在我自己的应用程序中还是在其他应用程序中。我还注意到其他进程文本应用程序(例如 Google 翻译)只是随机显示。

Google 文档对此非常基础,我依赖原始 blog post , 有什么变化吗?

这是我的相关 list 和 Activity 代码

list

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        ...
        <activity
            android:name=".TranslateActivity"
            android:label="@string/action_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.PROCESS_TEXT"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Activity

package mynamespace

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_translate.*

class TranslateActivity : AppCompatActivity() {

    private var readOnly = true
    val translator = Translator()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_translate)
        setTitle(R.string.app_name)

        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        toolbar.setNavigationOnClickListener({
            onBackPressed()
        })

        translator.loadTranslations()

        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)

        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        if (intent.hasExtra(Intent.EXTRA_PROCESS_TEXT_READONLY)) {
            readOnly = intent.getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false)
        }

        if (intent.hasExtra(Intent.EXTRA_PROCESS_TEXT)) {
            val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT).toString()

            val result: String = translator.translate(text.toLowerCase())

            if (readOnly) {
                // Display result
                textViewTranslation.text = result
            } else {
                replaceText(result)
            }
        }
    }

    private fun replaceText(replacementText: String) {
        val intent = Intent()
        intent.putExtra(Intent.EXTRA_PROCESS_TEXT, replacementText)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
...
}

最佳答案

ACTION_PROCESS_TEXT does not work very well on Android 8.x .

好消息是,这似乎已在 Android P 中得到修复。在 2025 年及以后,一旦所有 Android O 设备都退役,您将不会遇到任何问题。

关于android - action.PROCESS_TEXT Activity 未在 Oreo 的 EditText 中列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50446191/

相关文章:

android - 合并适配器 header 崩溃

android - 如何获取前面TextView的Id?

android - 当我尝试使用 android-image-cropper :2. 8.+ 时我被卡住了

android - 从 ViewModel 使用 Volley Singleton

android - 如何在xml中的框架布局中嵌套约束布局?

android - 我可以旋转 3D 汽车对象吗

Android - 成功安装通知图标 - Jellybean

android - 刷机前自定义 Android ROM 应用程序数据

Android HTTPPost 返回错误 "Method not allowed."

android - 如何在 calabash android 中设置 SCRENSHOT_VIA_USB=true 。