This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 9th, 2013  |  syntax: None  |  size: 2.56 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class ContactTutorialActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7.  
  8. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  9. intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
  10.  
  11.  startActivityForResult(intent, 1);                
  12.   }
  13. @Override
  14.   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  15.     if (data != null) {
  16.        Uri uri = data.getData();
  17.  
  18.        if (uri != null) {
  19.            Cursor c = null;
  20.            try {
  21.                c = getContentResolver().query(uri, new String[]
  22.                         {
  23.        ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE },null, null, null);
  24.  
  25.      if (c != null && c.moveToFirst()) {
  26.  
  27.      String number = c.getString(0);
  28.  
  29.      int type = c.getInt(1);
  30.  
  31.     showSelectedNumber(type, number);
  32.  
  33.       }
  34.         } finally {
  35.        if (c != null) {
  36.         c.close();
  37.         }
  38.        }
  39.       }
  40.     }
  41. }
  42. public void showSelectedNumber(int type, String number)
  43. {
  44.  
  45.     Toast.makeText(this, type + " " + number, Toast.LENGTH_LONG).show();      
  46. }
  47. }
  48.        
  49. ContentResolver cr = getContentResolver();
  50.          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
  51.                  null, null, null, null);
  52.          Log.e("phonenu",""+cur.getCount());
  53.          if (cur.getCount() > 0)
  54.          {
  55.  
  56.  
  57.         while (cur.moveToNext())
  58.         {
  59.             String id = cur.getString(
  60.                          cur.getColumnIndex(ContactsContract.Contacts._ID));
  61.         //String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  62.          //Log.e("contacts", name);
  63.         if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
  64.             //Query phone here.  Covered next
  65.             Cursor pCur = cr.query(
  66.                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  67.                     null,
  68.                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
  69.                     new String[]{id}, null);
  70.                     while (pCur.moveToNext()) {
  71.                     // Do something with phones
  72.                      name.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
  73.                     number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
  74.  
  75.                     }
  76.                     pCur.close();
  77.         }}}
clone this paste RAW Paste Data