Android retrieve contact name always returns false - android

I am trying to check whether given name is in phone contact or not, but it always returns false, i cant figure out what mistake i have done
public boolean ContactNotFound(String no, String name) {
if (no != null) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, Uri.encode(name));
String[] mPhoneNumberProjection = { PhoneLookup._ID,
PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = con.getContentResolver().query(lookupUri,
mPhoneNumberProjection, null, null, null);
LogUtil.d("Count -->" + cur.getCount());
if (cur.getCount() > 0) {
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
} else {
return false;
}
}
Kindly help me to figure out the issue

I suggest you to save all your contact names in arraylist temporarily and then check name using arrayList.contains(name) method

Related

Detect if media scanner running on Android

Is there any way to detect whether MediaScanner is running now or not .
for example if mediascanner is running,Thread will sleep for 200 milis.
thanks.
Use below code.
public static boolean isMediaScannerScanning(ContentResolver cr) {
boolean result = false;
Cursor cursor = query(cr, MediaStore.getMediaScannerUri(),
new String [] {MediaStore.MEDIA_SCANNER_VOLUME},
null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
result = "external".equals(cursor.getString(0));
}
cursor.close();
}
return result;
}
It is copied from ImageManager.java of AOSP.

retrieving record from database using cursor in android

I have used following codes to retrieve record:
String getCredentialsFromAdmint(String name ,String pwd) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ADMIN, new String[] { KEY_ID,
KEY_A_USER_NAME, KEY_A_PWD }, KEY_A_USER_NAME + "=?" + " AND " + KEY_A_PWD + "=?",
new String[] { name , pwd }, null, null, null, null);
if (cursor != null){
cursor.moveToFirst();
return cursor.getString(1);
}
else{
return "norecord";
}
This code works if there is a record but if no record is found then application gets an error.
Is there any other ways to retrieve data from database?
Change your code to following
if ( cursor.moveToFirst()){
return cursor.getString(1);
} else{
return "norecord";
}
You don't check if cursor.moveToFirst() returns true;
It should looks like this:
if (cursor != null) {
if(cursor.moveToFirst()){
return cursor.getString(cursor.getColumnIndexOrThrow(KEY_A_USER_NAME));
} else{
//no record
}
} else {
//invalid uri
}
Try to use the following code instead
if (cursor != null){
cursor.moveToFirst();
if(cursor.isAfterLast() == false) {
return cursor.getString(1);
}
else
{
return "norecord";
}
}
else{
return "norecord";
}

contact exists in contacts

I have phone number. Is there any way to check whether the phone number exists in contacts database in the device or not? Depending on that I need have move further in my app. Please suggest or if any one can have sample code snippet please provide.
The below is the code I wrote:
public boolean contactExists(Activity _activity, String number) {
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(number, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}// contactExists
Thanks in Advance...
public boolean contactExists(Activity _activity, String number) {
if (number != null) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
}// contactExists
Handled nullpointer exception.
A minor change in your code ::
You need to have lookupUri..
public boolean contactExists(Activity _activity, String number) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}// contactExists
I tried the code above on an ice cream device (SIII) and it didnt work
so after some search i ended up creating this method (which is working nicely )
private boolean isContact(String incommingNumber) {
Cursor cursor =null;
String name = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incommingNumber));
cursor = MainService.this.getContentResolver().query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
} finally {
if(cursor!=null){
cursor.close();
}
}
return Util.hasValue(name);
}

Query Android contact to get ACCOUNT_TYPE and ACCOUNT_NAME

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

How to know if Contact exists like a number in android

I am using the below code to know if any contact exists with a number in android native contacts
public boolean contactExists(Context context, String number) {
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}
my problem is that if i get a number with country code added.
so,can i compare only last 7 numbers in my query
You should probably look at the PhoneNumbersUtils class.
You can use this code:
String numberToCheck;
if (numberToCheck.endsWith(number)) {
//do smth
}
This should do the job.

Categories

Resources