简体   繁体   中英

Is it possible to pick a phone number of a specific contact?(Android)

I want to allow the user to pick a phone number of a specific contact in my application by the startActivityForResult() method. Is it possible? For example, if the contact has a home phone number and and a mobile phone number the user will be able to pick one of them.

The following code allows the user to pick one contact:

    Intent picker = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(picker, 53253);

The following code allows the user to pick a phone number from any contact(not from a specifc contact):

    Intent picker = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(picker, 53253);

Use this code get a contact in Txt file

Its my code ,it will help you

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView textDetail;
 StringBuffer sb;




 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textDetail = (TextView) findViewById(R.id.textView1);
  readContacts();      

  }


 public void readContacts() {
   sb = new StringBuffer();
  ////sb.append("......Contact Details.....");
  ContentResolver cr = getContentResolver();
  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    null, null, null);

  String phone = null;
  String emailContact = null;
  String emailType = null;
  String image_uri = "";
  Bitmap bitmap = 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));

    image_uri = cur
      .getString(cur
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    if (Integer
      .parseInt(cur.getString(cur
        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
     System.out.println("name : " + name + ", ID : " + id);
     sb.append("\nContact Name:" + name+"                                                      ");
     Cursor pCur = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
       null,
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID
         + " = ?", new String[] { id }, null);
     while (pCur.moveToNext()) {

      phone = pCur
        .getString(pCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      sb.append("\nPhone number:" + phone+"                                                 ");
      System.out.println("phone" + phone);


     }
     pCur.close();

     Cursor emailCur = cr.query(
       ContactsContract.CommonDataKinds.Email.CONTENT_URI,
       null,
       ContactsContract.CommonDataKinds.Email.CONTACT_ID
         + " = ?", new String[] { id }, null);
     while (emailCur.moveToNext()) {
      emailContact = emailCur
        .getString(emailCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
      emailType = emailCur
        .getString(emailCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
      sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
      System.out.println("Email " + emailContact
        + " Email Type : " + emailType);


     }

     emailCur.close();
    }

    if (image_uri != null) {
     System.out.println(Uri.parse(image_uri));
     try {
      bitmap = MediaStore.Images.Media
        .getBitmap(this.getContentResolver(),
          Uri.parse(image_uri));
      sb.append("\n Image in Bitmap:" + bitmap);
      System.out.println(bitmap);

     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

    }


    //sb.append("\n........................................");
   }

   textDetail.setText(sb);
   File logFile = new File("sdcard/log.txt");
   if (!logFile.exists())
   {
      try
      {
         logFile.createNewFile();
      } 
      catch (IOException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   try
   {
      /////BufferedWriter for performance, true to set append to file flag
      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
      buf.append(sb);
      buf.newLine();
      buf.close();
   }
   catch (IOException e)
   {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }

    }


  }
 }

Use permission in Mani fest

<uses-permission android:name="android.permission.READ_CONTACTS" /> 




  <?xml version="1.0" encoding="utf-8" ?> 
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contact" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> 
  <uses-permission android:name="android.permission.READ_CONTACTS" /> 
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
- <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
- <activity android:name="com.example.contact.MainActivity" android:label="@string/app_name">
- <intent-filter>
  <action android:name="android.intent.action.MAIN" /> 
  <category android:name="android.intent.category.LAUNCHER" /> 
  </intent-filter>
  </activity>
  </application>
  </manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM