Google Translate Activity not working anymore

后端 未结 2 968
心在旅途
心在旅途 2021-01-01 02:41

I wrote a program that invokes Google Translator android application via Intent.ACTION_VIEW. The problem is that invoking the Google Translator App does not wor

2条回答
  •  暖寄归人
    2021-01-01 03:17

    UPDATE:

    The following code works with the new version of Google Translate Application:

            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"));
    

    As you can see, this is the standard ACTION_SEND with additional parameters "to" and "from".

    There's a gotcha: "key_text_input" takes preference over Intent.EXTRA_TEXT, and "to" and "from" work only with "key_text_input".

    If you have an impression that no data is passed (at all), maybe it is because you use 3-character language codes instead of 2-character ones. But the codes for Chinese are zh-CN and zh-TW.

    My previous post:

    The action and the parameter names have changed.

            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"));
    

提交回复
热议问题