Android 尝试以编程方式使用相同的名字创建多个联系人

Android 尝试以编程方式使用相同的名字创建多个联系人,android,Android,我使用以下代码以编程方式创建新联系人。 此代码使用给定的详细信息在联系人列表中创建一个新联系人,但如果联系人的名字相同,并从循环中运行此代码以创建具有相同名字字段的多个联系人,例如“Crashre1234”,则它会多次在单联系人中添加所有字段(根据循环)。我使用当前系统时间和字段来创建差异。 但是没有运气。 我不明白我错过了什么 ArrayList<ContentProviderOperation> op_list = new ArrayList<

我使用以下代码以编程方式创建新联系人。 此代码使用给定的详细信息在联系人列表中创建一个新联系人,但如果联系人的名字相同,并从循环中运行此代码以创建具有相同名字字段的多个联系人,例如“Crashre1234”,则它会多次在单联系人中添加所有字段(根据循环)。我使用当前系统时间和字段来创建差异。 但是没有运气。 我不明白我错过了什么

                ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
                op_list.clear();
                long tti = System.currentTimeMillis();
                ///for(int i=0;i<2;i++)
                {
                    try{

                        int backRefIndex = 0;
                        System.out.println("Array List: "+backRefIndex);
                        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                        .withValue(RawContacts.ACCOUNT_TYPE, null)
                        .withValue(RawContacts.ACCOUNT_NAME, null)
                        .build());
                        //backRefIndex = backRefIndex+1;

                        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.GIVEN_NAME, "Crashre1234"+"\r")
                        .build());
                        //backRefIndex = backRefIndex+1;

                        /*op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.FAMILY_NAME, "ABCre12")
                        .build());*/
                        //backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
                        .withValue(StructuredPostal.FORMATTED_ADDRESS, "Secret password"+tti)
                        .build());
                        // backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, 11+tti)
                        .withValue(Phone.TYPE, Phone.TYPE_HOME)
                        .withValue(Phone.LABEL, "")
                        .build());
                        // backRefIndex = backRefIndex+1;

                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                         .withValue(Email.DATA, "Test@11test.com12"+tti)
                         .withValue(Email.TYPE, Email.TYPE_HOME)
                         .build());
                        // backRefIndex = backRefIndex+1;
                         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
                         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                         .withValue(Email.DATA, "mobile@test12.com12"+tti)
                         .withValue(Email.TYPE, Email.TYPE_HOME)
                         .build());
                        // backRefIndex = backRefIndex+1;

                         ContentProviderResult[] result = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
                         Uri uri = result[0].uri;
                         System.out.println("URI: "+uri);
                         System.out.println("Thread finish");
                          }catch(OperationApplicationException exp){
                           exp.printStackTrace();
                          }catch(RemoteException exp){
                           exp.printStackTrace();
                        }
                    }   
ArrayList op_list=new ArrayList();
op_list.clear();
long tti=System.currentTimeMillis();

///for(int i=0;i只需将oplist声明和初始化移到for块中即可

long tti = System.currentTimeMillis();
for(int i=0;i<2;i++)
{
    ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
    op_list.clear();
    try{

        int backRefIndex = 0;
        System.out.println("Array List: "+backRefIndex);
        op_list.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
        .withValue(RawContacts.ACCOUNT_TYPE, null)
        .withValue(RawContacts.ACCOUNT_NAME, null)
        .build());
        //backRefIndex = backRefIndex+1;

        op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.GIVEN_NAME, "Crashre1234"+"\r")
        .build());
        //backRefIndex = backRefIndex+1;

        /*op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.FAMILY_NAME, "ABCre12")
        .build());*/
        //backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
        .withValue(StructuredPostal.FORMATTED_ADDRESS, "Secret password"+tti)
        .build());
        // backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
        .withValue(Phone.NUMBER, 11+tti)
        .withValue(Phone.TYPE, Phone.TYPE_HOME)
        .withValue(Phone.LABEL, "")
        .build());
        // backRefIndex = backRefIndex+1;

         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
         .withValue(Email.DATA, "Test@11test.com12"+tti)
         .withValue(Email.TYPE, Email.TYPE_HOME)
         .build());
        // backRefIndex = backRefIndex+1;
         op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
         .withValue(Email.DATA, "mobile@test12.com12"+tti)
         .withValue(Email.TYPE, Email.TYPE_HOME)
         .build());
        // backRefIndex = backRefIndex+1;

         ContentProviderResult[] result = activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
         Uri uri = result[0].uri;
         System.out.println("URI: "+uri);
         System.out.println("Thread finish");
          }catch(OperationApplicationException exp){
           exp.printStackTrace();
          }catch(RemoteException exp){
           exp.printStackTrace();
    }
}   
long tti=System.currentTimeMillis();

对于(int i=0;我用正确的格式编辑了您的代码。至少在此处复制并粘贴它,以便我们可以格式化。为什么要发布两次此内容?当我编辑此文章并添加正确格式的代码,然后单击“保存编辑”按钮时,它会发送未找到页面错误。