Android 在自己的Activity中擷取系統中連絡人的姓名,電話等資訊 .

來源:互聯網
上載者:User

 
  1. public class contractTest extends Activity {  
  2.     private static final String TAG = "U0fly contractTest ===> ";  
  3.       
  4.     private static final int PICK_CONTACT = 3;  
  5.       
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.           
  11.         Log.d(TAG, "onCreate");  
  12.           
  13.         TextView tv = (TextView)findViewById(R.id.TextView);  
  14.           
  15.         Button btn = (Button)findViewById(R.id.btn);  
  16.         btn.setOnClickListener(new View.OnClickListener() {  
  17.               
  18.             @Override  
  19.             public void onClick(View v) {  
  20.                 // TODO Auto-generated method stub
      
  21.                 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
  22.                  startActivityForResult(intent, PICK_CONTACT);  
  23.             }  
  24.         });  
  25.           
  26.           
  27.           
  28.     }  
  29.       
  30.     @Override  
  31.     public void onActivityResult(int reqCode, int resultCode, Intent data){  
  32.         super.onActivityResult(reqCode, resultCode, data);  
  33.         Log.d(TAG, "onActivityResult");  
  34.         switch(reqCode){  
  35.            case (PICK_CONTACT):  
  36.                Log.d(TAG, "onActivityResult PICK_CONTACT");  
  37.              if (resultCode == Activity.RESULT_OK){  
  38.                                  Uri contactData = data.getData();  
  39.                                  ContentResolver  c = getContentResolver();  
  40.                  Cursor cursor= c.query(contactData, null, null, null, null);  
  41.                                  if (cursor.moveToFirst()){  
  42.                                            
  43.                       String[] PHONES_PROJECTION = new String[] { "_id","display_name","data1","data3"};  
  44.                       String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));  
  45.                         
  46.                     Cursor phone = c.query(  
  47.                             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
  48.                             PHONES_PROJECTION,  
  49.                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
  50.                                     + "=" + contactId, null, null);  
  51.                     // name type ..
      
  52.                     while (phone.moveToNext()) {  
  53.                         int i = phone.getInt(0);  
  54.                           
  55.                         String str = phone.getString(1);  
  56.                                                 str += " ";  
  57.                         str += phone.getString(2);  
  58.                                                 str += " ";  
  59.                         str += phone.getString(3);  
  60.                                             }  
  61.                         
  62.                      // other data is available for the Contact.  I have decided
      
  63.                      //    to only get the name of the Contact.
      
  64.                      String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
  65.                      Log.d(TAG, "onActivityResult PICK_CONTACT RESULT_OK 444");  
  66.                  //    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
      
  67.                      TextView tv = (TextView)findViewById(R.id.TextView);  
  68.                      tv.setText(name);  
  69.                  }  
  70.              }  
  71.         }  
  72.     }  
  73. }  

 

添加許可權:

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.