如何通过编程获取联系人的最大联系人ID?

How could I get the maximum the contact id in contact by programming?

我想在我的 SQLite 中插入一个带有新联系人 ID 的新 phone 号码。我需要知道联系人中的最大联系人 ID,以忽略保存相同的联系人 ID。是否可以知道 Android 中的最大联系人 ID?谢谢

从联系人 Table 中,您可以通过如下所示查询获取最后一个联系人 ID

    final String sortOrder = ContactsContract.Contacts._ID+" DESC LIMIT 1";
            Cursor lcursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,new String[] {ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME},null,null,sortOrder);
            if (lcursor != null &&  lcursor.moveToFirst()) {
                long id = lcursor.getLong(lcursor.getColumnIndex(ContactsContract.Contacts._ID));
                String displayName = lcursor.getString(lcursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            }

光标将 return 只有一个条目,该条目将是联系人 table

中的最后一个 contact_id

注意:这不应该在主线程上完成,因为如果设备联系人超过 1000 个联系人,这可能会花费太多时间,因此请务必小心查询数据库有大量条目

享受编码吧! :)