如何访问电话簿联系人?

时间:2014-01-20 08:46:48

标签: android android-contacts android-cursor

我正在使用Edittext和一个按钮。按下按钮,电话簿打开,然后用户将从中选择一个联系人,所选的电话号码将显示在编辑文本上。 我遵循了许多教程,但他们展示的方法已经被折旧了。

我已声明此权限:清单中的READ_CONTACTS

2 个答案:

答案 0 :(得分:2)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_picker);

        // this opens the activity. note the  Intent.ACTION_GET_CONTENT
        // and the intent.setType
        ((Button)findViewById(R.id.pick_person)).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);                
            }
        });
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String number = c.getString(0);
                    int type = c.getInt(1);
                    showSelectedNumber(type, number);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      
}

答案 1 :(得分:0)

Digvesh Patel提供的答案是正确的。他使用了“类型”的联系,它返回了数字。所以我使用了他的代码并进行了一些我在我的应用程序中使用的更改。它可能对某人有帮助

public int REQUESTCODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button select = (Button) findViewById(R.id.select);
    select.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, REQUESTCODE);

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();
        Log.i("data", uri.toString());
        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String name = c.getString(0);
                    String number = c.getString(1);
                    int type = c.getInt(2);

                    showSelectedNumber(name, number,type);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(String name,  String number, int type){
    TextView usernumber = (TextView) findViewById(R.id.textView1);
    String typelabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), type, "");
    usernumber.setText(name+": "+number+" "+typelabel);

}