Android 调用联系人,返回值并显示在edittext上

package com.example.contactdemo;  
  
import android.app.Activity;  
import android.content.ContentResolver;  
import android.content.Intent;  
import android.database.Cursor;  
import android.net.Uri;  
import android.os.Bundle;  
import android.provider.ContactsContract;  
import android.view.View;  
import android.widget.EditText;  
  
public class MainActivity extends Activity {  
  
    EditText text;  
    String username,usernumber;  //username和usernumber
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        text = (EditText)findViewById(R.id.number);  
    }  
  
      
   public void click(View v){  

  /**
   * 获取返回值的activity调用
   */ startActivityForResult(new Intent( Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI), 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { ContentResolver reContentResolverol = getContentResolver(); Uri contactData = data.getData();
@SuppressWarnings("deprecation")

        //cursor查询联系人数据库 Cursor cursor = managedQuery(contactData, null, null, null, null); cursor.moveToFirst(); username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); while (phone.moveToNext()) {
                //获取联系人的号码
usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                //将获取的联系人号码添加到edittext里
     text.setText(usernumber+" ("+username+")"); } } } }

 

posted @ 2012-12-17 13:54  ikarl  阅读(335)  评论(0编辑  收藏  举报