I am using this tutorial to learn about the basics of Intents in Android. The code sample I am using is adopted from the section named "Force an App Chooser" on the page linked.
The following is the method which should be invoked on the click of the button.
public void startSecondActivity(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
String fileChooserLabel = getResources().getString(R.string.fileChooserLabel);
Intent fileChooserIntent = Intent.createChooser(intent, fileChooserLabel);
if (intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
} else {
textView = (TextView) findViewById(R.id.text_view);
textView.setText("False");
}
}
But it just enters the else block of the if-else conditional. I tried this app on both a real device and the emulator. So can anybody point out what might be wrong here, and what I can do about it.
Note: I did not add anything to the Manifest file, since I am using Eclipse IDE and I suppose whatever is required at this point is automatically added to the manifest file.
It is returning null because there are no activities on the device that support your Intent. In this case, your ACTION_SEND Intent is not properly set up.
Note that the code sample you used as a reference is not a tutorial. It is not designed to be a complete code sample. In fact, what they list there will not even compile, as their ... is meant to be replaced by your own code to complete setting up the Intent.
You will need to fully configure your ACTION_SEND Intent, most notably setting the MIME type, as is covered elsewhere in the documentation. Replacing:
Intent intent = new Intent(Intent.ACTION_SEND);
with something like:
Intent intent = new Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, "IM IN UR STAK OVERFLO ANZWR");
should suffice.
In my application I do
Uri webAddress = Uri.parse("http://www.nrk.no");
Intent webIntent = new Intent(Intent.ACTION_WEB_SEARCH, webAddress);
String title = "Choose an app";
Intent chooserIntent = Intent.createChooser(webIntent, title);
startActivity(chooserIntent);
But the chooser that pops open says that No apps can perform this action. What am I doing wrong?
EDIT: Changing to ACTION_VIEW made it work, why?
I haven't used ACTION_WEB_SEARCH but it looks like the difference is what action it actually takes. You aren't providing a query to search for so the appropriate action would be ACTION_VIEW.
Take a look here in the docs. You could provide a query in the Intent.EXTRAS and see if that allows you to use ACTION_WEB_SEARCH
I would like to start a default application: browser, contact-book, phone, email, music app, etc. I have found many q/a, like browser opening a specific URL or blank, and here the answer is even "No not possible". But I would like to just open/launch it without telling it to go to a specific URL or sending a mail to someone, etc.
However, I also saw some Home applications where this seems to be working (at least for some apps). On my colleague's device there is for example a different contact-book (no google) which is detected and opened correctly.
I have seen in the Android documentation some intent categories that point to these problems, but these are only >= API.11. So I can't use/test them on my device.
Question: Is it not somehow possible to launch a default application (having the app chooser is of course ok) without providing extra data? If no, what do you think are these Home apps doing (perhaps workarounds are somehow possible).
PS: for the phone app I think, I have a workaround using Intent.ACTION_DIAL without any other information which will open simply the dialer.
UPDATE: I modified the title. Some applications like the address book may not be the same on different devices. So in this case I would like to start the address-book app, whichever this is.
This answer is not a 100% answer, but some workarounds on some typical applications.
Still open are: music player, address book
Browser: I get a list of applications that handle "http"-data intents, and then I look if one is available in the list of preferred applications.
Intent appFilter = new Intent(Intent.ACTION_VIEW);
appFilter.setData(Uri.parse("http://www.google.com"));
List<ResolveInfo> browserInfoList = pm.queryIntentActivities(appFilter, 0);
List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);
if(outActivities.size() > 0) {
for(ComponentName cn : outActivities) {
String cnClass = cn.getClassName();
String cnPkg = cn.getPackageName();
for (ResolveInfo info : browserInfoList) {
if(info.activityInfo.name.equals(cnClass) &&
info.activityInfo.packageName.equals(cnPkg)) {
return cn;
}
}
}
}
In case no default is found, I open a browser chooser dialog, see here.
Phone: as described in the question:
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
You can start apps by the function "startActivity" if you know about the canonical app name
like "android.com.browser". Do this simple by searching for AndroidManifest.xml in the app
source code (look at Codeaurora.com or at github/Cyanogenmod) and grab the app name you want.
After you know about the App name ("Activity") implement the code as follows:
Intent intent = new Intent();
intent.setClassName(this, "com.android.browser");
intent.setCategory(Intent.ACTION_MAIN);
startActivity(intent);
THIS is only a example, sometimes you have to put intent extras or data values, this information can be found in the app's AndroidManifest.xml too.
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);
//...
I would like to forward all calls to my number on to the new predefined number
automatically. Is it possible to forward incoming call ?
Probably it is possible for Froyo at least. I found application called Easy Call Forwarding.
http://www.appstorehq.com/easycallforwarding-android-189596/app
But many people reckon it doesn't work actually.
We can notice forwarded call by onCallForwardingIndicatorChanged() from PhoneStateListener but
I have no idea how to set forwarding mode.
I explored on the net and got the answer to my question that how one can forward a call programmatically. Add these lines of code and one will be able to achieve it.
String callForwardString = "**21*1234567890#";
Intent intentCallForward = new Intent(Intent.ACTION_DIAL); // ACTION_CALL
Uri uri2 = Uri.fromParts("tel", callForwardString, "#");
intentCallForward.setData(uri2);
startActivity(intentCallForward);
Here 1234567890 represents the phone number. Add the approriate phone number as you wish here. One can dial ##21# to deactivate the service.
My solution:
Intent intent = new Intent(Intent.ACTION_CALL);
String prefix = "#31#";
prefix = Uri.encode(prefix);
intent.setData( Uri.parse("tel:"+prefix+"123456"));
startActivity(intent);