How to get EMail address on Android 2.0.1 - android

I am trying to get the emails from a contact on Android (2.0.1). I canĀ“t get the email. The code I am using is:
String columns = new String[]{ContactsContract.Data._ID,
ContactsContract.Data.DATA1,
ContactsContract.Data.DATA2,
ContactsContract.Data.DATA3,
ContactsContract.Data.DATA4,
ContactsContract.Data.DATA5,
ContactsContract.Data.DATA6,
ContactsContract.Data.DATA7,
ContactsContract.Data.DATA8,
ContactsContract.Data.DATA9
};
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, columns, null, null, null);
When I try to get the values of the columns, I get null. How can I obtain the emails? Maybe the CONTENT_URI is not correct or tha data is stored in another table and I have to make a join.

I have done this way:
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
It is working ok. I hope it can be helpful for another people.

I have done it in this way,
String id = contactData.getLastPathSegment();
Cursor emailsCur = getContentResolver().query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emailsCur.moveToNext()) {
String email = emailsCur.getString(emailsCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emailsCur.close();
I hope it will help you.

ContactsContract.CommonDataKinds.Email.DATA?
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html

I have solved the problem this way:
private static final int SUBACTIVITY_VIEW_CONTACT = 2;
.
.
.
Intent viewContactActivity = new Intent(Intent.ACTION_VIEW, data.getData());
startActivityForResult(viewContactActivity , SUBACTIVITY_VIEW_CONTACT);
.
.
.
Then I can see all the emails of the contact on the screen, just as I would have clicked on the contact itself on the Contacts application, but when I click on any email, it appears an "Unsupported Action. The action is not currently supported" screen. Does anybody knows something about it?
Thanks.

Related

In Sqlite, LIKE function is not working or return nothing

I use below code to get number from database from given number, for that I fired below query to get number but it does not return number. Is there wrong something in my query? please help me to resolved this issue.
I need to use LIKE function because some number stored with special char and whitespace.
contactNumber = contactNumber.replaceAll("[-+.^:,]", "");
contactNumber="%"+contactNumber+"%";
String strEmail = "";
String name = "";
Cursor cursor = ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{contactNumber}, null);
This is the way to use LIKE when using ContentProviders:
getContentResolver().query(Phone.CONTENT_URI, null, Phone.NUMBER + " LIKE ?", new String[] { "%" + contactNumber + "%" }, null);
However, the best way to search by phone number is to use the dedicated PhoneLookup API:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
getContentResolver().query(uri, new String[]{PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME}, null, null, null);
It's both optimised to return a result faster, and handles phone numbers with or without country-codes.

How can i get suggestion for Email addresses while typing the "To" filed?

I am writing an app that will send an email using the Java Mail external library. I searched for a week about how can i display to user Email addresses suggestions, like the ones being displayed in Gmail.
Any help is appritiated!
Get a list of the emails and use a autocompleteTextView to display them as you type.
Set an adapter to the textBox:
ArrayAdapter<String> namesAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_dropdown_item_1line, names);
searchBox.setAdapter(namesAdapter);
searchBox.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
String email = searchBox.getText().toString(); //or use the position to reference the email address you want.
});
}
As far as I know, Gmail uses the Gmail AddressBook for the suggestions. For your suggestions you can use the Contacts of your phone, which in case you have synched them with your Gmail AddressBook, they are also available at the Contacts.
Hope this helps! In case you need anything more specific, please do not hesitate to ask.
Ok, after a bit digging, here is what i found. Take the answer about auto complete TextView, add to it the following and you get what i was looking for.
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
cursor.close();
"Smartr Contacts" http://smartrcontacts.com/ is a good app for Gmail and Android and iPhone that will Synch all ur contacts n merge their email tel mob etc then u can perhaps call what email u want from the dbase.

how to get email id of the person which are in my contact list in Android

I just want to get email id of user having phone number given.
so i have to get email id of user whose number is like 986879899.so plz suggest me the solution of this problem or any links from where i got the solution.
I want to add one more thing that how to get person contact no. from my phone by referring person name who is in my contact list.
This is the solution of the above problem. Ashutosh given me the solution of the problem.
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println("email address might be"+emailAddress);
}
emails.close();
Thanks Ashutosh :)
it will be something like this
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", new String[]{"986879899"}, null);
cursor.moveToFirst();
cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.Email));
you'll need to add permission to read contacts though

Android Contacs

I'm able to fetch all the contacts from Contact list in android(phone numbers and emails)
but fetching all of them takes long time.
To speed up this I have stored them once in my application. But now i can't get the updated contacts. How does they need to be synced with my application?
Problem arises when I try to display them in a list because a user database of phone numbers and emails exceeds the 2000. I'm currently using base adapter implementation for this.
Can somebody help me out for large contact database?
Application like GroupMe n Viber show all user contacts very fast can somebody tell me brief explanation to achieve it.
String KEY_NAME = "Name";
String KEY_NO = "No";
String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = 1";
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String data="";
String name="";
ContactEntry contactObj;
String id;
String index="";
final String[] projection = new String[]{ContactsContract.Contacts._ID , ContactsContract.Contacts.DISPLAY_NAME , ContactsContract.Contacts.HAS_PHONE_NUMBER};
final String[] email_projection = new String[] {ContactsContract.CommonDataKinds.Email.DATA , ContactsContract.CommonDataKinds.Email.TYPE};
final String[] phone_projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI , projection , selection , null , sortOrder);
if(cur.getCount()>0){
while(cur.moveToNext()){
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , phone_projection ,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext()){
data = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(!temp.contains(data) && !data.equals(null)){
//Adding PhoneNumbers in List
}
}
pCur.close();
}
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, email_projection,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
while (emailCur.moveToNext()){
data = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(!temp.contains(data) && !data.equals(null)){
//Adding Email in List
}
}
emailCur.close();
}
}
I have used the above code
How can i pass the Curser in CurosrAdaper for all Emails and Phonenumbers.
please help me i havn't found any solution.
Android provides a sync adapter to solve this problem. you may refer to this example for the implementation.
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html
i had same issue , i have used ResourceCursorAdapter which fetches contacts as fast as Contact API from android....
I think you need to have a service (or) Asynch task something like that which periodically updates contacts from phone to your local app. Otherwise you can't synch.

how to get android contact list data on my seperate listview in android 2.1?

i have created one listview.now i want to show data(contact no.,name) from contactlist of android(Phonebook) on my listview.
i got it for android 1.5 but i want to do it for android2.1updated.
How to do it?can any one guide me or give some sample code?
Thanks in advance---
If you have achieved it in 1.5 then there can be a small difference of URI. You should go for updated/changed URI of Contacts in 2.1. For further understanding here is a sample code for 2.1updated:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
//---------------------------------------- Contact Names
//------------------------------------------------------
String DisplayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor names = cr.query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
names.moveToNext();
String GivenName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA2));
String FamilyName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA3));
String MiddleName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA5));
names.close();
//----------------------------------------- Phone Numbers
//-------------------------------------------------------
String hasPhoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhoneNumber.equals("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext())
{
......
}
}
} // AND SO ON.... Get which ever data you need.
so above is just a sample code that I used for my task. You can get what you need by making changes accordingly. I hope it helps.
This will help you getting contacts.
Cursor contactList = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
For getting phone number of a particular contact,you need to use:
String id = contactList.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);

Categories

Resources