How to get last inserted native contact id in android - android

I have to get distinct raw contact id using content resolver.
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));
System.out.println("id............"+id+"..."+name);
}
}
Ist i have inserted all contacts into native contacts.Then Using above code i got all native contact id. How to get one id at a time after inserting 1 contact. Please help me.Thanks in advance.

Replace to
cursor.moveToLast();
you can easily get last id of the database

Related

How to get rawcontact id with contact id and lookup key

I am trying to get rawcontacts._ID from contacts provider by using the lookup key and contact id stored in the application.
This function exists that might help me but I am not sure how to construct the rawContactUri
public static Uri getContactLookupUri (ContentResolver resolver, Uri rawContactUri)
Build a CONTENT_LOOKUP_URI style Uri for the parent ContactsContract.Contacts entry of the given ContactsContract.RawContacts entry.
ContentResolver cr = getBaseContext()
.getContentResolver();
Cursor cur = cr
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
if (cur.getCount() > 0) {
Log.i("Content provider", "Reading contact emails");
while (cur
.moveToNext()) {
String contactId = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
}
}

Effective way to get contacts info with photo

I want to get some basic info of all contacts(I use api lvl 8). Currently i use this code snippet
private List<ContactInfo> readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, Phone.DISPLAY_NAME + " ASC");
for (int i = 0; i < cur.getColumnCount(); i++) {
Log.v("COlum", cur.getColumnName(i));
}
List<ContactInfo> temp = new ArrayList<ContactInfo>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactInfo mContactInfo = new ContactInfo();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
mContactInfo.setId(Long.parseLong(id));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
mContactInfo.setmDisplayName(name);
// get the <span class="IL_AD" id="IL_AD7">phone
// number</span>
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mContactInfo.setmPhoneNumber(phone);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email <span
// class="IL_AD" id="IL_AD9">addresses</span>
// if the email addresses were stored in an array
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
mContactInfo.setmEmail(email);
}
emailCur.close();
temp.add(mContactInfo);
}
}
}
return temp;
}
and pass to custom adapter (extended baseadapter). I get contact's photo using:
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = openContactPhotoInputStream1(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
I tested on my phone with 2x contacts (had photo). I took ~ 10s to fetch all contact at 1st runtime. I try force close in application settings and run again. This time it took ~2s to get data.So i want to know the most effective way to get contacts info.
I found some similar SO questions but they dont need photo. contacts in android
I tried use cursor and cursor adapter but i dont know what query to get photo_uri + contact name at the same time.
Edit: i removed all getColumnIndex i can out of loop and project only column i want. The performance is better(10s => 5s).
What i want to know :
Better way to query info and set to my ContactInfo model or the query which get name, phone number, email, photo at the same time to pass to cursor adapter.
Thanks in advance
I changed to CusorAdapter and use ContactsPhotoLoader from Contacts app and performance is improved.
To get contact info you have to work with the Android Contact API. Also you have to keep in min that you have to handle this Api in a different way for android api below API 4 (1.6)and for the Android API 5 (2.0) and higher:
I will provide you some good links that will help you:
Working With Android Contacts
Handling Contact Photos (All API Levels)
Using the Contact Picker API 2.0 and above
Retrieving Contact Information (Name, Number, and Profile Picture)
API4 and lower
Thes also some SO thread similar to yours that must b helpful for you
get contact info from android contact picker
Getting a Photo from a Contact

how to store contacts(name and mobile no) in hash maparraylist

I want to store contact information in an arraylist hash map so that i writ that information in .csv file.
I am able to get contact information but how to make consistency means if i store information "name" in an array and contactno also in array than if any name not contains any contactno then inconsistency would occurs so please tell me how to overcome this.
My code is:
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));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
String phoneno = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Log.i("phonenos",phoneno);
}
pCur.close();
}
}
}
}
I don't quite get the question, could you explain it a little better, than I might be able to help.
You want 2 Hashmaps, 1 with contact his name, one with their number, and then you have problems when a contact hasn't got a number or..?
edit:
If you're looking for exporting into an csv, you should see this related topic
Android - Generate CSV file from table values
Hope this helps.

Retrieving the Phone number from a contact using "ContactsContract" by an ID?

I'm able to retrieve the display name of contact using the id Code as follows:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, selectedid);
Cursor cur = managedQuery(uri, null, null, null, null);
startManagingCursor(cur);
cur.moveToNext();
String mname = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Where the "selectedid" is my id of the contact to be retrieved.
Now i have a problem in retrieving the phone number from that id.
If possible can u type me the code which i have to add to get the phone number from that id
Have you tried:
String number = cur.getString(cur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
i kind of figured it out .... try the link bellow...
Retrieve Contact Phone Number From URI in Android

contacts in android

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?
ContentResolver cr = getContentResolver();
int index=0;
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0)
{
phoneNames=new String[cur.getCount()];
phoneNumbers=new String[cur.getCount()];
while (cur.moveToNext())
{
String 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)
{
phoneNames[index]=name;
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
phoneIndex++;
phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
}
pCur.close();
}
}
After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.
If you are specifically looking for data related to phone numbers i suggest you query on
android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor.
The following are the fields that you would be interested in:
DISPLAY_NAME
HAS_PHONE_NUMBER
NUMBER
TYPE
e.g
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
Further details please refer this
Please post your requirement if the assumptions are not true.
Some of quick checks:
Select only the required columns and not all in the first query.
Instead of using Integer.parseInt(cur.getString) use
cur.getInt()
Use PhoneLookup whenever dealing with phone numbers ( the number
field gives the raw phone number
instead of the value stored in
the database which can contain
-,),( appended with it)
Avoid using Cursor within a cursor. Use the API's which includes
joins already implemented in it like
RawContactsEntity, PhoneLookup.
Hope that helps.
Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.

Categories

Resources