开发者

Launching external application from my app

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 a开发者_如何学JAVAt 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?


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.


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.


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.


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


The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜