电子邮件未显示来自Android AddressBok

时间:2016-10-23 06:25:28

标签: java android contacts addressbook contactscontract

我正在努力从设备的地址簿中获取联系人。我收到电子邮件时遇到问题。我试过并且它一直显示电话号码或号码(我猜这是一种类型)。请帮助。

这是我的AdressBook.java

package newgbacard.gbacard.com.gbacard.fragments;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import newgbacard.gbacard.com.gbacard.R;
import newgbacard.gbacard.com.gbacard.activities.ContactDetailActivity;
import newgbacard.gbacard.com.gbacard.activities.SettingsActivity;
import newgbacard.gbacard.com.gbacard.adapters.ContactUserAdapter;
import newgbacard.gbacard.com.gbacard.models.Contact;
import newgbacard.gbacard.com.gbacard.utils.Constants;


public class AddressBookFragment2 extends Fragment {

private ArrayList<Contact> selectUsers;
private ListView listView;

private Cursor phoneCursor;
private Cursor emailCursor;

private MenuItem searchMenuItem;

// Pop up
ContentResolver resolver;
SearchView searchView;
ContactUserAdapter adapter;

String email;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_addressbook, null);

    selectUsers = new ArrayList<Contact>();
    resolver = getActivity().getContentResolver();
    listView = (ListView) view.findViewById(R.id.list);

    try {
        phoneCursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

        LoadContact loadContact = new LoadContact();
        loadContact.execute();

        setHasOptionsMenu(true);
    } catch (Exception e){
        e.printStackTrace();
    }

    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_addressbook_fragment, menu);

    searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setOnQueryTextListener(listener);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();

    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent intent = new Intent(getActivity(), SettingsActivity.class);
        startActivity(intent);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

SearchView.OnQueryTextListener listener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // newText is text entered by user to SearchView
        adapter.filter(newText);
        return false;
    }
};

// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        // Get Contact list from Phone

        if (phoneCursor != null) {
            Log.e("count", "" + phoneCursor.getCount());
            if (phoneCursor.getCount() == 0) {
                Toast.makeText(getActivity(), "No contacts in your contact list.", Toast.LENGTH_LONG).show();
            }

            while (phoneCursor.moveToNext()) {
                Bitmap bitmapThumbnail = null;
                String id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                String displayName = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String email = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                String address = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                String imageThumbnail = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));

                try {
                    if (imageThumbnail != null) {
                        bitmapThumbnail = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(imageThumbnail));
                    } else {
                       // Log.e("No Image Thumb", "--------------");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Contact contact = new Contact();
                contact.setThumb(bitmapThumbnail);
                contact.setFirstName(displayName);
                contact.setLastName("");
                contact.setPhoneNumber(phoneNumber);
                contact.setEmail(email);
                contact.setAddress(address);

                selectUsers.add(contact);
            }
        }
        //phones.close();
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        adapter = new ContactUserAdapter(selectUsers, getActivity());
        listView.setAdapter(adapter);

        // Select item on listclick
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    Contact contact = selectUsers.get(i);
                    Intent intent = new Intent(getActivity(), ContactDetailActivity.class);
                    intent.putExtra(Constants.TAG_CONTACT_DETAIL, contact);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //listView.setFastScrollEnabled(true);
    }
}
}

如果这个问题得到解决,我将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

您正在尝试从您的phoneCursor字段中收到一封电子邮件,您可以这样开头:

phoneCursor = getActivity().getContentResolver().query(Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

Phone.CONTENT_URI上的表格仅包含与电话号码相关的信息,但不包含任何电子邮件。

如果您想要多种类型的数据,则应该在CommonDataKinds.Data.CONTENT_URI上查询哪些应该返回所有类型的联系信息,您可以使用{将其限制为电话和电子邮件{1}}:

selection

docs:https://developer.android.com/reference/android/provider/ContactsContract.Data.html