如何从onActivityresult返回结果

时间:2014-04-24 10:37:58

标签: android onactivityresult

我正在尝试从默认联系人应用中选择一个联系人。我成功地从联系人那里获得了信息。现在我想将这些数据保存在一个arraylist,联系人类型。如何从onActivityResult?我正在搜索这个5个小时。我是android的新手。

这是我的onActivityResult的代码

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (PICK_CONTACT):
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String id = c
                        .getString(c
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                long cId = (long)Double.parseDouble(id);

                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                int hasPhoneNumber = c
                        .getInt(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhoneNumber > 0) {
                    // this contact
                    Cursor phoneCursor = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + "=?", new String[] { id },
                                    null);
                    while (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor
                                .getString(phoneCursor
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Toast.makeText(getApplicationContext(),
                                id + " " + name + " " + phoneNumber,
                                Toast.LENGTH_LONG).show();
                    }
                    phoneCursor.close();
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

在为

指定值之后的上述内容中

String phoneNumber = phoneCursor                                 .getString(phoneCursor                                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

在INTENT对象中存储相同的值,即

意图数据

像 data.putExtra( “phoneNumber的”,phoneNumber的);

并在启动之后启动您要调用的新活动,如下所示

startActivity(new Intent(Intent.ACTION_VIEW,data));

新活动中的

从意图

获取上述值

Intent intentWithPhoneNumbers = getIntent(); 如果(空!= intentWithPhoneNumbers.getExtras()){         String phoneNumber = intentWithPhoneNumbers.getExtras()。getString(“phoneNumber”); }