i have an app which shows the received messages as a Toast through a BroadcastReceiver. I am presently using the SmsMessage.getOriginatingAddress() method which gives me the number of the sender, how can modify it to get the corresponding name of the sender if stored in the contacts?
You will need to query the contacts for the rest of the data.
First query for the contacts id using the phone number.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address),
new String[] { Contacts.Phones.PERSON_ID }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
Long id = Long.valueOf(cursor.getLong(0));
if (Log.DEBUG) Log.v("Found person: " + id);
return (String.valueOf(id));
}
} finally {
cursor.close();
}
}
Then query for the Contacts name with the id from the first query.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
new String[] { PeopleColumns.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
String name = cursor.getString(0);
if (Log.DEBUG) Log.v("Contact Display Name: " + name);
return name;
}
} finally {
cursor.close();
}
}
You may be able to combine these two queries somehow.
Related
public String[] getNewsLink(String prodcuttye) {
Cursor cursor = mDb.query(SQLITE_TABLE, new String[] { "productname" }, "ProdctType='"
+ prodcuttye + "'", null, null, null, null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("productname"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
mDb.close();
}
return result;
}
This function for fetch data in String array i am trying to excute this Query am getting data when i am call the in Sqlite browser[ select productname from MyShopingTable where ProdctType= 'Basmati Rice'] But when i try to run this Query in function i am getting cursor count 0 so i am unable to get data please tell me where am doing wrong please suggest me
Try changing your query like following :
Cursor c = mDb.query("SELECT * FROM MyShopingTable where ProdctType = ?",new String[]{prodcuttye})
See if that works in your case...
I am looking to fetch complete contact detail of the owner (me profile - e.g. in Nexus). I found ContactsContract.Profile API which is available for API v14+.
I am successfully able to get display name, corresponding to which i further require contact number. To do all this, i am using following code
private void getSelfSimNumber() {
String displayName = null;
try {
Cursor c = this.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI, null, null, null,
null);
int count = c.getCount();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
displayName = c.getString(c
.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
if (Integer
.parseInt(c.getString(c
.getColumnIndex(ContactsContract.Profile.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " = ?", new String[] { displayName },
null);
while (pCur.moveToNext()) {
String ret = c.getString(0);
String ret1 = c.getString(1);
}
pCur.close();
}
}
c.close();
} catch (Exception e) {
}
}
The problem is I am unable to fetch contact number of "me" profile. If, suppose, I have total 3 contacts (including "me" profile) then total contacts available i am getting is only 2. The system is perhaps not considering "me" profile as a contact.
How to solve this problem and fetch contact number of "me" profile?
Thanks
public ArrayList < PatientInfo > getAllPatients(String username) {
ArrayList < PatientInfo > patients = new ArrayList < PatientInfo > ();
open();
Cursor cursor = db.query(UserTable, null, null, null, null, null, AccessedDate);
while (cursor != null && cursor.moveToNext()) {
try {
} catch (Exception ex) {
//Log.e("XXX", ex.getMessage());
}
}
if (cursor != null)
cursor.close();
close();
Collections.reverse(patients);
return patients;
}
I am getting username as argument in my method, how can i query out my table based on username and get the user specific result.
As you can see from the documentation, the third and fourth parameters to query() are the selection and selection args.
So you want something like this:
Cursor cursor = db.query(UserTable, null,
"username=?", new String[] {username},
null, null, AccessedDate);
Edit the third parameter as needed to match the actual name of the relevant column in your table.
I want to get several contact details from a contact list view. I have this code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
//HERE I WANT TO GET CONTACT DETAILS FROM THE ID PARAMETER
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, Uri.encode(id));
Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null,null,null);
try {
c.moveToFirst();
String displayName = c.getString(0);
} finally {
c.close();
}
}
But I get this exception: IllegalArgumentException, Invalid lookup id (when I call query method from cursor).
So I dont know how to get a valid lookup id from the item list.
Any idea? thanks!
Here id means, A contact id for which you want to fetch the contact details,
The simply code for getting phone number for a particular contact id is like,
// Build the Uri to query to table
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
// Query the table
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("Phone: " + phoneNumber + "\n");
}
}
So, from your question I have to doubt for id parameter which are you sing in your uri just clear that, Also in my example the id is string type...
Hope you will understand it.
Update: Uri.encode(id) instead of just pass id in string format.
Thanks..
Because managedQuery is deprecated you can also use
Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null);
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int columnIndex = arg2.getColumnIndex(ContactsContract.Contacts._ID);
private void retriveContactImage(ImageView imageView, int columnIndex) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(photo);
}
private ArrayList<String> retrieveContactNumbers(TextView textView, long contactId) {
ArrayList<String> phoneNum = new ArrayList<String>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId + "" }, null);
if (cursor.getCount() >= 1) {
while (cursor.moveToNext()) {
// store the numbers in an array
String str = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (str != null && str.trim().length() > 0) {
phoneNum.add(str);
}
}
}
cursor.close();
textView.setText(phoneNum.get(0));
return phoneNum;
}
private void retrieveContactName(TextView textView, long contactId) {
String contactName = null;
Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts._ID + " = ?", new String[] { String.valueOf(contactId) }, null);
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
textView.setText(contactName);
}
I am able to obtain a list of contacts and their basic information like: name. phones, emails, ims, notes, organizations for backup purposes by using ContactsContract.Contacts.CONTENT_URI for a list of Contacts and other specific URIs for different information type.
I need, in order to fully restore all the information two more fields:
ContactsContract.RawContacts.ACCOUNT_TYPE
ContactsContract.RawContacts.ACCOUNT_NAME
Can anyone guide me how to obtain this info, knowing the Contact Id from ContactsContract.Contacts.CONTENT_URI ?
Thank you
public ContactAccount getContactAccount(Long id,ContentResolver contentResolver) {
ContactAccount account = null;
Cursor cursor = null;
try {
cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
ContactsContract.RawContacts.CONTACT_ID +"=?",
new String[]{String.valueOf(id)},
null);
if (cursor != null && cursor.getCount() >0)
{
cursor.moveToFirst();
account = new ContactAccount();
account.setAccountName(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME)));
account.setAccountType(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
cursor.close();
}
} catch (Exception e) {
Utils.log(this.getClass().getName(), e.getMessage());
} finally{
cursor.close();
}
return(account);
}
The above answer is perfect if you are looking for account information using the contactID column. But, often information is stored using rawContactID. So, if you want to access account information for a raw-contact-id then you can use this method below.
The key difference is that I am using the _ID column from the rawContacts table. This maps to the rawContactID that you will see in other tables
public int updateAccountInfoForContactData(String rawContactID) {
int accountPos = 0;
Cursor cursor = null;
String accountName = null;
String accountType = null;
Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI;
String[] syncColumns = new String[] {
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsContract.RawContacts.ACCOUNT_TYPE,
};
String whereClause = ContactsContract.RawContacts._ID +"=?";
String[] whereParams = new String[]{String.valueOf(rawContactID)};
//Uri rawContactUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, longContactID);
try {
cursor = mContext.getContentResolver().query(
rawContactUri,
syncColumns,
whereClause,
whereParams,
null);
if (cursor != null && cursor.getCount() >0)
{
cursor.moveToFirst();
if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME) >= 0) {
accountName = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
}
if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE) >= 0) {
accountType = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
}
cursor.close();
cursor = null;
}
} catch (Exception e) {
Log.d(TAG, "getting account info failed");
} finally{
if(cursor != null) {
cursor.close();
}
cursor = null;
}
return(accountPos);
}