【问题标题】:displaying contacts in gmail and phone's contact using Cursor android使用 Cursor android 显示 gmail 中的联系人和手机的联系人
【发布时间】:2015-06-28 03:56:30
【问题描述】:

我试图在我的应用中显示来自手机的联系人,但它同时显示了来自 gmail 和手机的联系人。但是我的模拟器只显示电话联系人而不是 gmail 联系人。

如何避免 gmail 联系人不被选中。我只需要手机中的联系人。

电话联系人是:

我的应用中显示的联系人是,

我的代码是,

private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor>{

    @Override
    protected Cursor doInBackground(Void... params) {
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        Cursor contactsCursor = getActivity().getContentResolver().query(contactsUri, null, null, null,
                ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        if(contactsCursor.moveToFirst()){
            do{
                long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

                Uri dataUri = ContactsContract.Data.CONTENT_URI;

                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone, work email etc corresponding to each contact
                Cursor dataCursor = getActivity().getContentResolver().query(dataUri, null,
                                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                                        null, null);

                String displayName="";
                photoPath="" + R.drawable.noimage;
                byte[] photoByte=null;

                if(dataCursor.moveToFirst()){
                    // Getting Display Name
                    displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                    do{
                        // Getting Phone numbers
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                            number = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        // Getting Photo
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                            photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));

                            if(photoByte != null) {
                                Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);

                                // Getting Caching directory
                                File cacheDirectory = getActivity().getBaseContext().getCacheDir();

                                // Temporary file to store the contact image
                                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");

                                // The FileOutputStream to the temporary file
                                try {
                                    FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                                    // Writing the bitmap to the temporary file as png file
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);

                                    // Flush the FileOutputStream
                                    fOutStream.flush();

                                    //Close the FileOutputStream
                                    fOutStream.close();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                photoPath = tmpFile.getPath();
                            }
                        }
                    }while(dataCursor.moveToNext());

                    // Adding id, display name, path to photo and other details to cursor
                    mMatrixCursor.addRow(new Object[]{Long.toString(contactId), displayName, number, photoPath});

                }
            }while(contactsCursor.moveToNext());
        }
        return mMatrixCursor;
    }

如何避免来自 gmail 的联系人。这可能吗?

【问题讨论】:

  • 检查循环中的ContactsContract.SyncColumns#ACCOUNT_TYPE,然后修改主查询的 selection 和 selectionArgs 以过滤掉特定的帐户类型
  • @pskink :- 有什么例子吗?
  • 不,没有例子:您只需要按照我的提示进行操作
  • @pskink :- 好的。谢谢。

标签: android android-contacts contactscontract


【解决方案1】:

必须尝试这个 代码:

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

所需权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

如果你需要一个例子,那么你可以看看这个 http://examples.javacodegeeks.com/android/core/provider/android-contacts-example/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多