I want to use the QuickTranslateActiviy
and I have tried this
Intent i = new Intent();
i.setAction(Intent.ACTION_PROCESS_TEXT);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.putExtra(Intent.EXTRA_TEXT,"String");
i.setType(ClipDescription.MIMETYPE_TEXT_PLAIN);
i.setComponent(new ComponentName("com.google.android.apps.translate","com.google.android.apps.translate.QuickTranslateActivity"));
startActivity(i);
and It just keep showing the toast that could not find text.
But the manifest of the Google translate here show that it accept plain text
had anyone try that before?Or am I doing it in a wrong way?
Tactically, you are using the wrong extra name. It should be EXTRA_PROCESS_TEXT or EXTRA_PROCESS_TEXT_READONLY, not EXTRA_TEXT.
Strategically, your implementation will break any time that the Google Translate app refactors their code or otherwise changes the fully-qualified class name of the activity.
I tried picking a file from the internal or external storage with the code below:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 1);
Of course it has onActivityResult method, and it's not the problem. It works fine in the modern phones or phones that have file manager installed. But the old one with no file manager throws
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT typ=file/* }
I tried switching to ACTION_PICK but no luck. I also tried intent.setType("*/*");, it didn't crash but the popup ask for action (videos, contacts,...) which is not true. I just want to pick any file not just a specified type.
I don't want to use any other file manager just to pick a file. Is there anyway I can get through this?
I believe that having the error explained, makes the solution much easier. So let me explain it to you:
You're starting an implicit intent. That means it's an intent that you know what you want to happen (use select a file) and you don't care which application will do it.
The error you're encountering is simply the system telling you (the developer), that there's no application installed that is capable of doing it (neither system nor 3rd party). There's simply no one capable of handling the action you want.
So you have two options from what I can see:
try-catch the error
.
try {
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException e) {
// maybe you should show a toast to the user here?
Toast.makeText(context, "You need to install a file picker", Toast.LENGTH_SHORT).show();
// or maybe redirect to a 3rd party app that you know works
startIntent(new Intent(Uri.parse("https://play.google.com/... some app
}
you can find a library or code to pick the file from inside your own app: http://bit.ly/1N1fZbO
Below code section should work for you!
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
Intent intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
What's your Android version? Some android maybe not released with such activity.
I have a code for opening URL with browser:
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
My question is, if user has set up default app for opening url, is there a way to override the default app and show application choose dialog instead?
i just found a solution. it was a so simple:
startActivity(Intent.createChooser(intent, title));
I do not know the exact answer to your question, but there is a bit more complicated decision. You can get a list of browsers, that installed on smartphone:
PackageManager manager = getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(myIntent,PackageManager.MATCH_DEFAULT_ONLY);
And then create a choose dialog manually.
P.S. Maybe there is a more simple solution ...
Now i am working on a Home Launcher application.I want to clear defaults of default home launcher(eg: Samsung Home). ie.I want to show Settings-> Applications->Manage Application->Samsung Home->clear defaults programmatically.
How to show this through code?
Thanks in Advance
NOTE: Since this question is limited to accessing the Manage Application Settings options, my answer covers just that. You will have to figure out a way of getting the actual Package Name.
Also, if the idea is to also Clear the Defaults automatically via code, then that, to the best of my knowledge, cannot be done. Someone can correct me if I am wrong on this.
That being said, this piece of code will open the specific application's Manage Application screen from your app (the package name must be supplied).
Intent showSettings = new Intent();
showSettings.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uriAppSettings = Uri.fromParts("package", "THE_APP_PACKAGE_NAME", null);
showSettings.setData(uriAppSettings);
startActivity(showSettings);
For example, if the package name of the Google Maps application is com.google.android.apps.maps, the replace THE_APP_PACKAGE_NAME with it and the code will open the Manage Application screen for the Google Maps application.
UPDATE:
The PackageManager has a method, clearPackagePreferredActivities used to clear the default via code. However, that doesn't seem to work in newer Android versions: https://stackoverflow.com/a/10246711/450534
Other posts worth reading:
https://stackoverflow.com/a/7750187/450534
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/Rzv8VU-EUAw
Just for complete the picture, for getting "THE_APP_PACKAGE_NAME" youc can use something like that :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String packageName = resolveInfo.activityInfo.packageName;
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?
From what I can tell, the Google Translate Android app does not expose any standard Intents that you could use (it's a pitty, but it's weird at the same time. You'd think Google would encourage this type of interaction between apps.. anyway).
However, it seems Google have opened up the translate API via a web service. This way, you can do the translation yourself and show it within your app. It's a bit more work, but it should do the job.
You could look at google-api-translate-java if you want to spare yourself from writing an API wrapper.
I have the same problem. Initially, I tried to use Google Translate Ajax API, but since Google have deprecated API version 1 and make version 2 as paid service, my code stops working. Then, I decompiled Google Translate App, looked into the Smali code and got some hint about the logic inside it. Use this code, it works for me:
private void callGoogleTranslateApps(String word, String fromLang, String toLang) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.putExtra("key_text_input", word);
i.putExtra("key_text_output", "");
i.putExtra("key_language_from", fromLang);
i.putExtra("key_language_to", toLang);
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.TranslateActivity"));
startActivity(i);
}
Phi Van Ngoc's answer was fantastic, thanks for that.
However it didn't work initially for me and after investigating the Translate apk, it looks like they've modified their file structure slightly, so the intent ComponentName should now be:
i.setComponent(
new ComponentName(
"com.google.android.apps.translate",
"com.google.android.apps.translate.translation.TranslateActivity"));
The difference is that "translation" has been added before "TranslateActivity"
So my final version, including hard-coded translation from Spanish to English, is:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.putExtra("key_text_input", "Me gusta la cerveza");
i.putExtra("key_text_output", "");
i.putExtra("key_language_from", "es");
i.putExtra("key_language_to", "en");
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"));
startActivity(i);
OMG! They have changed it once again! They have made it look more reasonable, but not compatible with the previous version.
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!");
i.putExtra("from", "en");
i.putExtra("to", "zh-CN");
i.setComponent(new ComponentName("com.google.android.apps.translate",
"com.google.android.apps.translate.HomeActivity"));
Looks like this is a SEND intent with two additional (BTW, optional) parameters, "to" and "from".
There's a gotcha: "key_text_input" takes preference over Intent.EXTRA_TEXT, and "to" and "from" work only with "key_text_input".
For people that change the API with each new version it may look only reasonable to rename "key_text_input" to, say, just "text_input", so we will look forward to the next release...
To be on the safe side, I'd propose to set both Intent.EXTRA_TEXT and "key_text_input" to the same value.
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 likely keep working with future updates (as long as the package name stays the same):
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 <<<
}
To add the above answers:
it is important that you pass two-letter language codes. With 3-letter codes, it may look like the google translate app does not receive any data.
In addition, if Intent.ACTION_VIEW does not work, you can use Intent.ACTION_SEND.
intent = new Intent();
//intent.setAction(Intent.ACTION_VIEW); // this did not work for me initially
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, m_text);
intent.putExtra("key_text_input", m_text);
intent.putExtra("key_text_output", "");
intent.putExtra("key_language_from", m_language);
intent.putExtra("key_language_to", lang_to);
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"
));
//try {
startActivityForResult(intent, REQUEST_CODE_TRANSLATE);
//...