I have been searching for days now on why this is not working. I am trying to get the sender number and name of the text message, but it always returns as zero or it's always the same exact number no matter who text me. And the name is always blank.
Cursor cur = mContext.getContentResolver().query(uriReceive, null, null, null,null);
while (cur.moveToNext())
{
int index_Address = cur.getColumnIndex("address");
int index_Person = cur.getColumnIndex("person");
strAddress = cur.getString(index_Address);
intPerson = cur.getString(index_Person);
}
If you're reading existing SMSes (not just incoming new ones)
String columns[] = new String[] { "_id", "date", "address", "subject", "body" };
Cursor cursor = ctx.getContentResolver().query(Uri.parse("content://sms/inbox"), columns, whereClause, bindVars, "date desc");
//Only want newest, so only handle first item in cursor
if(cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
long date = cursor.getLong(1);
String address = cursor.getString(2);
String subject = cursor.getString(3);
String body = cursor.getString(4);
//Handle message here
}while(cursor.moveToNext())
}
cursor.close();
Then to turn address into a name, you'd need to cross-reference address with the contacts database.
Related
I am using the below code to fetch the local phone contacts. It is working fine and also fetching the contacts very fast.
But the problem comes here, in my contact list there are few contacts that are having multiple Emails and multiple phone numbers.
In case of multiple phone or emails address it repeats the name of the same person multiple times.
And if i change
ContactsContract.CommonDataKinds.Email.CONTENT_URI
to ContactsContract.CommonDataKinds.Phone.CONTENT_URI then it will repeat name according to the number of phone number exists for a contact. Please help
private static final String[] PROJECTION = new String[]{
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.CommonDataKinds.Nickname.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Phone.NUMBER,
};
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
try {
final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
final int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
long contactId;
String displayName, email, phone, photo;
while (cursor.moveToNext()) {
mNK_UserModel = new NK_Contact();
contactId = cursor.getLong(contactIdIndex);
displayName = cursor.getString(displayNameIndex);
//Adding display name
mNK_UserModel.setFirstName(displayName);
Util.DEBUG_LOG(1, "contact", "contact id :" + contactId);
al_PhoneContacts.add(mNK_UserModel);
}
} finally {
cursor.close();
}
}
If i had to guess i would say you are missing a "break" in your while-loop. Since the cursor tries to fetch the next available column entry. But have a look at my solution which worked for me in the past.
It uses a seperate cursor for each row which gives you more control over the data.
Map<String, String> contactDataMap = new HashMap<String, String>();
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
contactDataMap.put(NAME, (name != null)?name:"");
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactDataMap.put(PHONE, (number != null)?number:"");
break;
}
pCur.close();
}
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));
contactDataMap.put(MAIL, (email != null)?email:"");
break;
}
emailCur.close();
cursor.close();
I'm trying to read contact numbers from users contact list. Here is my code
Cursor cursor = getContacts();
if(cursor.getCount()>0){
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
textViewDisplay.append("Name: ");
textViewDisplay.append(displayName+"Number :"+numberField);
textViewDisplay.append("\n");
}
}
You are taking cursor.getColumnIndex(COLUMN) into int. So the method as it says returns the index of COLUMN sent to it as parameter. You need Phone Number which will never be contained by an int as its size is always be greater than 4 bytes, and also it contains some special characters like +
So you need to take your Number to some String variable
Use String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
as suggested by Anuj.
use this
String numberField = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
instead of
int numberField = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
It returns "1" means that contact has phone number. try this,
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String cNumber="";
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.close();
}
try this one :
public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// Get contact id (id)
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// Get contact name (displayName)
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Get Phone Number....
Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?";
String[] SELECTION_ARRAY_PHONE = new String[] { id };
Cursor currPhone = cr.query(URI_PHONE, null,SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
int indexPhoneNo = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int indexPhoneType = currPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
if (currPhone.getCount() > 0) {
while (currPhone.moveToNext()) {
String phoneNoStr = currPhone.getString(indexPhoneNo);
String phoneTypeStr = currPhone.getString(indexPhoneType);
}
}
currPhone.close();
}
}
}
cur.close();
}
I'm implementing an AutoCompleteTextView and I need Name and E-Mail of all my contacts.
I found this snippet that I'm running asynchronously but it's very slow.
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));
Cursor emailCur = cr.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));
autoCompleteAdapter.add(name + " - " + email);
}
emailCur.close();
}
}
}
I'm performing a sort of inner query and I think that's the problem. Is there a way to tune it and make it faster?
private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
...
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
try {
final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
long contactId;
String displayName, address;
while (cursor.moveToNext()) {
contactId = cursor.getLong(contactIdIndex);
displayName = cursor.getString(displayNameIndex);
address = cursor.getString(emailIndex);
...
}
} finally {
cursor.close();
}
}
few notes:
use just ContactsContract.CommonDataKinds.Email.CONTENT_URI to get information you need, see ContactsContract.CommonDataKinds.Email for information what columns you can query
use projection to get only those columns you really need, you save some memory and increase query performance
get column indexes only once, just before the while cycle
You should not query directly the ContactsContract.Contacts
Make just one query on the ContactsContract.CommonDataKinds with the email data kind.
The ContactsContract.CommonDataKinds.Email inherits a lot of other interfaces that you can use to build your projection. (see inherited constants from the documentation)
For example :
import android.provider.ContactsContract.CommonDataKinds.Email;
[...]
public static final String[] EMAILS_PROJECTION = new String[] {
Email._ID,
Email.DISPLAY_NAME_PRIMARY,
Email.ADDRESS
};
to be used with the
Email.CONTENT_URI
You can retrieve a lot of information (such as user id, user display name ...) directly from the email data kind.
EDIT:
I just realized you're trying to build an AutoCompleteTextView.
You should override the runQueryOnBackgroundThread method and the convertToString of your CursorAdapter and use the Email.CONTENT_FILTER_URI
I really strongly suggest you to take a look at the ApiDemo samples.
Especially the AutoComplete4.java sample that you can find HERE.
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, "HAS_PHONE_NUMBER <> 0", null, null);
if (cursor!= null)
{
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int numberIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
final int idIndex= cursor.getColumnIndex(ContactsContract.Contacts._ID);
String displayName, number = null, idValue;
while (cursor.moveToNext())
{
displayName = cursor.getString(displayNameIndex);
idValue= cursor.getString(idIndex);
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = '" + idValue + "'", null, null);
phones.moveToFirst();
try
{
number = phones.getString(phones.getColumnIndex("data1"));
}
catch (CursorIndexOutOfBoundsException e)
{
}
phones.close();
userList.add(new ContactModel(displayName, number, null));
}
}
Please tell me how can i get the name of message sender. i tried following .All field works fine but but name always 0. Please give me solution.
String SORT_ORDER = "date DESC";
int count = 0;
String[] str= new String[] { "_id", "thread_id", "address", "person", "date", "body" };
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms"),
str,
null,
null,
SORT_ORDER);
Log.e("!!!!!!!!", ""+cursor.getCount());
while (cursor.moveToNext()) {
try {
// count = cursor.getCount();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
String address = cursor.getString(2);
long contactId = cursor.getLong(3);
String contactId_string = String.valueOf(contactId);
long timestamp = cursor.getLong(4);
String body = cursor.getString(5);
Log.e("!!!!!!!!", "number"+address);
Log.e("!!!!!!!!", "name"+contactId_string);
I think you will get number of message sender which you have to search in PhoneLookup
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)
In my application I'm able to get all MMS through code below, but I am not able to get the mms address. Can you guys/girls please help me?
Cursor cursor = activity.getContentResolver().query(Uri.parse("content://mms"),null,null,null,date DESC);
count = cursor.getCount();
if (count > 0) {
cursor.moveToFirst();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
long timestamp = cursor.getLong(2);
String subject = cursor.getString(3);
}
to get address from MMS. do something like this
in msgnumber pass your messageID.
String add="";
final String[] projection = new String[] { "address", "contact_id", "charset", "type" };
final String selection = "type=137"; // "type="+ PduHeaders.FROM,
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(msgnumber)).appendPath("addr");
Cursor cursor = context.getContentResolver().query(
builder.build(),
projection,
selection,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(0);
}
Hope this will help.