在android联系人中导入重复项

时间:2012-03-20 10:24:55

标签: android android-contacts android-contentprovider

我在android中导入.vcf文件,但它导入了重复记录。

代码: 导入联系方式:

 public void doImport(final String fileName, final boolean replace) {
    try {

            File vcfFile = new File(fileName);

            final BufferedReader vcfBuffer = new BufferedReader(new FileReader(fileName));



            final long maxlen = vcfFile.length();

    // Start lengthy operation in a background thread
            new Thread(new Runnable() {
         public void run() {
            long importStatus = 0;

            synchronized (syncMonitor) {
                    mAction = Action.IMPORT;
                    syncFileName = fileName;
                    }

            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            SQLiteStatement querySyncId = db.compileStatement("SELECT " + SYNCID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + PERSONID + "=?");
            SQLiteStatement queryPersonId = db.compileStatement("SELECT " + PERSONID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + SYNCID + "=?");
            SQLiteStatement insertSyncId = db.compileStatement("INSERT INTO  " + SYNCDATA_TABLE_NAME + " (" + PERSONID + "," + SYNCID + ") VALUES (?,?)");
            Contact parseContact = new Contact(querySyncId, queryPersonId, insertSyncId);
                    try {
                            long ret = 0;
                            do  {
                                    ret = parseContact.parseVCard(vcfBuffer);
                                    if (ret >= 0) {
                                            parseContact.addContact(getApplicationContext(), 0, replace);
                                            importStatus += parseContact.getParseLen();

                                            // Update the progress bar
                      //   app.updateProgress((int) (100 * importStatus / maxlen));
                                    }
                            } while (ret > 0);

                            db.close();
                        //    app.updateProgress(100);
                    synchronized (syncMonitor) {
                            mAction = Action.IDLE;
                          //  showNotification();
                    }
                 //   stopSelf();
                    } catch (IOException e) {
                    }
         }
     }).start();


    } catch (FileNotFoundException e) {
         //   app.updateStatus("File not found: " + e.getMessage());
    }
    Toast.makeText(mContext, "Import", Toast.LENGTH_LONG).show();

    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + SYNCDATA_TABLE_NAME + " ("
                    + PERSONID + " INTEGER PRIMARY KEY,"
                    + SYNCID + " TEXT UNIQUE"
                    +");");
        }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                        // No need to do anything --- this is version 1

                }
    }

Contact.java: Click here Contact.java

代码链接:Click here to get whole code of contact import and export

如何限制导入上述代码中的重复项

感谢Adv。

1 个答案:

答案 0 :(得分:0)

您应该检查VCard中的字段“X-IRMC-LUID”是否与地址簿中保存的字段不同。如果它相同则VCard条目是重复的。

没有关于如何生成此类字段的规范,AFAIK以不同方式完成,主要是复制UID字段或VCard中的位置。这个开源应用程序包含一些可能有用的代码示例:http://code.google.com/p/vcardio/