I am trying to access the contact details of a person which i selected from contact picker intent.
here is what my contact looks like:
Here is the code which i am using to open the contact picker:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
Now, i am able to get Phone number and email using following API's:
android.provider.ContactsContract.CommonDataKinds.Email;
android.provider.ContactsContract.CommonDataKinds.Phone;
but i am not able to get the address which is stored. I want to get both the address value and custom tag associated with it.
Any help is appreciated.
if You have a contact ID and you want to fetch the Postal Address then use this :
Uri postal_uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
Cursor postal_cursor = getContentResolver().query(postal_uri,null, ContactsContract.Data.CONTACT_ID + "="+contactId.toString(), null,null);
while(postal_cursor.moveToNext())
{
String Strt = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.STREET));
String Cty = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.CITY));
String cntry = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.COUNTRY));
}
postal_cursor.close();
http://gabrielaradu.com/?p=367
https://stackoverflow.com/a/13471370/2480911
Related
I'm developing an sms blocker for which I need to save contact numbers in DB and compare them to SMS sender. I've only default SMS application installed on my android phone. But there is difference of contact number format as picked from following code:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
and when I receive SMS using broadcast receiver (see following code segment):
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String number = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
The format of phone # in my contact list is like "033-xxxx-xxxx" without country code but SMS Broadcast Receiver adds country code to the number like "+92331xxxxxxx".
My question (a) is why SMS receiver is using different format then that is saved in my contact lists and (b) how do I compare these numbers in my SMS broadcast receiver.
Check out PhoneNumberUtils.You need to let the O/S help you because it can be international number.
Use String incomingNumber = PhoneNumberUtils.formatNumber(incomingNumber,defaultCountryIso);
on both numbers, then compare them.
To compare you do this:
boolean SameNumber= PhoneNumberUtils.compare(phoneNumber, otherPhoneNumber)
//if true, numbers are the same
I want to open Contacts pick activity from my application with search field should be filled programmatically.
Can anyone suggest what URI shoud i use or anything to put in intent's extra?
private static final int PICK_CONTACT_SUBACTIVITY = 2;
private void startContactActivity() {
Uri uri = Uri.parse("content://contacts/people");
// Here in this normally we pass number e.g. Uri.encode("987") but i want to pass name as filter is it possible?
// I have also tried
//uri = Uri.withAppendedPath(android.provider.ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode("pra"));
//uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("pra"));
uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode("pra"));
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
}
Can anyone suggest how can i achieve this?
You can follow :
how-to-pick-contact-number-from-phone-book-of-android-in-to-my-application
or
developer.android.com/training/contacts-provider/modify-data
It seems that it is not possible to specify a filter programmatically.
The Android SDK documentation states (in the chapter "Retrieval and modification with intents") that for ACTION_PICK you can only use one of
Contacts.CONTENT_URI, Phone.CONTENT_URI, StructuredPostal.CONTENT_URI, Email.CONTENT_URI. This is indirectly a filter (all contacts, all with phone numbers, all with postal address, or all with email), but it is limited to this.
Context:
I wrote a small easy dialer for android which dials telephone numbers
for contacts.
I use Intent.ACTION_CALL and pass in "tel:" for contacts with just one tel number or with a preferred default number
Within the app I have the contact's lookup_key so that I can access the contact
Question:
I want to initiate a call to a Contact not to a number so that Android:
if contact has 2+ numbers: will fire up a phone number selection dialog and then initiate the call
if the contact has just 1 number: just initiate the call
I could of course implement a popup from scratch but I'd prefer to delegate to a standard action so that the user has the very same UX as using the standard dialer.
You can use something like this to prompt the user to choose the contact, then which phone number to call (if there is more than one)... than pass that to the intent to make the call:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String theID = contactData.toString());
//MAKE YOUR CALL .. do whatever... example:
ContentResolver contentResolver = getContentResolver();
Uri contactData = Uri.parse(theID);
Cursor cur = contentResolver.query(contactData, null, null, null, null);
String theNumber = cur.getString(cur.getColumnIndex("data4"));
cur.close();
Intent my_callIntent = new Intent(Intent.ACTION_CALL);
my_callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
my_callIntent.setData(Uri.parse("tel:" + theNumber));
startActivity(my_callIntent);
}
}
Its not pretty or perfect, probably needs some modifications, just kinda going off the top of my head but hopefully you get the idea.
I'm creating an Sms app which contains a ListView in the Main Activity that displays all the conversations from the sms inbox. Each ListView row displays one conversation along with the phone number, message body and time of the message. Now instead of the phone number I want to display the contact name if it exists.
So far, for getting the contact name by phone number I found this code
private String getDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor contactLookup = context.getContentResolver().query(uri, new String[] {ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
int indexName = contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
try {
if (contactLookup != null && contactLookup.moveToNext()) {
number = contactLookup.getString(indexName);
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return number;
}
But this seems inefficient as it has to make a query for each contact name individually and lags the app. So instead I tried to get all the contact names from the phone and store it in an HashMap with the phone number as the key and the contact name as the value, so that I can get the contact name any time I want from the HashMap. But there seems to be another problem, the phone numbers are stored in many different formats, for eg:
+91 4324244434
04324244434
0224324244434
So how do I search for a phone number from the HashMap since it can be stored in many different formats?
Before you add the contact to the HashMap, use regular expression to grab the phone number. This way, no matter what format the phone number is in, the regular expression will be able to grab the appropriate number needed.
Once you grab the number, add it to the HashMap accordingly.
Hope that answers your question.
Moving on from my original question below:
Android Sending an email using a list of email addresses
I have tried to populate the email address line by using a string. When writing the email the message section is populated with the answer given by clicking on a spinner, the code for this is shown as "%1$s". So if I have a spinner that has 4 different countries in the string for the main email I put:
Country: %1$s
And it will show in the final email to be sent as:
Country: UK
For example.
When I put this string into the email Address instead of it originally looking like:
home%1$s#gmail.com
And coming out in the final email as:
homeUK#gmail.com
It stays as home%1$s#gmail.com!
Is there a reason for this? Is it something that cannot be added to the address line?
I have attached my code below and would very much appreciate someones help. I have been pulling my hair out with this one. The rest of the app is complete!
Main Activity
public void sendFeedbackMessage(String subject, String message) {
Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { getResources().getString(R.string.emailaddress_format) };
String bEmailList[] = { ("country#gmail.com") };
messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
messageIntent.putExtra(Intent.EXTRA_CC, bEmailList);
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
messageIntent.setType("plain/text");
messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(messageIntent);
}
}
And the relevant String
<string
name="emailaddress_format">country%1$s#gmail.com</string>
If any more code is needed to see what I am trying to do then please let me know and I will add it. I didnt want to overload everyone with the whole code and then try and explain where I am struggling.
Hopefully someone can help me and i'm making sense.
Many Thanks
Not sure on the exact answer to the particular problem you're having, but an easier way to accomplish what it seems like you're trying to accomplish would be:
Spinner yourSpinner = (Spinner)findViewById(R.id.yourSpinner);
String country = yourSpinner.getSelectedItem().toString();
String aEmailList[] = { "country" + country + "#gmail.com" };
This will get the country the person has selected from the spinner and convert it to a String, which you can then place in the EXTRA_EMAIL as you're doing already.