使用 Cursor android 显示 gmail 中的联系人和 phone 的联系人

displaying contacts in gmail and phone's contact using Cursor android

我试图在我的应用程序中显示来自 phone 的联系人,但它同时显示来自 gmail 和 phone 的联系人。但是我的模拟器只显示 phone 联系人而不是 gmail 联系人。

如何避免gmail联系人不被选中。我只需要来自 phone.

的联系人

来自 phone 的联系人是:

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

我的代码是,

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 的联系人。这可能吗

必须尝试这个 代码:

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/