【问题标题】:Access Google Translate App through intents in android通过 android 中的意图访问谷歌翻译应用程序
【发布时间】:2015-06-26 14:45:01
【问题描述】:

我正在开发一个 Android 应用程序,我正在通过 Intents 调用 Google 翻译应用程序。 here 描述的过程确实很有帮助,但我无法弄清楚为什么我的应用程序崩溃说“没有找到处理 Intent 的活动”。

提前致谢!

这是我的代码,

public void translate()
{
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "hello");
    intent.putExtra("key_text_input", "hello");
    intent.putExtra("key_text_output", "");
    intent.putExtra("key_language_from", "en");
    intent.putExtra("key_language_to", "mal");
    intent.putExtra("key_suggest_translation", "");
    intent.putExtra("key_from_floating_window", false);
            new ComponentName(
                "com.google.android.apps.translate",
                "com.google.android.apps.translate.HomeActivity");
    startActivity(intent);

}

这就是我的 logcat 显示的内容

04-20 07:29:01.647: E/AndroidRuntime(31465): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 (has clip) (has extras) }
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Activity.startActivityForResult(Activity.java:3388)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Activity.startActivityForResult(Activity.java:3349)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Activity.startActivity(Activity.java:3584)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.Activity.startActivity(Activity.java:3552)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at com.example.arch.vaani.ocr.FinalActivity.translate(FinalActivity.java:122)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at com.example.arch.vaani.ocr.FinalActivity$1.onClick(FinalActivity.java:101)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.view.View.performClick(View.java:4212)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.view.View$PerformClick.run(View.java:17476)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.os.Handler.handleCallback(Handler.java:800)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.os.Handler.dispatchMessage(Handler.java:100)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.os.Looper.loop(Looper.java:194)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at android.app.ActivityThread.main(ActivityThread.java:5371)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at java.lang.reflect.Method.invoke(Method.java:525)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-20 07:29:01.647: E/AndroidRuntime(31465):    at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

标签: java android android-intent android-logcat google-translate


【解决方案1】:

您需要将HomeActivity 更改为TranslateActivity

try {
   Intent intent = new Intent();
   intent.setAction(Intent.ACTION_SEND);
   intent.putExtra(Intent.EXTRA_TEXT, "hello");
   intent.putExtra("key_text_input", "hello");
   intent.putExtra("key_text_output", "");
   intent.putExtra("key_language_from", "en");
   intent.putExtra("key_language_to", "mal");
   intent.putExtra("key_suggest_translation", "");
   intent.putExtra("key_from_floating_window", false);
   intent.setComponent(new ComponentName(
        "com.google.android.apps.translate",
        //Change is here 
        //"com.google.android.apps.translate.HomeActivity")); 
        "com.google.android.apps.translate.TranslateActivity"));
   startActivity(intent);
} catch (ActivityNotFoundException e) {
    // TODO Auto-generated catch block
    Toast.makeText(getApplication(), "Sorry, No Google Translation Installed",
            Toast.LENGTH_SHORT).show();
}

【讨论】:

    【解决方案2】:

    No Activity found to handle Intent 表示没有Intent 与您启动的Intent 匹配,并且您的设备上没有安装Google Translation App。所以你最好在启动这个Intent 时尝试捕捉ActivityNotFoundException,以防出现这样的场景。 代码在这里:

    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, "hello");
        intent.putExtra("key_text_input", "hello");
        intent.putExtra("key_text_output", "");
        intent.putExtra("key_language_from", "en");
        intent.putExtra("key_language_to", "mal");
        intent.putExtra("key_suggest_translation", "");
        intent.putExtra("key_from_floating_window", false);
        intent.setComponent(new ComponentName(
                "com.google.android.apps.translate",
                "com.google.android.apps.translate.HomeActivity"));
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getApplication(), "Sorry, No Google Translation Installed",
                    Toast.LENGTH_SHORT).show();
    }
    

    最后,Google Translate API 可作为付费服务使用。。检查here

    【讨论】:

    • 我认为即使您安装了翻译应用程序,该应用程序仍无法正常工作。
    • 试过intent.setComponent(new ComponentName( "com.google.android.apps.translate", "com.google.android.apps.translate.HomeActivity"));?你甚至没有在Intent 上设置ComponentName
    • 我也试过了。和旧包名。不工作。你试过了吗?
    • 如果这些代码仍然不起作用,我认为你必须重新阅读它的API
    • 我认为这是故意的,Google 希望 API 的用户使用付费服务。 ;) 请参阅我发布的对此问题的答案中的链接。
    【解决方案3】:

    我使用 Google 翻译应用程序尝试了该代码,但它无法正常工作。 您可能需要使用付费服务Translate API

    【讨论】:

    • @Chaythanya nair 考虑将接受的答案更改为下面的答案 (stackoverflow.com/a/46088337/2102748),因为基本上您的代码唯一有问题的是活动名称。
    • 翻译应用程序现在可以使用上面链接答案中的代码 sn-p 按预期工作 ^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多