如何在Android中获取最近和最喜欢的联系人?

时间:2017-05-01 06:31:37

标签: android android-contacts android-cursor

我一直在尝试获取最近和收藏的联系人,但每次我都会收到错误。 我在获取后将联系人存储在数据库中。

  

无法读取-1列   有时它表示光标没有正确初始化。

请帮帮我。

这是我的代码。

 ContentResolver cr = getActivity().getContentResolver();
  /*  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null );*/


  Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,CallLog.Calls.DATE, null,null);
    String phone = null;
    String emailContact = null;
    String image_uri;
    Bitmap bitmap;

    final SQLiteDatabase mDb = db.getWritableDatabase();
    mDb.beginTransaction();

    if (cur.getCount() > 0)
    {
        while (cur.moveToNext())
        {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            image_uri = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                System.out.println("name : " + name + ", ID : " + id);

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[]{id}, null);
                Log.e("pCur","dfgfdg  "+pCur.getCount());
                while (pCur.moveToNext())
                {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // contactid=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                   /* phonenumber.add(pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));`*/

                }
                pCur.close();


                Cursor emailCur = cr.query
                        (
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                        + " = ?", new String[]{id}, null);

                while (emailCur.moveToNext())
                {
                    emailContact = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase(""))
                    {
                        emailContact="";

                        Log.e("isEmpty","isEmpty " + emailContact);
                    }

                    else
                    {
                        Log.e("gfdszfg","Email " + emailContact);
                    }
                  /*  emailType = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/

                    Log.e("gfdszfg","Email " + emailContact);
                }
                emailCur.close();
            }

            if (image_uri != null)
            {
                System.out.println(Uri.parse(image_uri));
                try
                {
                    bitmap = MediaStore.Images.Media
                            .getBitmap(getActivity().getContentResolver(),
                                    Uri.parse(image_uri));
                    System.out.println(bitmap);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            mList.add(new Contacts(name, phone, image_uri,emailContact));

            ContentValues contentValues = new ContentValues();
            contentValues.put("contact_name", name);
            contentValues.put("contact_number",phone);
            contentValues.put("contact_email",emailContact);
            contentValues.put("contact_image",image_uri);
            mDb.insert(TABLE_CONTACT, null, contentValues);

            emailContact="";
            phone="";
        }
        mDb.setTransactionSuccessful();

        mDb.endTransaction();
        cur.close();
    }

2 个答案:

答案 0 :(得分:0)

您正在查询CallLog表:

  

Cursor cur = cr.query( CallLog.Calls.CONTENT_URI ,null,CallLog.Calls.DATE,null,null)

然后尝试使用Contacts表中的字段从该游标获取信息:

  

cur.getString(cur.getColumnIndex(的 ContactsContract.Contacts._ID ));

显然,这不是它应该如何运作。

另外,您的选择:

  

CallLog.Calls.DATE

不是合法的选择字符串。

你应该怎么做:

获取已加星标(收藏)的联系人列表:

String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts.STARRED + "=1", null, null);

要获取过去24小时内的联系人列表:

String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
int oneDay = (1000 * 60 * 60 * 24);
long last24h = (System.currentTimeMillis() - oneDay);
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts.    LAST_TIME_CONTACTED + ">" + last24h, null, null);

答案 1 :(得分:0)

我知道到目前为止你可能已经解决了那个错误,但我仍然想回答寻求者的进一步帮助。用于获取收藏夹联系人的代码正在运行,您只需要在投影中添加以下内容:

String[] projection = new String[] { ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};