I'm making a new application, one of the feature of this application is that a user can interact only with his phone contacts ( Same concept as WhatsApp).
I'm using Parse.com as a backend of my app, Users signed up first with their phone numbers, then I should compare all the user's contacts and contacts in backend, to retrieve only the commun contacts ( and store then in SQL)
So do you have any Idea how to make this function better, or any better idea to compare ? it consumes a lot of Bandwidth this function
public void sync_contacts(){
String phoneNo;
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));
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()) {
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNo = phoneNo.replace(" ", "");
phoneNo = phoneNo.replace("+33","0");
Log.d(name, phoneNo);
ParseQuery query = ParseUser.getQuery();
query.whereEqualTo("username",phoneNo);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null ){
contacts c = new contacts();
if (!(parseObject.get("Name").toString()==null)) c.setName(parseObject.get("Name").toString());
if (!(parseObject.get("username").toString()==null)) c.setNumero(parseObject.get("username").toString());
ParseFile image = (ParseFile)parseObject.get("Profilepic");
if(!(image==null))
{
c.setProfil(image.getUrl());
}
c.setShow("true");
s.addcontact(c);
Log.d("Found","IO");
}
else {
Log.d("NotFound","IO");
}
}
});
}
pCur.close();
}
}
}
}
Related
I am trying to fetch contact with name, mobile number, email address and birthday, but operation is taking too much time (by the way device has 2000 approx. contacts). Even UI is hanged, if I show a animation on my custom progress dialog, it is not animating.
(either fast contact fetching or animation should work at least).
(Even, I have used asynctask for both contact fetching and animation but I got same result).
Here is my code
ContactListClass.phoneList.clear();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
int progress = 0;
// customIndeterminantDialog.setMax(cur.getCount());
int i = 0;
while (cur.moveToNext()) {
ContactObject contactObject = new ContactObject();
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) {
// Query phone here. Covered next
JSONObject jsonObject = new JSONObject();
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
String phoneNo = "";
String emailId = "";
while (pCur.moveToNext()) {
// Do something with phones
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNo = phoneNo.replaceAll("\\W+", "");
}
// Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
//
// if (emailCur != null || emailCur.getCount() > 0) {
//
// while (emailCur.moveToNext()) {
// String emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// if (emailContact != null) {
// emailId = emailContact;
//
// } else {
// emailId = "";
// }
// }
// emailCur.close();
// }
contactObject.setName(name);
contactObject.setphone(phoneNo);
contactObject.setEmail(emailId);
contactObjectsList.add(contactObject);
//customIndeterminantDialog.setProgress(++progress);
pCur.close();
// }
i++;
}
customIndeterminantDialog.dismiss();
} else {
customIndeterminantDialog.dismiss();
}
Solved the issue using AsyncTaskLoader
How can I get a list of contacts that either have a phone number or an email in Android.
I'm a bit confused with the contracts and how can I join the info.
Thank you
Here is how I fetched contacts with email and phone number. The Contact object is just a simple pojo that I created. This code is in an AsyncTask that I run after the user has provided permission to access Contacts.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
// get the contact's information
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// get the user's email address
String email = null;
Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if (ce != null && ce.moveToFirst()) {
email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
ce.close();
}
// get the user's phone number
String phone = null;
if (hasPhone > 0) {
Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (cp != null && cp.moveToFirst()) {
phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
cp.close();
}
}
// if the user user has an email or phone then add it to contacts
if ((!TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
&& !email.equalsIgnoreCase(name)) || (!TextUtils.isEmpty(phone))) {
Contact contact = new Contact();
contact.name = name;
contact.email = email;
contact.phoneNumber = phone;
contacts.add(contact);
}
} while (cursor.moveToNext());
// clean up cursor
cursor.close();
}
For DISPLAY_NAME you can use the following:
private final String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
Here is a link to an AsyncTask Example that I use with this.
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));
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()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
I am trying to send all the contacts of a mobile phone to my server.
But I am getting out of memory issue while appending huge number of contacts in StringBuffer. So please need suggestion how can this be resolved. Attaching below my chunk of code.
try{
userContactDetails = new StringBuffer();
Thread t1 = new Thread(new Runnable() {
public void run() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
StringBuffer singleUserDetail = new StringBuffer();
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));
// Create query to use CommonDataKinds classes to fetch emails
Cursor eCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + id, null, null);
StringBuffer phoneNos = new StringBuffer("");
String emailAddress = "";
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()) {
phoneNos.append(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+PHONO_NO_SEPARATOR);
}
pCur.close();
}
while (eCur.moveToNext()) {
// This would allow you get several email addresses
emailAddress = eCur
.getString(eCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//Log.i("SyncContacts", "Email=="+emailAddress);
}
eCur.close();
if(!phoneNos.toString().equals("")){ // phono not null
singleUserDetail.append(name);
singleUserDetail.append(CONTACT_ITEMS_SEPARATOR);
singleUserDetail.append(phoneNos.substring(0,phoneNos.length()-1));
}
if(phoneNos.toString().equals("") && !emailAddress.equals("")){ // phone no null and email not null
singleUserDetail.append(name);
singleUserDetail.append(CONTACT_ITEMS_SEPARATOR);
singleUserDetail.append(emailAddress);
}
if(!phoneNos.toString().equals("") && !emailAddress.equals("")){
singleUserDetail.append(CONTACT_ITEMS_SEPARATOR);
singleUserDetail.append(emailAddress);
}
if(!singleUserDetail.toString().equals("")){
userContactDetails.append(singleUserDetail);
userContactDetails.append(CONTACTS_SEPARATOR);
}
}
}
if(!userContactDetails.toString().equals("")) {
}
}
});
t1.start();
}catch(Exception e ){
e.printStackTrace();
Log.i("SyncContacts", "Server not called : " + e.toString());
}
I am making a contact application but when I get all contact numbers I get duplicate numbers. How can I make sure that I only get unique numbers?
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
strPhontNumberTemp = "";
mPhoneContactsVo = new PhoneContactsVo();
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, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "phoneNumber="+phoneNumber); // Dupblicate number print
}
}
}
}
Use Set Interface to add the phone numbers to avoid duplicate.
Set<String> uniques = new HashSet<String>();
Check this simple example
public static void main(String[] args) {
Set<String> uniques = new HashSet<String>();
Set<String> dups = new HashSet<String>();
for (String a : args)
if (!uniques.add(a))
dups.add(a);
// Destructive set-difference
uniques.removeAll(dups);
System.out.println("Unique words: " + uniques);
System.out.println("Duplicate words: " + dups);
}
from this link ...
http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
I'm trying to get the email adresses and phone numbers from a contact. My class doesn't extend Activity and now I'm stuck.
How can I overcome this problem?
for getting email contacts
public void getEmailContacts()
{
try
{
String name;
ContentResolver cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
emailIndex=0;
if (cur.getCount() > 0)
{
ArrayList<String> emailNameList=new ArrayList<String>();
ArrayList<String> emailPhoneList=new ArrayList<String>();
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
while (emails.moveToNext())
{
// This would allow you get several email addresses
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.v(name+"==>", emailAddress);
if ((!emailAddress.equalsIgnoreCase(""))&&(emailAddress.contains("#")))
{
emailNameList.add(name);
emailPhoneList.add(emailAddress);
emailIndex++;
}
}
emails.close();
}
for getting phone contacts
String name;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
if (cur.getCount() > 0)
{
ArrayList<String> phoneNameList=new ArrayList<String>();
ArrayList<String> PhoneList=new ArrayList<String>();
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
phoneNameList.add(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
while (pCur.moveToNext())
{
PhoneList.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
pCur.close();
}
}