【问题标题】:how to access phone book contacts?如何访问电话簿联系人?
【发布时间】:2014-02-09 08:38:53
【问题描述】:

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

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

【问题讨论】:

    标签: android android-contacts android-cursor


    【解决方案1】:
        @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();      
    }
    

    【讨论】:

    • 除了 READ_CONTACTS 之外,我是否必须声明任何其他权限
    • 完成 .. 但它没有在编辑文本上显示选定的联系人,只是吐司
    【解决方案2】:

    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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多