通过 android 中的意图访问 Google 翻译应用程序
Access Google Translate App through intents in android
我正在开发一个 Android 应用程序,我在其中通过 Intents 调用 Google Translate 应用程序。 here 中描述的过程非常有用,但我无法弄清楚为什么我的应用程序崩溃并显示 "No Activity found to handle 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.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)
No Activity found to handle Intent
表示没有 Intent
与您开始的 Intent
匹配,并且您的设备上没有安装 Google Translation App
。所以你最好在启动这个Intent
的时候try-catchActivityNotFoundException
,这样的场景。
代码在这里:
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 翻译 API 可以作为付费服务使用了。。检查 here.
我用 Google 翻译应用程序尝试了代码,但它不起作用。
您可能需要使用 Translate API,这是一项付费服务。
您需要将 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();
}
我正在开发一个 Android 应用程序,我在其中通过 Intents 调用 Google Translate 应用程序。 here 中描述的过程非常有用,但我无法弄清楚为什么我的应用程序崩溃并显示 "No Activity found to handle 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.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)
No Activity found to handle Intent
表示没有 Intent
与您开始的 Intent
匹配,并且您的设备上没有安装 Google Translation App
。所以你最好在启动这个Intent
的时候try-catchActivityNotFoundException
,这样的场景。
代码在这里:
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 翻译 API 可以作为付费服务使用了。。检查 here.
我用 Google 翻译应用程序尝试了代码,但它不起作用。 您可能需要使用 Translate API,这是一项付费服务。
您需要将 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();
}