从Contacts到EditText android获取号码

时间:2011-05-29 01:32:28

标签: android android-edittext contacts phone-number

我正在制作Android应用,允许我拨打联系人列表功能,选择联系人,然后使用联系人号码返回我的应用程序,但我只能弄清楚如何获取名称。

我曾经按照一个教程进行工作,但课程人员已经折旧,所以我重新开始。

到目前为止,这是我的代码:

public class Contacts extends Activity {

    int PICK_CONTACT;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText number=(EditText)findViewById(R.id.number);
        Button contactButton = (Button) findViewById(R.id.contacts);
        contactButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                  Intent intent = new Intent(Intent.ACTION_PICK);
                  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                  startActivityForResult(intent, PICK_CONTACT);
            }
        });     
        Button dial=(Button)findViewById(R.id.dial);
        dial.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
        String toDial="tel:"+number.getText().toString();
        startActivity(new Intent(Intent.ACTION_DIAL,
        Uri.parse(toDial)));
        }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {

      if (requestCode == PICK_CONTACT)
      {         
          Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
          cursor.moveToNext();
         // String contactId =     cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

          Toast.makeText(this, "did it work?  =  "+name, Toast.LENGTH_LONG).show(); 
          finish(); 
          }
    }}

谢谢!

0 个答案:

没有答案