App gets hanged while trying to read contacts - java

I'm working on an app which reads the phone contacts and displays them in the form of list. I have to send message to selected contacts from the list. But the problem is that the app gets hanged if the mobile has more than 500 contacts. I don't get where the problem is..
I found this code on internet and implemented in my app. Contacts will be displayed but after taking so much time. Here is my code
ContentResolver cr = getActivity().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));
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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (phoneType) {
case Phone.TYPE_MOBILE:
Log.e(name + "(mobile number)", phoneNumber);
break;
case Phone.TYPE_HOME:
Log.e(name + "(home number)", phoneNumber);
break;
case Phone.TYPE_WORK:
Log.e(name + "(work number)", phoneNumber);
break;
case Phone.TYPE_OTHER:
Log.e(name + "(other number)", phoneNumber);
break;
default:
break;
}
}
pCur.close();
}
} }

To read contacts -
private void fetchContacts() {
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));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (name == null || name.equals(""))
name = phoneNumber;
if (Utils.notNull(phoneNumber)) {
phoneNumber = Utils.checkAndWrapMobileNumber(getApplicationContext(), phoneNumber);
allContacts.put(phoneNumber, name);
contactList.add(phoneNumber);
}
}
phones.close();
}
And better to use this code in AssyncTask so read in background thread.
Hope it will help you :)

it is due to heavy task in UI thread that blocks UI, Use AyncTask for this purpose.

Related

Showing some contacts multiple times in my App from phone book

I'm getting same contact three or two times in my app this happening with some contacts not with every contacts. In my app everything is working as expected but when clicking on show contact from my it's shows three time same contact but in mobile phone contact stored only one time. I tried everything from my side but not able to solve this can any body please help me. Or is there any alternative way for same.
Here is my code:-
#Override
protected Integer doInBackground(Void... params) {
try {
db = new WhooshhDB(myContext);
this.list = new ArrayList<>();
ContentResolver cr = myContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(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()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactModel model = new ContactModel();
if (phoneNo.replaceAll("\\s", "").trim().length() > 7) {
model.name = name;
model.mobileNumber = phoneNo.replaceAll("\\s", "");
if (model.mobileNumber.contains("-")) {
model.mobileNumber = model.mobileNumber.replaceAll("-", "");
}
model.iconHexColor = AppConstant.getRandomSubscriptionHexColor(myContext);
if (!phoneNumber.equals(model.mobileNumber)) {
list.add(model);
}
}
Log.i("FetchContacts", "Name: " + name);
Log.i("FetchContacts", "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
return AppConstant.SUCCESS;
} catch (Exception ex) {
return null;
}
}
You're printing those "FetchContacts" logs for per contact per phone, so if a contact has multiple phones stored for her you'll see it printed multiple times (even if it's the same phone number).
If you have an app like Whatsapp installed, then almost always you'll see all phone number duplicated for each contact causing those logs to be printed more then once per contact.
Also, that's a slow and painful way of getting contacts w/ phones.
Instead you can simply query directly over Phones.CONTENT_URI and get all phones in the DB, and map them out into contacts by Contact-ID:
Map<String, List<String>> contacts = new HashMap<String, List<String>>();
String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0); // contact ID
String name = cur.getString(1); // contact name
String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234
Log.d(TAG, "got " + id + ", " + name + ", " + data);
// add info to existing list if this contact-id was already found, or create a new list in case it's new
String key = id + " - " + name;
List<String> infos;
if (contacts.containsKey(key)) {
infos = contacts.get(key);
} else {
infos = new ArrayList<String>();
contacts.put(key, infos);
}
infos.add(data);
}
// contacts will now contain a mapping from id+name to a list of phones.
// you can enforce uniqueness of phones while adding them to the list as well.
Get rid of
while (cur != null && cur.moveToNext()) {
Change it to
if(cur.moveToFirst()){
list.clear();

How get only mobile numbers in contact

I have the below code to get contact name and number. How to get only mobile numbers and name in contact? A name in contact may have a few numbers. How to get mobile numbers for a name?
ContentResolver cr = activity.getContentResolver();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Try to use this code. Work for me.
private static final String[] PROJECTION =
{
Contacts._ID,
Contacts.LOOKUP_KEY,
Contacts.HAS_PHONE_NUMBER,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME
};
private static final String[] PROJECTION_PHONE =
{
Phone.NUMBER
};
private static final String SELECTION_PHONE = Phone.LOOKUP_KEY + " = ?";
HashMap<String, ArrayList<String>> contacts = new HashMap<>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(Contacts.CONTENT_URI,
PROJECTION, null, null, null); //get contacts
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME));
String lookUpKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0) { //check if has numbers
Cursor pCur = cr.query(Phone.CONTENT_URI, PROJECTION_PHONE, SELECTION_PHONE,
new String[]{lookUpKey}, null); //get contacts phone numbers
ArrayList<String> phones = new ArrayList<>();
while (pCur.moveToNext()) {
String phone = pCur.getString(0);
phones.add(phone);
}
contacts.put(name, phones);
pCur.close();
}
}
}
cur.close();
You could query the ContactsContract.CommonDataKinds.Phone.TYPE column to determine the type of the number:
int type = phones.getInt(phones.
getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
phoneNumber = phones.getString(phones.
getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
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));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("Number", phoneNumber);
}
phones.close();
}
}
}
You will have to ask for this permission:
uses-permission android:name="android.permission.READ_CONTACTS"

how to get email id , Display name and phone number

I have used following query to get phone numbers,display name and email
String[] PROJECTION = new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER,
Email.DATA, Contacts._ID };
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, PROJECTION,
null, null, null);
But not getting Phone number using
phonenumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER_ID));
Try with..
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
Or,see..
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
Read all contact's phone numbers in android

Get the caller id

I'm trying to make app which will log my incoming and outgoing calls and the problem is i can't seem to get working this code to return caller id(Like name and surname) but it allways returns "Unknown"
public String getname(String num,String nbc ){
String namer="";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
try {
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));
Log.i("",name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
// get the phone number
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));
System.out.println("phone" + phone);
if(phone==nbc||phone==num)
{
namer = name;
return namer;
}
}
pCur.close();
}
}
}
}
catch (Exception e) {
// TODO: handle exception
}
return namer;
}
It doesn't execute this(if(phone==nbc||phone==num)) part.
Can you please tell me what's wrong or another way to do this or at least point me in the right direction.
Use equals to compare Strings
if(phone.equals(nbc)||phone.equals(num))

Retrieve HOME Contact Number from address book

currently i have a application which will retrieve all the contact
details and will display all the available Contact Names.But now i want to retrieve Home,work numbers.I search for this everywhere but i couldn't. how can
i achieve this? please help me.
Thanks.
I used this code to get Home Numbers.
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Data.MIMETYPE},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.TYPE_HOME + "'",
new String[] {String.valueOf(contactId)}, null);
But what i need is to get Home,Work,Mobile (with name,email address,etc) numbers using single query.
using this code it always returns me type=0
Cursor c = getContentResolver().query(Data.CONTENT_URI,
null,
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
while(c.moveToNext()){
int type = c.getInt(c.getColumnIndex(Phone.TYPE));
..
}
try this:
int type = mCursor.getInt(mCursor.getColumnIndex(Phone.TYPE));
the "type" value will be 1,2,3 or 4 where
TYPE_HOME = 1;
TYPE_MOBILE = 2;
TYPE_WORK = 3;
TYPE_OTHER = 7;
Ok finally i completed the task.here my code
String mobile = "";
String home="";
String work="";
String fax="";
String other="";
String disName="";
String pName="";
String fName="";
String lName="";
String sName="";
String mName="";
String postBox="";
String streat="";
String country="";
String emailAdd="";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
disName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
///*
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
System.out.println("Numeber= "+number);
switch (type) {
case Phone.TYPE_HOME:
home=number;
break;
case Phone.TYPE_MOBILE:
mobile=number;
break;
case Phone.TYPE_WORK:
work=number;
break;
case Phone.TYPE_OTHER:
other=number;
break;
case Phone.TYPE_FAX_HOME:
fax=number;
break;
case Phone.TYPE_FAX_WORK:
fax=number;
break;
}
}
phones.close();
Cursor name = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'", null, null);
while(name.moveToNext()){
sName=name.getString(name.getColumnIndex(StructuredName.PREFIX));
fName=name.getString(name.getColumnIndex(StructuredName.GIVEN_NAME));
mName=name.getString(name.getColumnIndex(StructuredName.MIDDLE_NAME));
lName=name.getString(name.getColumnIndex(StructuredName.FAMILY_NAME));
sName=name.getString(name.getColumnIndex(StructuredName.SUFFIX));
System.out.println(mName);
}
Cursor address = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);
while(address.moveToNext()){
postBox=address.getString(address.getColumnIndex(StructuredPostal.POBOX));
streat=address.getString(address.getColumnIndex(StructuredPostal.STREET));
mName=address.getString(address.getColumnIndex(StructuredPostal.COUNTRY));
System.out.println(postBox);
}
Cursor email = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id , null, null);
while(email.moveToNext()){
emailAdd=email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
In where clause of your query use Phone.TYPE_HOME.. this will give you the desired nor..

Categories

Resources