How to pick just the phone number from Phone book with Intent - android

I know the intent for getting phone number
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, GET_CONTACT_NUMBER);
But I don't know how to get the phone number without requesting the contact read permission in onActivityResult().
Thanks.

Try Replacing your code with
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);

#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == GET_CONTACT_NUMBER) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
Note: Before Android 2.3 (API level 9), performing a query on the
Contacts Provider (like the one shown above) requires that your app
declare the READ_CONTACTS permission (see Security and Permissions).
However, beginning with Android 2.3, the Contacts/People app grants
your app a temporary permission to read from the Contacts Provider
when it returns you a result. The temporary permission applies only to
the specific contact requested, so you cannot query a contact other
than the one specified by the intent's Uri, unless you do declare the
READ_CONTACTS permission.
Source: http://developer.android.com/training/basics/intents/result.html

Related

Android receiving number, email, etc. from contact

I want to access different contact information like a number, an email etc.
I use code from official Android documentation:
static final int PICK_CONTACT_REQUEST = 1; // The request code
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
String[] projection = {Phone.NUMBER};
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
The problem is, I can only access phone number and name, other data not. I can change pickContactIntent.setType to Email content type, but that I can access only Email... How can I access different kind of data in this case ?

How can show device primary contact viewer to show ONE contact with phone number

I'm writing an app and I need to show a contact to user. I have the phone number and I can make a query to get the contact detail. Like this:
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri,
new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = phoneNumber;
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
BUT I need to show the DEVICE primary contact viewer and I do't know How?
Like this:
This is all documented in official Android docs, Common Intents, chapter "Contacts/People App".
EDIT
But unfortunately I can not access to your link , nor google documents also in my area !!
Here're quotes from linked docs:
View a contact
To display the details for a known contact, use the ACTION_VIEW action and specify the contact with a content: URI as the intent data.
There are primarily two ways to initially retrieve the contact's URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Example:
public void viewContact(Uri contactUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Edit an existing contact
To edit a known contact, use the ACTION_EDIT action, specify the contact with a content: URI as the intent data, and include any known contact information in extras specified by constants in ContactsContract.Intents.Insert.
There are primarily two ways to initially retrieve the contact URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Note: Extras - One or more of the extras defined in ContactsContract.Intents.Insert so you can populate fields of the contact details.
public void editContact(Uri contactUri, String email) {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(contactUri);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
For more information about how to edit a contact, read Modifying Contacts Using Intents.

read contacts primary screen shown(open from) is supernumerary

I'm writing an app that get contacts and all detail , such as any numbers in each contact. it works fine but i have a problem. as you see in below images when I call:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
first of all I see the (open from) screen like this:
and when i select " Contacts " the second screen is shown:
Is there any way that I call the the method, the second screen with contacts and detail numbers directly shown and i needless to see the first screen and select " Contacts ".
Thanks.
After all I found this way:
instead of:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
I have write these lines:
Intent intent = new Intent(Intent.ACTION_PICK,Uri.parse("content://contacts/people")); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
I also had the same problem. Finally, I got rid of intermediate picker screen using below code,
Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);
In onActivityResult get phone number as below
if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContext().getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
cursor.close();
}

Getting Android Contacts From Local Contacts Only

I have an app that allows the user to enter a phone number or select one from their contacts. To do this I use :
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
//intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT); //PICK_CONTACT is defined earlier as 1
This works fine, but it includes contacts from various apps such as Facebook emails and such. If I uncomment
//intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
It only shows contacts that you would see in the Contacts app, but it doesn't return phone numbers. Any way to fix that?
I do have the READ_CONTACTS permission.
Code to read the Uri of the contact
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
if(reqCode != PICK_CONTACT) return;
Cursor cur = getContentResolver().query(data.getData(), null,
null, null, null);
if (cur.getCount() <= 0) return;
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext())
//phone is the EditText view where the user enters a phone number
phone.setText(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
phones.close();
}
}
}
but it doesn't return phone numbers
If you add the READ_CONTACTS permission, you can query ContactsContract, either to present the entire list yourself, or to query to get the phone number of a contact the user selected via ACTION_PICK.
Without READ_CONTACTS, you have no right to access contact phone numbers by any means, other than the user typing one into your app directly.

querying contacts - SOMETIMES returns empty cursor

I'm trying to query contact's display name:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CODE_PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
ContentResolver cr = getActivity().getContentResolver();
Cursor c = cr.query(contactUri, null, null, null, null);
if (c != null && c.moveToFirst()) {
//get the contact name
}
}
break;
}
}
Now here is the problem:
For some contacts the cursor returns empty, and I don't figure out why.
I checked the value of contactUri, it looks like: content://com.android.contacts/data/3032
The Uri looks the same for all types of contacts I tried - facebook, google, phone etc.
For some contacts the cursor returns with a result, which is good and I can extract the name. But for others it's somehow empty even though the ContentUri is exactly the same, It was originated from Intent.getData().
Here are some facts that may have something to do with this weird problem:
All the contacts that has empty cursor are facebook contacts. the Uri looks like above.
Not all the facebook contacts cause this: I have HTC One X, which on the phonebook I can "Link" between contacts if the OS finds a relation between them (say, if it detects the similar phone number for a gmail account and facebook acount, it suggests me to "link" between them). Only the facebook contacts who were NOT "Linked" returns empty.
Right now im out of ideas. Did anyone encountered this before?
Thanks in advance.
I had the same problem on an HTC Incredible S, which makes me think that it might be an HTC phones issue. Anyway, the workaround I ended up using is by retrieving the phone number from the bundle you get back with the data intent.
final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");
at that point you'll need to do some "reverse logic" to fetch the other data of the contact using PhoneLookup.

Categories

Resources