I am developing an android app, in which I am taking contacts from contact list and showing in listview, when I display mobile number if user has two mobile number saved in a contact, then in listview his name is repeated and second mobile number is shown below his first name, so if two mobile number is saved for a particular number I need to select only first number, so what change I need to do in following code, if anybody know please help.
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
ArrayList<Contact> contacts = new ArrayList<Contact>();
while (c.moveToNext())
{
int type = c.getInt(c.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_MOBILE)
{
Contact con = new Contact(c.getString(c
.getColumnIndex(Contacts.DISPLAY_NAME)), c.getString(c
.getColumnIndex(Phone.NUMBER)));
contacts.add(con);
}
}
public void readContacts() {
Cursor contactsCursor =getActivity().getContentResolver().query(ContactsContract
.Contacts.CONTENT_URI, null, null, null,ContactsContract.Contacts.DISPLAY_NAME);
mContactsMetaDataList = new ArrayList<>();
try {
Set<String> idSet = new HashSet<>();
if (contactsCursor.getCount() > 0) {
while (contactsCursor.moveToNext()) {
String id =contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts._ID));
String name =contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(contactsCursor.getString(contactsCursor.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
idSet.add(name);
if (!idSet.contains(name)){
//add contact to your list here
}
}
}
}
}finally {
if (contactsCursor != null) {
contactsCursor.close();
}
}
}
Related
I want to get the name for incoming call number from contact list through the following method. it work on all android version but it give null in android 9.0 pie.
private String getContactName(String number, Context context) {
String contactName = "";
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER,
ContactsContract.PhoneLookup.HAS_PHONE_NUMBER };
Uri contactUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
Cursor cursor = context.getContentResolver().query(contactUri,
projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return contactName.equals("") ? number : contactName;
}
i also try a new method but it also not working and give me null value. i get this code from google developer documentation. whats wrong with my code or please help me on which method i can get contact name for a number.
public String onCreateIncomingConnection (String s) {
// Get the telephone number from the incoming request URI.
String phoneNumber = s;
String displayName = "Unknown caller";
boolean isCallerInWorkProfile = false;
// Look up contact details for the caller in the personal and work profiles.
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = getContentResolver().query(
lookupUri,
new String[]{
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.CUSTOM_RINGTONE
},
null,
null,
null);
// Use the first contact found and check if they're from the work profile.
if (cursor != null) {
try {
if (cursor.moveToFirst() == true) {
displayName = cursor.getString(1);
isCallerInWorkProfile =
ContactsContract.Contacts.isEnterpriseContactId(cursor.getLong(0));
}
} finally {
cursor.close();
}
}
// Return a configured connection object for the incoming call.
// MyConnection connection = new MyConnection();
// connection.setCallerDisplayName(displayName, TelecomManager.PRESENTATION_ALLOWED);
//
// Our app's activity uses this value to decide whether to show a work badge.
// connection.setIsCallerInWorkProfile(isCallerInWorkProfile);
// Configure the connection further ...
return displayName;
}
I am trying to get the SimInfo of a dual sim device using this content provider
public static List<SimInfo> getSIMInfo(Context context) {
List<SimInfo> simInfoList = new ArrayList<>();
Uri URI_TELEPHONY = Uri.parse("content://telephony/siminfo/");
Cursor c = context.getContentResolver().query(URI_TELEPHONY, null, "slot>=0", null, null);
if (c!=null && c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("_id"));
int slot = c.getInt(c.getColumnIndex("slot"));
String display_name = c.getString(c.getColumnIndex("display_name"));
String icc_id = c.getString(c.getColumnIndex("icc_id"));
String number=c.getString(c.getColumnIndex("number"));
SimInfo simInfo = new SimInfo(id, display_name, icc_id, slot, number);
Log.d("apipas_sim_info", simInfo.toString());
simInfoList.add(simInfo);
} while (c.moveToNext());
c.close();
}
return simInfoList;
}
it is working on API 17 but for API >=22 the Cursor returns null.
I am facing one problem and not getting solution on internet.
I am able to list all user profile contacts but its not showing contacts from work profile.
please refer to below links for detail about work profile
https://support.google.com/work/android/answer/6191949?hl=en
https://support.google.com/work/android/answer/7029561?hl=en
`
private static final String[] PROJECTION =
{
Contacts._ID,
Contacts.LOOKUP_KEY,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME
};
private static final String SELECTION =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
Contacts.DISPLAY_NAME + " LIKE ?";
#Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
/*
* Makes search string into pattern and
* stores it in the selection array
*/
mSelectionArgs[0] = "%" + mSearchString + "%";
// Starts the query
return new CursorLoader(
getActivity(),
Contacts.CONTENT_URI,
PROJECTION,
SELECTION,
mSelectionArgs,
null
);
}
`
For example: i have a contact with name "todd" in normal profile on the other hand i have a "tom" in contact under my work profile. Now in native message app during compose it shows todd and tomm both. i want same to happen in my implementation.
How should i get work profile contacts?
refer to the code below that solved my problem
private static final String[] PROJECTION_ENTERPRISE = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.DATA1,
ContactsContract.CommonDataKinds.Phone.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL
};
#RequiresApi(api = Build.VERSION_CODES.N)
private Cursor getEnterpriseContact(String searchString, String[] cols, ContactSearchType mContactSearchType, String digits, String sortOrder) {
// Get the ContentResolver
ContentResolver cr = mContext.getContentResolver();
// Get the Cursor of all the contacts
Uri phoneUri = ContactsContract.CommonDataKinds.Phone.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
Uri phoneUriWithSearch = Uri.withAppendedPath(phoneUri, Uri.encode(searchString));
Cursor pCursor = cr.query(phoneUriWithSearch, cols, null, null, sortOrder);
Cursor workCur = null;
if (mContactSearchType != ContactSearchType.CONTACT_WITH_PHONE_NO) {
Uri emailUri = ContactsContract.CommonDataKinds.Email.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.ENTERPRISE_DEFAULT)).build();
Uri emailUriWithSearch = Uri.withAppendedPath(emailUri, Uri.encode(searchString));
Cursor eCursor = cr.query(emailUriWithSearch, cols, null, null, sortOrder);
workCur = new MergeCursor(new Cursor[]{pCursor, eCursor});
} else {
workCur=pCur;
}
return workCur;
}
I am working in Android application in which I am using ormlite. I am taking my phone book contacts and saving them in my local database, but the problem is that it is taking too much time like for almost 1500 contact it is taking almost 70 seconds.
I searched for the Bulk insert in ormlite, but I can't figure it out how to implement it in my following code.
public static void loadLocalPhoneBookSample(Context ctx) {
try{
ContentResolver contentRes = ctx.getContentResolver();
Cursor cur = null;
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
cur = contentRes.query(ContactsContract.Contacts.CONTENT_URI, PROJECTIONS, selection, null, Phone.DISPLAY_NAME + " ASC");
context = ctx;
if (cur.getCount() > 0) {
// create DB object
MUrgencyDBHelper db = new MUrgencyDBHelper(ctx);
RuntimeExceptionDao<ContactLocal, ?> contactDAO = db.getContactLocalIntDataDao();
UpdateBuilder<ContactLocal, ?> updateDAO = contactDAO.updateBuilder();
try {
updateDAO.updateColumnValue("isUseless", true);
updateDAO.update();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// db.writeUnlock();
}
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** Phone Numbers **/
Cursor pCur = contentRes.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatedNo = number.replaceAll("\\s+", "").replace("+", "00").replace("-", "").trim();
try {
QueryBuilder<ContactLocal, ?> query = contactDAO.queryBuilder();
query.where().eq("mFormatedNumber", number);
ContactLocal contact = query.queryForFirst();
boolean addContact = false, alreadyUpdated = true;
if (contact == null) {
addContact = true;
contact = new ContactLocal();
contact.setFirstName(displayName.trim());
contact.setLastName(displayName.trim());
contact.setContactNumber(formatedNo);
}
// check if this contact was already updated before
if (contact.getContactNumber() == null || contact.getContactNumber().length() == 0) {
contact.setContFirstLastNo(number, displayName, displayName, number);
alreadyUpdated = false;
}
contact.setUseless(false);
// if not updated already, Create/Update
if (addContact) {
contactDAO.create(contact);
} else
contactDAO.update(contact);
}
}
pCur.close();
}
}
}
the problem is that it is taking too much time like for almost 1500 contact it is taking almost 70 seconds
#CarloB has the right answer in terms of doing the mass creates inside the dao. callBatchTasks(...) method. Here's the docs on that subject:
http://ormlite.com/docs/batch
To make things a bit faster, you could also go through and record all of the mFormatedNumber in another List and then query for them using an IN query. Use a raw in query to get back the mFormatedNumber that are already in the database:
results = dao.queryRaw(
"SELECT mFormatedNumber from Contact WHERE mFormatedNumber IN ?",
mFormatedNumberList);
For using raw queries with ORMLite, see:
http://ormlite.com/docs/raw-queries
So then you would make one query to see which of the contacts need to be created and then do all of the inserts from within a batch transaction.
Otherwise you are doing ~3000 synchronous database transactions and 40/sec on an Android device is unfortunately pretty typical.
Here is my revised version (might need a few syntax changes)
public static void loadLocalPhoneBookSample(Context ctx) {
try {
ContentResolver contentRes = ctx.getContentResolver();
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Cursor cur = contentRes.query(ContactsContract.Contacts.CONTENT_URI, PROJECTIONS, selection, null, Phone.DISPLAY_NAME + " ASC");
context = ctx;
if (cur.getCount() > 0) {
// create DB object
MUrgencyDBHelper db = new MUrgencyDBHelper(ctx);
RuntimeExceptionDao<ContactLocal, ?> contactDAO = db.getContactLocalIntDataDao();
UpdateBuilder<ContactLocal, ?> updateDAO = contactDAO.updateBuilder();
try {
updateDAO.updateColumnValue("isUseless", true);
updateDAO.update();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// db.writeUnlock();
}
ArrayList<ContactLocal> contacts = new ArrayList<>();
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
/** read names **/
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/** Phone Numbers **/
Cursor pCur = contentRes.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String formatedNo = number.replaceAll("\\s+", "").replace("+", "00").replace("-", "").trim();
try {
QueryBuilder<ContactLocal, ?> query = contactDAO.queryBuilder();
query.where().eq("mFormatedNumber", number);
ContactLocal contact = query.queryForFirst();
if (contact == null) {
contact = new ContactLocal();
contact.setFirstName(displayName.trim());
contact.setLastName(displayName.trim());
contact.setContactNumber(formatedNo);
}
contact.setUseless(false);
contacts.add(contact);
}
}
pCur.close();
}
contactDao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
for (ContactLocal contact : contacts) {
contactDAO.createOrUpdate(contact);
}
}
});
}
}
The main optimization is to use callBatchTasks. From the ormlite documentation:
Databases by default commit changes after every SQL operation. This method disables this "auto-commit" behavior so a number of changes can be made faster and then committed all at once.
By creating an ArrayList and keeping track of the changes, you can use callBatchTasks to create/update at the end all in one shot.
Also I noticed that alreadyUpdated was never accessed, so it's safe to remove.
Also Dao has a createOrUpdate method which is the same as the addContact if statement you had before.
I have been developing the application which must recognize SMS commands. So, I need to know how I can get the last sms. I know that I need to use BroadcastReceiver (and I use it), but I don't know how I can get the last sms in this class. Please, help me with it, I hope you can do it. Thank you in advance.
thanks Bo for the link to my blog.
To get the last message from your embeded android database (sqlite) you have at first create a cursor instance.
Cursor cursor = context.getContentResolver().query("content://sms", null, null, null, null);
and then move to the first sms (the first is the last receiver ;))
cursor.moveToFirst();
Look at my blog how I did it as Bo told you ;)
// removed link
Cheers
see below code it may help you.
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = con.getContentResolver();
Cursor c = cr.query(myMessage, new String[] { "_id",
"address", "date", "body", "read" }, null,
null, null);
startManagingCursor(c);
Main_calss.getSmsLogs(c, con);
public static final ArrayList<String> sms_num = new ArrayList<String>();
public static final ArrayList<String> sms_body = new ArrayList<String>();
public static void getSmsLogs(Cursor c, Context con) {
if (sms_num.size() > 0) {
sms_num.clear();
sms_body.clear();
}
try {
if (c.moveToFirst()) {
do {
Log.d("error",
""
+ c.getString(c
.getColumnIndexOrThrow("address")));
if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
c.moveToNext();
continue;
}
String Number = c.getString(
c.getColumnIndexOrThrow("address")).toString();
String Body = c.getString(c.getColumnIndexOrThrow("body"))
.toString();
sms_num.add(Number);
sms_body.add(Body);
} while (c.moveToNext());
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
now get last massage you have to do this.
sms_num.get(sms_num.size()-1);
sms_body.get(sms_num.size()-1);
if it is correct then make it right.