【问题标题】:Google Translate Activity not working anymore谷歌翻译活动不再工作
【发布时间】:2013-10-08 13:44:17
【问题描述】:

我编写了一个程序,通过Intent.ACTION_VIEW 调用谷歌翻译安卓应用程序。 问题是调用谷歌翻译应用程序不再起作用,尽管它曾经做过一次。

代码与此处给出的代码相同:

Returning Translated Text from Google Translate Activity

(是的,我试图用那个代码替换我的代码,谷歌翻译应用的行为就像它没有收到任何数据一样。)

目前我无法指定文本和两种语言。我能做的最好的就是使用ACTION_SEND,但它忽略了两种语言:

        Intent i = new Intent();
        i.setAction(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_TEXT, "What is going on?");
        i.putExtra("key_text_input", "What time is it?");
        i.putExtra("key_text_output", "");
        i.putExtra("key_language_from", "en");
        i.putExtra("key_language_to", "es");
        i.putExtra("key_suggest_translation", "");
        i.putExtra("key_from_floating_window", false);
        i.setComponent(new ComponentName("com.google.android.apps.translate",
            "com.google.android.apps.translate.translation.TranslateActivity"));

当我运行这段代码时,实际发生的事情是:谷歌翻译问我是否想从英语翻译并翻译“发生了什么事?”到法语。

那么:我现在如何将语言传递给 Google 翻译应用程序?

【问题讨论】:

    标签: android android-intent google-translate


    【解决方案1】:

    其他答案将作为全屏活动打开 Google 翻译应用。我想将其作为当前应用上方的浮动窗口打开。

    事实证明,您可以使用“android.intent.action.PROCESS_TEXT”的 Intent 操作来做到这一点,例如

    translateIntent.setComponent(new ComponentName(
        "com.google.android.apps.translate",
        "com.google.android.apps.translate.QuickTranslateActivity"
    ));
    translateIntent.setAction(Intent.ACTION_PROCESS_TEXT);
    translateIntent.putExtra(Intent.EXTRA_PROCESS_TEXT, textToTranslate);
    

    注意:我实际上并没有使用此方法,因为我使用 onActionStarted 提取了系统上下文菜单的意图,但我为您嗅探了意图,所以我不明白为什么如果手动创建它不起作用。

    【讨论】:

    • 我一直在用这个,但是如何指定输入/输出语言呢? “来自”或“key_language_from”似乎不起作用。这个调用意图没有官方文档,并且必须“嗅探”意图以找出支持的参数?
    【解决方案2】:

    他们又改了一次:

                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.setPackage("com.google.android.apps.translate");
    
                intent.putExtra(Intent.EXTRA_TEXT, text);
    

    更新:如果将文本和语言打包到 URI 中,可以传递语言:

                intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setPackage("com.google.android.apps.translate");
    
                Uri uri = new Uri.Builder()
                        .scheme("http")
                        .authority("translate.google.com")
                        .path("/m/translate")
                        .appendQueryParameter("q", "c'est l'meunier Mathurin qui caresse les filles au tic-tac du moulin")
                        .appendQueryParameter("tl", "pl") // target language
                        .appendQueryParameter("sl", "fr") // source language
                        .build();
                //intent.setType("text/plain"); //not needed, but possible
                intent.setData(uri);
    

    【讨论】:

    • 谢谢.. 尝试了所有以前的解决方案,但都不起作用,令人沮丧的是他们不断更改 API,非常感谢您不断更新我们!!
    • 唯一有效的解决方案是使用该 uri。
    【解决方案3】:

    更新:

    以下代码适用于新版本的 Google 翻译应用程序:

            Intent i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_TEXT, "What is going on?");
            i.putExtra("key_text_input", "Oh my God! What is going on here?");
            //i.putExtra("key_text_output", "");
            i.putExtra("from", "en");
            i.putExtra("to", "zh-CN");
            //i.putExtra("key_suggest_translation", "");
            //i.putExtra("key_from_floating_window", false);
            i.setComponent(new ComponentName("com.google.android.apps.translate",
                    "com.google.android.apps.translate.HomeActivity"));
    

    如您所见,这是带有附加参数“to”和“from”的标准 ACTION_SEND。

    有一个问题:“key_text_input”优先于 Intent.EXTRA_TEXT,“to”和“from”仅适用于“key_text_input”。

    如果您认为(根本)没有传递任何数据,可能是因为您使用的是 3 字符语言代码而不是 2 字符语言代码。但是中文的代码是zh-CN和zh-TW。

    我之前的帖子:

    动作和参数名称已更改。

            Intent i = new Intent();
            i.setAction("com.google.android.apps.translate.action.QUERY");
            i.putExtra("key_text_input", "Oh my God! What is going on?");
            i.putExtra("key_text_output", "");
            i.putExtra("from", "en");
            i.putExtra("to", "zh-CN");
            i.putExtra("key_suggest_translation", "");
            i.putExtra("key_from_floating_window", false);
            i.setComponent(new ComponentName("com.google.android.apps.translate",
                "com.google.android.apps.translate.translation.TranslateActivity"));
    

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2015-05-19
      相关资源
      最近更新 更多