2013-10-02 55 views
2

我写了一个程序,通过Intent.ACTION_VIEW调用Google Translator android应用程序。 问题是,调用谷歌翻译应用程序不再工作,虽然它只做过一次。Google翻译活动不再工作

的代码是相同的这里给出的代码:

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翻译应用程序?

回答

1

UPDATE:

下面的代码工作与谷歌的新版本的翻译应用:

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

正如你所看到的,这是标准的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")); 
7

他们再一次改变了它:

  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); 
+0

谢谢..尝试了所有以前的解决方案,他们没有工作,这是令人沮丧的,他们不断改变API和非常ks不断更新我们! – Bruce

+0

实际工作的唯一解决方案是使用该uri。 – Virusman

+0

感谢它的工作 – cuasodayleo