How to use google translator app

前端 未结 6 1628
你的背包
你的背包 2021-02-02 02:14

I coded program about dictionary sentence and I want to have function to go to \"google translator\" application in my app

How can I use it , Should I import anything?

6条回答
  •  故里飘歌
    2021-02-02 02:40

    The Google Translate activity names tend to change over time which makes the code fragile if you hardcode them.

    Here is an approach that works with the current version of google translate and will keep working with future updates:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        for (ResolveInfo resolveInfo : getPackageManager().queryIntentActivities(new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain"), 0)) {
            if (resolveInfo.activityInfo.packageName.equals("com.google.android.apps.translate")) {
                String activityName = resolveInfo.activityInfo.name;
                String packageName = resolveInfo.activityInfo.packageName;
    
                Intent intent = new Intent().setPackage(packageName)
                        .setClassName(packageName, activityName)
                        .setAction(Intent.ACTION_PROCESS_TEXT)
                        .setType("text/plain")
                        .putExtra(Intent.EXTRA_PROCESS_TEXT, "Nobody expects the Spanish Inquisition!")
                        .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, true);
    
                startActivity(intent);
            }
        }
    } else {
        // >>> deprecated code from other answers goes here <<<
    }
    

提交回复
热议问题