separating country code from mobile number if present android - android

I have a code which fetches phone number from mobile contacts using Content Resolver.I tested it on 2 mobiles.In one it fetches the country code and in another it doesnt.Is there a way to find out if a country code is present in a mobile number and if present I want to separate it from the number.Here is my code.Please help me
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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));
phoneContactName.add(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));
String countryCode = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneContactNos.add(phoneNo);
dup_phoneContactNos.add(phoneNo);
/*if (registeredContactNos.contains(phoneNo)) {
Toast.makeText(getApplicationContext(), "match found", Toast.LENGTH_SHORT).show();
selectedContactName.add(name);
selectedContactNos.add(phoneNo);
populateList(name, phoneNo);
}*/
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
/*for(int i=0;i<selectedContactName.size();i++)
{
Toast.makeText(getApplicationContext(),selectedContactName.get(i)+","+selectedContactNos.get(i),Toast.LENGTH_SHORT).show();
}*/
}
if (cur != null) {
cur.close();
}

You can use Android's PhoneNumberUtils.formatNumber.
If you're targeting only Lollipop and above devices:
String formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
If you're targeting older versions as well:
String formattedPhone;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
} else {
formattedPhone = PhoneNumberUtils.formatNumber(phone));
}

Getting telephone country code with Android
This link worked for me.I am posting the link for all those looking for an answer like me

Related

specific api for accessing contacts app in android

I am making an app, in that app, the user will speak CALL (person name).
But, I am not able to understand how to access contacts and call that person automatically.
Till now, I am able to call a particular number only.
My code is mentioned below.
if(msg.indexOf("call")!=-1){
Intent i2=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"123456789"));
startActivity(i2);
}
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
You can refer this stackoverflow links:
android get all contacts
http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
https://developer.android.com/training/contacts-provider/retrieve-names
You can use below library to fetch contacts
implementation 'com.github.tamir7.contacts:contacts:1.1.7'
https://github.com/tamir7/Contacts

Importing phone contacts doesn't import google contacts saved in local database android

I want to fetch the phone contacts, SIM contacts and Google contacts saved in my phone but I am getting only phone and SIM contacts. If I can access the Google contacts offline it must be stored in the local database somewhere.
How can I import all of them? I have searched everywhere but couldnt find a solution.
pDialog = new ProgressDialog(this);
pDialog.setMessage("Reading contacts...");
pDialog.setCancelable(false);
pDialog.show();
//fetching contacts from phone
contacts_fetched = false;
contacts_refreshed=false;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
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));
//phoneContactName.add(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)).trim();
//getting present country code of mobile
GetCountryZipCode(phoneNo);
if (CountryZipCode != null) {
if (phoneNo.contains("+")) {
phoneContactNos.add(phoneNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
} else {
//adding country code if not present
phoneNo = CountryZipCode + " " + phoneNo;
phoneContactNos.add(phoneNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
phoneContactName.add(name);
}
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
//importing contacts from sim
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
while (cursorSim.moveToNext())
{
simContactName=cursorSim.getString(cursorSim.getColumnIndex("name"));
simContactNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
simContactNo.replaceAll("\\D","");
simContactNo.replaceAll("&", "");
simContactName=simContactName.replace("|","");
System.out.println("SimContacts"+simContactName);
System.out.println("SimContactsNo"+simContactNo);
if(!phoneContactNos.contains(simContactNo))
{
GetCountryZipCode(simContactNo);
if (CountryZipCode != null) {
if (simContactNo.contains("+")) {
phoneContactNos.add(simContactNo);
phoneContactName.add(simContactName);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
} else {
//adding country code if not present
simContactNo = CountryZipCode + " " + simContactNo;
phoneContactNos.add(simContactNo);
//Toast.makeText(this, phoneNo, Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Sim Contact Name: " + simContactName);
Log.i(TAG, "Sim Phone Number: " + simContactNo);
}
}
}
the People API is meanwhile the suggested method - compared to the previous Contacts API:
For read and write access to users' contacts, use the People API, which provides both contact and profile information using JSON instead of the older GData protocol.
instead of a code example, here's the Java quickstart & documentaion.
the dependency for the build.gradle should be:
dependencies {
implementation 'com.google.api-client:google-api-client:1.23.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
implementation 'com.google.apis:google-api-services-people:v1-rev187-1.23.0'
}
by the way, this is no CountryZipCode - but a country (entry) code.

Best way to retrieve contact list

I am developing a contact app having three fragments (callLog,contactList,favoriteContactList). when moving one to another fragment, it takes time to retrieve all contacts. I have approx 276 contacts on my device. Any solution to this which reduce time.
ContentResolver cr = getContext().getContentResolver();
String[] projection = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_URI,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
String id, name, image_uri, phone = "";
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
Log.d(TAG, " name " + name + " id " + id);
// phone number
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);
if (pCur != null && pCur.moveToNext()) {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int colIndex = pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int lblIndex = pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
int labelType = pCur.getInt(colIndex);
String phoneType;
if (labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) {
phoneType = pCur.getString(colIndex);
} else {
phoneType = String.valueOf(ContactsContract.CommonDataKinds.Phone.getTypeLabel(this.getResources(), labelType, "Mobile"));
}
Log.d(TAG, phoneType + " " + phone);
pCur.close();
}
mContactArrayList.add(new Contact(id, name, phone, image_uri));
}
}
cur.close();
}
Try This one code
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 phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
mContactArrayList.add(new Contact(id, name, phone, image_uri));
}
I think it works.

How to get contacts with email or phone number

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

Not Able TO Fetch Local Phone Contact name , Cell Number and Email addresses

In my Application i want to fetch and List out Contact name their Number and there Email addresses i tried but ContactsContract.CommonDataKinds.Email and ContactsContract.Contacts.DISPLAY_NAME conflicts so not able to fetch kindly if possible provide me some code i tried following code
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();
}
}
}
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("Email " + email + " Email Type : " + emailType);
}
emailCur.close();
It looks like the problem with your code may be that what you want should be in two separate queries.
Querying for a list of email accounts or phone numbers uses constants like those in ContactsContract.CommonDataKinds, URI:ContactsContract.CommonDataKinds.Email.CONTENT_URI.
Querying for actual Contact attributes that are not lists like name will use constants like those in ContactsContract.Contacts, URI:based of data returned from Contacts.CONTENT_URI.
The information is not all stored in the same place, and that's probably why there can be conflicts.
switch (reqCode) {
case (REQUEST_CODE_email):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
//Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();
//asdasdasdsa
if(getEmail(num).isEmpty()){
Toast.makeText(this, "Email Not Found In That Contact Try Another", Toast.LENGTH_SHORT).show();
}
else {
edt_email_contact.setText("" + getEmail(num));
} }
}
}
break;
}
case (REQUEST_CODE_number):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();
edt_email_contact.setText("" + num);
}
}
}
break;
}
}

Categories

Resources