How to select multiple contacts at a time? - android

I retrieve phonebook contacts to my application by using following code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
But I want to select multiple contacts and upload those to a DB. Is this possible and if so how can I do this?

Well you could query the content provider directly inside your activity so the user can select multiple contact. This can be achieved using the contacts contract. For more information you can look at the api documentation or this tutorial blog post

try this code..
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int position=0;
Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
people.moveToFirst();
int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
while(!people.isAfterLast()) {
Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
if(!(o.getCount()>0))
{
mConname.add(position, people.getString(nameFieldColumnIndex));
try{
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mConno.add(position,phoneNumber);
}
phones.close();
}
if(hasPhone=="false")
{ mConname.remove(position);
}
else
position++;
}
catch(Exception e)
{
}
}
people.moveToNext();
}
use the sqlite and store it

Related

Getting phone number from contacts using ContentProvider - Android

I am making a small app where I can get the contacts of my phone using a content provider and display them in a listview as illustrated.
I want to select a row of the listview and automically make a phone call to that specific contact. I tried some things,but they don't work. Any ideas? Here is my code.
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener{
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
null, null, null);
List<String> contacts = new ArrayList<String>();
if (c.moveToFirst()) {
do {
contacts.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
} while (c.moveToNext());
}
adapter = new ArrayAdapter<String>(this, R.layout.activity_main, contacts);
setListAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//The answer should be inside here.
}
}
First, ensure that you have added the Permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
UPDATE:
On Android 6 and higher stating the permission in the manifest is not enough, you have to explicitly ask user for granting permission on reading contacts otherwise, you will get an exception. See this answer for more details.
Then you can loop through your phone contacts like this:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
Try with:
private void doMagicContacts() {
Cursor cursor = getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
if (cursor == null) {
return;
}
cursor.moveToFirst();
do {
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID+" = " + id,
null,
null);
if (phones != null) {
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG, "doMagicContacts: " + name + " " + phoneNumber);
}
phones.close();
}
} while (cursor.moveToNext());
cursor.close();
}

How to get Contact Information in Android?

I want to get Contact Image, Name, Number from my mobile using android. im using ContactsContract to fetch these information. but i can't. can anyone know the exact tutorial for learning. i want to list these info in a custom listview. Thanks in Advance. i hope your valuable guiding will help me.
PS: In the DB i have my friends number. i want to synchronize the phone contact and the DB contact using the matched phone no. and i need to display all the matched contacts in the listview. the listview have imageView, name, number..
setReminder.numbers.clear();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
((Activity) mcontext).startActivityForResult(intent, 1);
and on activity result do this
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
{
getContactInfo(intent);
}
}
protected void getContactInfo(Intent intent)
{
String phoneNumber = "";
//String email="";
Cursor cursor = ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
//Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber);
}
phones.close();
Cursor emailCur = ((Activity) mcontext).getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,
null, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
if(email==null)
{
email="";
}
}
}
if(phoneNumber != null && phoneNumber.length() > 0){
chooseContactArray.clear();
for(int i=0;i<numbers.size();i++)
{
chooseContactArray.add(numbers.get(i));
}
adapter.notifyDataSetChanged();
}
cursor.close();
}

android unable to get contacts from some phones

I wrote an app that needs to read the contacts from the user's phone. Since I've put it on the market, a few users have told me that my app cannot read their contact list (it pops up an error message saying it can't get the contact list).
For roughly 80% of the users, everything works fine, but others have this problem.
In my MainActivity, I call the method: GetContacts()
If that method returns no contacts, then I call: GetContacts2()
If the app still can't get the contacts, then it pops up the error message. Below are the two methods I'm using. Some of the phones, this doesn't work, are Droid X2 and HTC Incredible.
I'm also setting the required API level to 8 in my manifest, so that eliminates older phones. I also do not have log files from these users.
NOTE: I've removed extra code that simply puts the contacts in an array for simplicity's sake.
Any ideas on why these methods don't work on all phones?
Any help would be greatly appreciated!!
Mark
private void GetContacts()
{
try
{
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhone)==1)
{
Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phone.moveToNext())
{
phoneNo = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneType = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
}
}
}
}
catch (Exception e)
{
}
}
private void GetContacts2()
{
try
{
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
contactId = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
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[]{contactId}, null);
while (pCur.moveToNext())
{
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
}
pCur.close();
}
}
}
}
}
catch (Exception e)
{
}
}
I agree. Exceptions are definitely not meant to be tossed aside. They're exceptions; they indicate that something is seriously wrong and needs to be looked at. I recommend you at least log them, so that users that run into the problems can send you a LogCat. If you don't want your users to have to go through that trouble, write data to a file on the SD card (public), so that they can access it and send it to you.
//declaration
Cursor cursor;
int cNameIndex = 0;
int cNumIndex = 0;
String [] arrPosDrawn= null;
//oncreate
TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
AlertDialog.Builder alert = new AlertDialog.Builder(
ContactList.this);
alert.setMessage("Please insert Sim card.")
.setTitle("Alert")
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
}).show();
break;
case TelephonyManager.SIM_STATE_READY:
cursor = refreshContactsCursor();
//contacts.setAdapter(new EfficientAdapter(this));
break;
}
//function
public Cursor refreshContactsCursor()
{
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
cursor.moveToFirst();
cNameIndex = cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
cNumIndex = cursor.getColumnIndex(ContactsContract.Data.DATA1);
arrPosDrawn = new String[cursor.getCount() - 1];
for (int j=0; j < arrPosDrawn.length; j++)
{
arrPosDrawn[j] = "";
}
return cursor;
}

ContactPicker filtered

In a simple Android App I need to get phone number from Address Book using Contacts picker.
To open Contact picker I use following code:
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
All works fine but in Contacts Picker I can see contacts from gmail, if I use Contact Picker in Phone App I see only Addess book's contacts. How can I show a Contact Picker as Phone App (without gmail contacts)?
This helped me Filter just for phone numbers:
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
ArrayList<String> phoneNumber = new ArrayList<String>();
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
phoneNumber.add(name);
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
hasPhone = "true";
} else {
hasPhone = "false";
}
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = null;
phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber
.add(phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
}
}
cursor.close();

Android Api - get mobile number from contacts

I tried so much tutorials, and read a lot here at SO, but i cant solve my problem:
When a button is clicked, the user can choose the mobile number of a contact. Actual I can get the name of the selected contact, but i can't find a way, to get/chose the mobile number..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Layouting */
this.mGetMobileNumberButton = (Button)findViewById(R.id.getMobileNumberButton);
this.mNameTextView = (TextView)findViewById(R.id.nameTextView);
this.mMobileNumberTextView = (TextView)findViewById(R.id.mobileNumberTextView);
/** onClick getContactInfos*/
this.mGetMobileNumberButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
mNameTextView.setText(name);
}
}
}
Hope anyone can help :)
This will get the cursor holding base contact data, and will loop through the phone numbers the contact has, can have multiple.
Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
Is unless when you get all contact from phone and after that you test the result by one to one if has has number or not. You can set condition into query function.
Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null, null);
The rest of code will be the same without this test:
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {

Categories

Resources