Android阅读联系人(和详细信息)似乎很慢

时间:2013-09-09 22:06:31

标签: android performance contacts android-contacts android-contentresolver

我使用以下代码阅读每个联系人及其详细信息。

private static final String[] PROJECTION =
{
    Data._ID,
    Data.MIMETYPE,
    Data.DATA1,
    Data.DATA2,
    Data.DATA3,
    Data.DATA4,
    Data.DATA5,
    Data.DATA6,
    Data.DATA7,
    Data.DATA8,
    Data.DATA9,
    Data.DATA10,
    Data.DATA11,
    Data.DATA12,
    Data.DATA13,
    Data.DATA14,
    Data.DATA15
};    


private static final String SELECTION = Data.LOOKUP_KEY + " = ?";
private String[] mSelectionArgs = { "" };
private static final String SORT_ORDER = Data.MIMETYPE;
private static final int MIME_TYPE_INDEX = 1;
private static final int DISPLAY_NAME_INDEX = 3;//data2
private static final int GIVEN_NAME_INDEX = 3;//data2
private static final int FAMILY_NAME_INDEX = 4;//data3
private static final int MIDDLE_NAME_INDEX = 6;//data5
private static final int ORGANIZATION_INDEX = 2;//data2
private static final int PHONE_TYPE_INDEX = 3;//data2
private static final int PHONE_LABEL_INDEX = 4;//data3
private static final int PHONE_NUMBER_INDEX = 2;//data1
private static final int EMAIL_TYPE_INDEX = 3;//data2
private static final int EMAIL_LABEL_INDEX = 4;//data1
private static final int EMAIL_INDEX = 2;//data1

 private byte[] createJsonData(ArrayList<String> selected) throws JSONException, IOException{

    Log.d("SynchContactActivity", "Time 1: " + java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
    int current = 0;
    final String messagePrep = getResources().getString(R.string.progress_message_prep);
    final String messageCompress = getResources().getString(R.string.progress_message_compress);
    final String messageUpload = getResources().getString(R.string.progress_message_upload);

    if(selected == null ){
        selected = getContacts();
    }

    final int count = selected.size();

    mHandler.post(new Runnable() {

        @Override
        public void run() {
            if(mProgressDialog != null){
                mProgressDialog.setMax(count);
                mProgressDialog.setMessage(messagePrep);
            }
        }
    });

    updateProgress(current);

    JSONObject root = new JSONObject();
    JSONArray contactsArray = new JSONArray();
    JSONObject contactJSON, phoneJSON, emailJSON;
    JSONArray phonesArray,emailsArray;
    String name, lastName, middleName,organization;

    for (String key : selected) {

        contactJSON = new JSONObject();
        phonesArray = new JSONArray();
        emailsArray = new JSONArray();

        mSelectionArgs[0] = key;
        //Cursor details = managedQuery(Data.CONTENT_URI, PROJECTION, SELECTION, mSelectionArgs, SORT_ORDER);
        Cursor details = getApplicationContext().getContentResolver().query(Data.CONTENT_URI, PROJECTION, SELECTION, mSelectionArgs, SORT_ORDER);

        //initialize null variables
        name = null;
        lastName = null;
        middleName = null;
        organization = null;
        while(details.moveToNext()){
            String mimeType = details.getString(MIME_TYPE_INDEX);

            if(mimeType.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)){
                name = details.getString(GIVEN_NAME_INDEX);
                lastName = details.getString(FAMILY_NAME_INDEX);
                middleName = details.getString(MIDDLE_NAME_INDEX);

            }
            else if(mimeType.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                organization = details.getString(ORGANIZATION_INDEX);
            }               
            else if(mimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                phoneJSON = new JSONObject();
                String phoneNumber = details.getString(PHONE_NUMBER_INDEX);
                int type = details.getInt(PHONE_TYPE_INDEX);
                String typeLabel = phoneTypeMap.get(String.valueOf(type));
                if (typeLabel == null) {
                    typeLabel = details.getString(PHONE_LABEL_INDEX);
                }
                phoneJSON.put("ptype", typeLabel);
                phoneJSON.put("number", phoneNumber);
                phonesArray.put(phoneJSON);
            }
            else if(mimeType.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)){
                emailJSON = new JSONObject();
                String email = details.getString(EMAIL_INDEX);
                int type = details.getInt(EMAIL_TYPE_INDEX);
                String typeLabel = emailTypeMap.get(String.valueOf(type));
                if (typeLabel == null) {
                    typeLabel = details.getString(EMAIL_LABEL_INDEX);
                }
                emailJSON.put("etype", typeLabel);
                emailJSON.put("address",email);
                emailsArray.put(emailJSON);
            }
        }

        contactJSON.put("firstname", name==null?"null":name);
        contactJSON.put("middlename", middleName==null?"null":middleName);
        contactJSON.put("lastname", lastName==null?"null":lastName);
        contactJSON.put("organization", organization==null?"null":organization);
        contactJSON.put("phones", phonesArray);
        contactJSON.put("emails", emailsArray);
        contactsArray.put(contactJSON);
        details.close();
        ++current;
        updateProgress(current);
    }
    root.put("contacts", contactsArray);
    Log.d("SynchContactActivity", "Time 1: " + java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));

    mHandler.post(new Runnable() {

        @Override
        public void run() {
            if(mProgressDialog != null){
                mProgressDialog.setMessage(messageCompress);
            }
        }
    });
    // to compress
    String json_doc = root.toString();
    byte[] compressed = compress(json_doc);

    mHandler.post(new Runnable() {

        @Override
        public void run() {
            if(mProgressDialog != null){
                mProgressDialog.setMessage(messageUpload);
            }
        }
    });
    return compressed;
}

此代码太慢 - 平均每秒读取3-4个联系人。这是正常的还是可以优化的?

我认为投影可能是一个很好的候选者,但我不确定。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我很难确切地说出您正在尝试做什么,但看起来您正在尝试从联系人提供程序读取数据并使用JSON将其发送到服务器。我建议您查看ContactsContract.RawContacts.Entity表,其中包含您可能正在寻找的所有数据,而不会试图找出您刚刚检索到的DATA行的MIME类型。通过获取DATA行的全部内容,您肯定会放慢您的应用程序。

此外,您应该使用SyncAdapter来完成这项工作。见Transferring Data Using Sync Adapters

答案 1 :(得分:-2)

阅读联系人可在2-5秒内完成。请参阅此处的示例应用

Source code attached