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?
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 <<<
}