如何在Android中获取最近和最喜欢的联系人? [英] How to fetch recent and favourite contacts in android?

查看:238
本文介绍了如何在Android中获取最近和最喜欢的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取最近和最喜欢的联系人,但是每次遇到错误时,我都会尝试. 提取后,我将联系人存储在数据库中.

I have been trying to fetch recent and favorites contacts but every time i get error . i am storing contacts in database after fetching .

无法读取第-1列 有时它说光标没有正确初始化.

cannot read column -1 and sometimes it says cursor not initialized properly.

请帮助我.

这是我的代码.

 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();
    }

推荐答案

您正在查询CallLog表:

You're querying the CallLog table:

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

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

,然后尝试使用联系人"表中的字段从该光标获取信息:

and then trying to get info from that cursor with fields from the Contacts table:

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

显然,这不是它的工作方式.

Obviously, that's not how it should work.

此外,您的选择:

CallLog.Calls.DATE

CallLog.Calls.DATE

不是合法的选择字符串.

is not a legal selection string.

您应该如何做:

要获取已加星标(最爱)的联系人的列表,请执行以下操作:

To get a list of starred (favourites) contacts:

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小时内的联系人列表:

To get a list of contacts with in the last 24 hours:

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);

这篇关于如何在Android中获取最近和最喜欢的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆