views:

819

answers:

4

Hi, I would like to launch an app the user selects from within my application. However, I'm not sure how I'd go about doing this. I've tried this:

Intent intent = new Intent();
intent.setAction(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
startActivity(intent);

But this seems to throw an error and force close my application. I also tried adding:

<action android:name="Contacts.Intents.SHOW_OR_CREATE_CONTACT"/>

in the AndroidManifest file, but to no avail.

A look at Logcat shows that it's an "IOexception - no such file or directory". A couple of questions arise from this. I read through the Android docs and noticed that the Contact.Intents class is deprecated. However, it's successor, ContactContracts is aimed at API level 5 whereas I'm targeting API level 3. Could this be the problem? Also, I've hardcoded this application into the code. Is there a way to retrieve the intents of any application the user selects so that they can be launched?

Thanks

A: 

You need to pass in valid arguments to the apps you start. A lot of apps expect the data URI and / or certain extras to be valid.

CaseyB
A: 

Hi

Please try the following code:

Intent intent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);

this.startActivity(intent);

(sorry if there is something wrong on the syntax, I dont have android in this computer)

And remove the action from the manifest. that is not needed. The action method is used for something else. For more info, please look at the android site: http://developer.android.com/reference/android/content/Intent.html

Daniel

Daniel Benedykt
The string you pass in the `Intent` constructor *is* the action; the original code snippet is correct (if a needlessly verbose version of what you wrote).
Christopher
+2  A: 

You need to pass extra information into the intent to tell Android what you want to show or create. Otherwise Android doesn't know what activity to start and (presumably in your case) throws an ActivityNotFoundException.

For a contact, you use the generic Intent.ACTION_INSERT_OR_EDIT then use the MIME type of an individual contact (Contacts.People.CONTENT_ITEM_TYPE).

For example:

Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);

That will bring up the contacts app, prompting you to select an existing contact to add the phone number to, or to create a new contact.

You don't need to add anything special to your manifest to start external activities. Only if you were to directly manipulate the contacts ContentProvider would you need to add the appropriate CONTACT permissions to your manifest.

Christopher
Thanks, that worked! Does this mean that I'd have to hardcode extra parameters for every application?
keyboardP
Well you need to know what intent to fire, so in that sense you need to hardcode in some values. But you're not hardcoding an application name or anything, just an intent action.So to view an image (without caring which app will handle it), you could do: `new Intent(Intent.ACTION_VIEW, Uri.parse("content://foo/example/me.jpg"));` and so long as there's an app that can handle the combo of generic `VIEW` action and a JPEG file type, then that's all you need.
Christopher
Ah, I get it now. That makes it a lot easier lol :). Thanks for the help.
keyboardP
A: 

I use this code for that purpose:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings"); 
startActivity(intent);

This will launch the Settings app, you can use these also:

intent.setClassName("com.android.music", "com.android.music.MediaPlaybackActivityStarter");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");

The first starts the default music app, the second the contacts, and the third the dialer. Hope this helps.

nCounTr