Android - How to call contact screen through our application
How to push contact a开发者_Python百科pp through our application and return back to our application with new modify details. It is possible or not?
Thanks.
From here: How to call Android contacts list?
I'm not 100% sure what your question is supposed to ask, but the following snippet should help you 'call the contacts list function, pick a contact, then return to [your] app with the contact's name'.
There are three steps to this process.
1) Permissions
Add a permission to read contacts data to your application manifest.
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2) Calling the Contact Picker
Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
Call startActivityForResult
, passing in this Intent (and a request code integer, PICK_CONTACT
in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICK
on the People.CONTENT_URI
, then return to this Activity when the selection is made (or canceled).
startActivityForResult(intent, PICK_CONTACT);
3) Listening for the Result
Also in your Activity, override the onActivityResult
method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK
.
You can get the URI of the selected contact by calling getData()
on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
In your manifest, you will need to add permission:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Next, within your activity, you would use something like:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
This queries for contacts and gets id and name.
You probably want to send an ACTION_EDIT
intent to Contacts
Uri uri = Uri.parse(iduri);
i.setData(uri);
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id));
startActivityForResult(i, some_int_EDIT_CONTACT);
or better
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, read_id);
i.setData(contactUri);
startActivityForResult(i, some_int_EDIT_CONTACT);
assuming you have the contact id
精彩评论