安卓:联系人选择器意向|不能开放的实例化的类型 [英] Android: Contact Picker Intent | Cannot instantiate the type Uri

查看:103
本文介绍了安卓:联系人选择器意向|不能开放的实例化的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接的联系与电话号码only.And我下面这code

 静态最终诠释PICK_CONTACT_REQUEST = 1; //请求code
...
私人无效pickContact(){
    意图pickContactIntent =新的意图(Intent.ACTION_PICK,新的URI(内容://联系人));
    pickContactIntent.setType(Phone.CONTENT_TYPE); //显示用户只接触W /电话号码
    startActivityForResult(pickContactIntent,PICK_CONTACT_REQUEST);
}
 

但不幸的是,它显示错误:无法实例的类型乌里

其实我有另一个工作code这是工作完美,但崩溃在选择电子邮件联系人。我只需要电话号码。

 意图int​​entContact =新的意图(Intent.ACTION_PICK,
                                ContactsContract.Contacts.CONTENT_URI);
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivityForResult(intentContact,PICK_CONTACT);
 

的onReceive(),这种方法被称为

 公共无效getContactInfo(意向意图){

    ContentResolver的CR = getContentResolver();
    光标= cr.query(intent.getData(),NULL,NULL,NULL,NULL);

    而(cursor.moveToNext()){
        字符串的ContactID = cursor.getString(光标
                .getColumnIndex(ContactsContract.Contacts._ID));
        如果(整数
                .parseInt(cursor.getString(光标
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0){
            光标手机= getContentResolver()查询(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    空值,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            +=+的ContactID,NULL,NULL);
            而(phones.moveToNext()){
                phoneNumber的=手机
                        .getString(电话
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phones.close();
        } 其他 {
            snipp.showAlertDialog(getApplicationContext(),否号码,
                    无法读取号,假);
        }

    }
    cursor.close();
}
 

解决方案

这对我的作品:

 私人无效pickContact(){
    意图pickContactIntent =新的意图(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(pickContactIntent,PICK_CONTACT_REQUEST);
}
 

编辑:

onActivityResult()应该是这样的:

  @覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图意图){

    super.onActivityResult(要求code,因此code,意图);
    如果(要求code == PICK_CONTACT_REQUEST){

        如果(结果code == RESULT_OK){
                乌里pickedPhoneNumber = intent.getData();
                //处理在这里拿起电话号码。
            }
        }
    }
}
 

I am trying to pick contacts with phone number only.And I am following this code

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

But unfortunately, its showing an error: Cannot instantiate the type Uri

Actually I have another working code which is working perfectly, but crashes on selecting Email contacts. I need only phone numbers.

Intent intentContact = new Intent(Intent.ACTION_PICK,
                                ContactsContract.Contacts.CONTENT_URI);
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivityForResult(intentContact, PICK_CONTACT);

and at onReceive(), this method is called

public void getContactInfo(Intent intent) {

    ContentResolver cr = getContentResolver();
    cursor = cr.query(intent.getData(), null, null, null, null);

    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        if (Integer
                .parseInt(cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phones.close();
        } else {
            snipp.showAlertDialog(getApplicationContext(), "No Number",
                    "Cannot read number", false);
        }

    }
    cursor.close();
}

解决方案

This works for me:

private void pickContact() {
    Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

Edit:

Your onActivityResult() should look like this:

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

    super.onActivityResult( requestCode, resultCode, intent );
    if ( requestCode == PICK_CONTACT_REQUEST ) {

        if ( resultCode == RESULT_OK ) {
                Uri pickedPhoneNumber = intent.getData();
                // handle the picked phone number in here.
            }
        }
    }
}

这篇关于安卓:联系人选择器意向|不能开放的实例化的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆