Getting name of the message sender - android

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} .....)

Related

How to read "Contact Name" from inbox messages

I'm reading sms messages from inbox using content provider. See following code:
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = this.getContentResolver().query(uriSms, null, null, null, SORT_ORDER);
Inside loop I'm fetching all required columns:
String body= cursor.getString(cursor.getColumnIndex("body"));
String person = cursor.getString(cursor.getColumnIndex("person"));
I'm getting all values properly but person's name is returning 'null' value. Please guide me why contact name is not being returned from above statement.
The person column's value, if it exists, is not the sender's name. It's an ID that you would use to query the Contacts Provider to get the name. If you're getting null for person, then you'll have to use the address, which is the phone number, to query against. For example:
String address = cursor.getString(cursor.getColumnIndex("address"));
final String[] projection = new String[] {ContactsContract.Data.DISPLAY_NAME};
String displayName = null;
Cursor contactCursor = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(address));
contactCursor = getContentResolver().query(uri,
projection,
null,
null,
null);
if (contactCursor != null && contactCursor.moveToFirst()) {
displayName = contactCursor.getString(
contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(contactCursor != null) {
contactCursor.close();
}
}
Try this:
Cursor cursor = getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);

android sms contact name

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.

How do I change the phone number to name android?

I have this method to display the contact numbers in my inbox:
public ArrayList<String> fetchInboxNumbers() {
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null, null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
address = cursor.getString(1); // Displays phone number
Log.d("CONTACT", address);
sms.add(address); // + " " + body
}
Log.d("NAMES IN CALLLOG", sms.get(7));
return sms;
} // END FETCHINBOX
What I'm trying to do is change String address to its contact name.
From fetchContactNumbers() and 'fetchContactNames(), how can I changeaddress` to a contact name?
Its important to understand that fetchContactNumbers and fetchContactNames() are aligned, meaning fetchContactNumbers().get(5)'s contact name is fetchContactNames().get(5)`.
In the end, fetchInboxNumbers() populates a custom adapter.
This method returns a list of all contact names:
public ArrayList<String> fetchContactNames() {
ArrayList<String> names = new ArrayList<String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
names.add(name);
}
phones.close();
return names;
}
And this method lists all phone numbers:
public ArrayList<String> fetchContactNumbers() {
ArrayList<String> numbers = new ArrayList<String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber);
}
phones.close();
return numbers;
}
You can use CONTENT_FILTER_URI to get the display name, instead of getting all names and numbers separately, something like below:
public ArrayList<String> fetchInboxNumbers() {
String displayName = "";
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null, null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
address = cursor.getString(1); // Displays phone number
displayName = getDisplayName(address);
sms.add(displayName); // + " " + body
}
return sms;
} // END FETCHINBOX
private String getDisplayName(String phoneNumber) {
String displayName = "";
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri, new String[] { Phone._ID, Phone.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
return displayName;
}
Is it a spelling mistake? a variable mistake?
private String getDisplayName(String phoneNyumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.
CONTENT_FILTER_URI, Uri.encode(phoneNumber));
.....
}
Your argument is having phoneNyumber and you are using phoneNumber.

How to access SMS and contacts data?

is content providers the only way to read/write private data such SMS and contacts? I first try the easy and lazy way (copy sms and contacts SQLite databases files) but I faced some permission issues. I'm asking because I'm trying to backup and restore SMS and contacts and that would be a lot of work accessing data field one by one.
Getting Contacts:
How to call Android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
and for sms:
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
Log.e("Count",count);
while (cursor1.moveToNext()){
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
}
}
To get the contact
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
while (cursor.moveToNext()) {
try{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
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));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
}
phones.close();
}
}catch(Exception e){}
}
To read the sms from the inbox
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor1 = getContentResolver().query(
mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body",
"type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
Log.e("Count",count);
while (cursor1.moveToNext()) {
out.write("<message>");
String address = cursor1.getString(cursor1
.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1
.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1
.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1
.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1
.getColumnIndex(columns[4]));
}
}

retrieve mms address number

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.

Categories

Resources