I am using contact picker to chose a phone number from phone.But after using the contact picker once I click on any other button or menu, the app gets restarted.
Below is my code :- Here mobilText is the textbox where I am setting the phone number which I get from contact picker.
contact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
String cNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mobilText.setText(cNumber+"");
}
}
Related
In an android project i had this intent to pick a phone number from my contact list
btnContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Open Contacts
Intent intent= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
and this to get the result
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
insertedPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
formatContact(insertedPhoneNumber, name);
}
}
break;
}
}
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
No. You ask for phone numbers you get phone numbers. If you need additional logic or ifs, then you need to code it yourself.
I created an onActivityResult method. I am in the contact list, Upon clicking I should get the name and the phone number of the contact, but it crashes.
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();
}
}
}
}
Please note, if I remove the line
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it doesn't crash. (But I don't get the phone number.
my app must picks one contact.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(pickContact, REQUEST_CONTACT);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CONTACT && intent != null) {
Uri contactUri = intent.getData();
String[] queryFields = new String[]{Contacts.DISPLAY_NAME_PRIMARY} ;
Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null);
try {
if (c.getCount() == 0) {
return;
}
c.moveToFirst();
String contact = c.getString(0);
mButton.setText(contact);
} finally {
c.close();
}
}
}
But it works fine only with contacts stored in my phone. If contact stored in Google account, it wouldn't work. I'll get empty cursor. I've read this and this. And i've read Google Contacts API. These methods are completely different. What should i do if i want get data from any contacts (Google, phone, sim) by pressing single button?
I need to ask a user for a contact number to make a call. On Button Click the User should be directly redirected to Contacts Book and the user can select a Phone Number. Following is the Source Code what I am using now.
Button buttonReadContact;
TextView textPhone;
final int RQS_PICKCONTACT = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonReadContact = (Button)findViewById(R.id.readcontact);
textPhone = (TextView)findViewById(R.id.phone);
buttonReadContact.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
//Start activity to get contact
final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, RQS_PICKCONTACT);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(resultCode == RESULT_OK){
if(requestCode == RQS_PICKCONTACT){
Uri returnUri = data.getData();
Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
if(cursor.moveToNext()){
int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = cursor.getString(columnIndex_ID);
int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
if(stringHasPhoneNumber.equalsIgnoreCase("1")){
Cursor cursorNum = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID,
null,
null);
//Get the first phone number
if(cursorNum.moveToNext()){
int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String stringNumber = cursorNum.getString(columnIndex_number);
textPhone.setText(stringNumber);
}
}else{
textPhone.setText("NO Phone Number");
}
}else{
Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
}
}
}
}
But now the issue is I can only select one number from a contact which is having multiple Phone Numbers saved.
I need to do this as in Skype Application. When the User select a contact which is having multiple contacts, from the Contacts Book itself it should ask the User to choose the number. Please help me to do it.
I used this code to open the Contacts and allow the user to select a single contact, then parse the result to display the contact name, phone number and thumbnail photo. In the example below, the members mName, mPhoneNumber, and mContactImage are already defined.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start an activity to pick a single contact (ACTION_PICK)
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// Show only contacts with phone numbers
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
// Start the Contacts activity
startActivityForResult(intent, PICK_CONTACT);
}
});
Parse the results in onActivityResult().
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_CONTACT :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI};
Cursor c = getActivity().getContentResolver().query(contactData, projection, null, null, null);
c.moveToFirst();
int nameIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int photoIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI);
String name = c.getString(nameIdx);
String phoneNumber = c.getString(phoneNumberIdx);
String photo = c.getString(photoIdx);
if (photo == null) {
// If no photo then substitute a dummy image
mContactImage.setImageResource(R.drawable.ic_contact_picture);
} else {
// Display the contact photo
final Uri imageUri = Uri.parse(photo);
mContactImage.setImageURI(imageUri);
}
if (name == null) {
name = "No Name";
}
mName.setText(name);
mPhoneNumber.setText(phoneNumber);
c.close();
// Now you have the phone number
}
break;
}
}
I think this answers your question.
I have a button that I click and that shows contacts. It displays the name, but not the number of a selected contact...
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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));
contactName.setText(name);
Something here !!!!!!!!!!!!!!!!!!!!!!!!
contactName.setText(number);
}
}
}
}
It String the NAME but how can I get the phone number?
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
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 number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactName.setText(name);
contactNumber.setText(number);
}
}
With this code it opens my contacts. I touch one, but it always display the LAST name and number that I have added to contacts.
For example, I touch Adrian (22222222) and it shows Paul (33333333) which is the last person that I have added.
What should I do?
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));