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))
Related
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();
My piece of code is here
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
To get message I am using
while (cur.moveToNext()) {
sms +=cur.getString(2) + " : " +
cur.getString(13).replace(" "," ") + " : " +cur.getString(14) +"\n\n\n";
}
In the above cur.getString(int id) 2nd Index is the sender, 13 index is the message, 14th is the number. I am confused at unread messages.
Here
https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns.html
is description of all columns. You need column "read". If it zero - this is unread message.
How to use:
ContentResolver cr = getContentResolver();
Uri inboxURI = Uri.parse("content://sms/");
Cursor c = cr.query(inboxURI, null, null, null, null);
if (c!=null) {
int columnAddress = c.getColumnIndex("address");
int columnBody = c.getColumnIndex("body");
int columnRead = c.getColumnIndex("read");
while (c.moveToNext()) {
String smsFrom = c.getString(columnAddress);
String smsBody = c.getString(columnBody);
int smsRead = c.getInt(columnRead);
if (smsRead == 0) {
// message unread
} else {
// message read
}
}
}
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.
I have a picture, that is stored into the android phone. I want to be able to change the picture of a contact.
What I've done so far is launch the contact picker, have the user select a contact, and then I get the URI of the selected contact. From this contact, I can get the associated rawContact and I use this code.
Uri rawContactPhotoUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
try {
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
OutputStream os = fd.createOutputStream();
os.write(photo);
os.close();
fd.close();
} catch (IOException e) {
// Handle error cases.
}
The problem is, the AssetFIleDescriptor is always empty (when I call length on it, we always get -1).
I'm not asking for the entire solution, just some leads to follow that can help me to get that working. I cannot seem to find this problem already on StackOverflow, so any help would be appreciated.
EDIT
It's always when we ask for question that we find the solution.
I want to share it for other
So I gave up on android link and find another one :
http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/
The picture picker return the Uri of the selected contact, so with this you can get the Contact._ID of it :
// This is onActivityResult
final Uri uri = data.getData();
final Cursor cursor1 = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
final long contactId = cursor1.getLong(cursor1.getColumnIndex(Contacts._ID);
cursor1.close();
Then I had to get the RawContactId :
final Cursor cursor2 = getContentResolver().query(RawContacts.CONTENT_URI, null, RawContacts.Contact_ID + "=?", new String[] {String.valueOf(contactId)}, null);
cursor2.moveToFirst();
final long rawContactId = cursor2.getLong(cursor2.getColumnIndex(RawContacts._ID));
cursor2.close();
Then I had to get the Data._ID of the RawContacts (same way as above).
Then I used the ContentProviderOperations :
final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data._ID, dataId),
.withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArrayOfThePicture);
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
And this is working like charm. Hope it helps
String contactId = "10001"; // change it as your IDs
if (mBitmap != null) {
// Picture
try {
ByteArrayOutputStream image = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, image);
Uri rawContactUri = null;
Cursor rawContactCursor = managedQuery(
ContactsContract.RawContacts.CONTENT_URI,
new String[] {
ContactsContract.RawContacts._ID
},
ContactsContract.RawContacts.CONTACT_ID + " = " + contactId,
null,
null);
if (!rawContactCursor.isAfterLast()) {
rawContactCursor.moveToFirst();
rawContactUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendPath("" + rawContactCursor.getLong(0)).build();
}
rawContactCursor.close();
ContentValues values = new ContentValues();
int photoRow = -1;
String where111 = ContactsContract.Data.RAW_CONTACT_ID + " == " +
ContentUris.parseId(rawContactUri) + " AND " + ContactsContract.Data.MIMETYPE + "=='" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = managedQuery(
ContactsContract.Data.CONTENT_URI,
null,
where111,
null,
null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if (cursor.moveToFirst()) {
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID,
ContentUris.parseId(rawContactUri));
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
getContentResolver().update(
ContactsContract.Data.CONTENT_URI,
values,
ContactsContract.Data._ID + " = " + photoRow, null);
} else {
getContentResolver().insert(
ContactsContract.Data.CONTENT_URI,
values);
}
} catch (Exception e) {
Log.e("!_##Image_Exception", e + "");
}
}
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
Log.e("#####UPLOADERR", e + "");
}
i'm trying to read contacts with this code but it only gets Contacts but not contacts data.Please
tell me am i going on a wrong path or what is wrong with this.
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(contacts.moveToNext()) {
int contactIdColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts._ID);
long contactId = contacts.getLong(contactIdColumnIndex);
System.out.println(contactId);
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);
//retrieving data
while(c.moveToNext()){
long Id=c.getLong(0);
String number=c.getString(1);
String type=c.getString(2);
String name=c.getString(3);
System.out.println(Id);
System.out.println(number);
System.out.println(type);
System.out.println(name);
}
c.close();
}
contacts.close();
This runs well when i debug this and it only print ContactIds only
1
2
3
4
but no data...(apologize for long question)
try this code
String 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));
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,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
for(int i=0;i<pCur.getColumnCount();i++)
number = pCur.getString(i);
}
pCur.close();
pCur = null;
}
}
}
cur.close();
cur = null;
cr = null;