从手机获取联系人需要花费太多时间

时间:2016-04-07 05:25:22

标签: android android-contacts

我正在开发一个即时聊天消息应用程序。我从手机接收联系人并在活动的列表视图中显示它们。但它需要25-30秒。我使用以下代码:

1。点击“联系人”按钮:

   contacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Contacts Clicked", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                ArrayList<Bean_PhoneNumbers> listBeanPhone = getContactsFromPhone();
                Intent intent = new Intent(SingleChatActivity.this, PhoneBookActivity.class);
                //intent.putExtra("listBeanPhone", listBeanPhone);
                intent.putParcelableArrayListExtra("listBeanPhone", listBeanPhone);
                intent.putExtra("friendName", friendName);
                intent.putExtra("absoluteURL", completeURLFriend);
                intent.putExtra("friendID", frndID);
                startActivity(intent);
            }
        });

2。 getContactFromPhone()方法

 private ArrayList<Bean_PhoneNumbers> getContactsFromPhone() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            bean_phoneNumbers = new Bean_PhoneNumbers();
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(SingleChatActivity.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    bean_phoneNumbers.setName(name);
                    bean_phoneNumbers.setPhoneNumber(phoneNo);
                    listBeanPhoneNumbers.add(bean_phoneNumbers);
                }
                pCur.close();
            }

        }
    }
    return listBeanPhoneNumbers;
}

3.PhoneBookActivity.java

    public class PhoneBookActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private ViewPager viewPager;
    private SearchView searchView;
    private ListView listView;
    private Adapter_PhoneBook adapter_phoneBook;
    private String friendName, completeURLFriend, frndID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phonebook);

        //findViewByID
        searchView = (SearchView) findViewById(R.id.search);
        listView = (ListView) findViewById(R.id.list);

        // Fetching data from a parcelable object passed from SingleChatActivity
        ArrayList<Bean_PhoneNumbers> listBeanPhone = getIntent().getParcelableArrayListExtra("listBeanPhone");
        for (int i = 0; i < listBeanPhone.size(); i++) {
            Bean_PhoneNumbers bean = listBeanPhone.get(i);
            String name = bean.getName();
            Log.e("Name", name);
        }
        //Initialization
        adapter_phoneBook = new Adapter_PhoneBook(PhoneBookActivity.this, listBeanPhone);
        listView.setAdapter(adapter_phoneBook);
        searchView.setOnQueryTextListener(this);

        //Getting values from previos screen(ChatFragment)
        Bundle bundle = getIntent().getExtras();
        friendName = bundle.getString("friendsName", null);
        completeURLFriend = bundle.getString("absoluteURL", null);
        frndID = bundle.getString("friendID");


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bean_PhoneNumbers bean_phoneNumbers = (Bean_PhoneNumbers) adapterView.getItemAtPosition(i);
                Intent intent = new Intent(PhoneBookActivity.this, SingleChatActivity.class);
                intent.putExtra("bean", bean_phoneNumbers);
                intent.putExtra("friendName", friendName);
                intent.putExtra("absoluteURL", completeURLFriend);
                intent.putExtra("friendID", frndID);
                startActivity(intent);
            }
        });


    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        adapter_phoneBook.getFilter().filter(s);
        return false;
    }
}

请查看我的代码并告诉我我在哪里做错了。我从电话获取联系人并存储在ArrayList中。然后我将该arraylist传递给具有listview的PhoneBookActivity。我使用BaseAdapter填充listview。

1 个答案:

答案 0 :(得分:1)

我已经使用LoaderManager解决了这个问题。以下是我的PhoneBookActivity.java的代码

public class PhoneBookActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> {
    private SearchView searchView;
    private ListView listView;
    private Adapter_PhoneBook adapter_phoneBook;
    private String friendName, completeURLFriend, frndID, roomID, ownerID, group_members, group_name;
    private String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    private String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
    private Bean_PhoneNumbers bean_phoneNumbers;
    private List<Bean_PhoneNumbers> listBeanPhoneNumbers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phonebook);

        //findViewByID
        searchView = (SearchView) findViewById(R.id.search);
        listView = (ListView) findViewById(R.id.list);
        searchView.setOnQueryTextListener(this);
        //Getting values from previos screen(ChatFragment)
        Bundle bundle = getIntent().getExtras();
        friendName = bundle.getString("friendsName", null);
        // Log.e("FriendName",friendName);
        completeURLFriend = bundle.getString("absoluteURL", null);
        //Log.e("CompleteURLFriend",completeURLFriend);
        frndID = bundle.getString("friendID");
        //Log.e("FriendID",frndID);
        roomID = bundle.getString("GroupRoomID", null);
        ownerID = bundle.getString("owner_id", null);
        group_members = bundle.getString("group_members", null);
        group_name = bundle.getString("group_name", null);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
                Bean_PhoneNumbers bean_phoneNumbers = (Bean_PhoneNumbers) adapterView.getItemAtPosition(i);

                //On clicking list item ,we can move to either SingleChatActivity or GroupChatActivity
                //If PhoneBookActivity is started from SingleChatActivity
                if (friendName != null && completeURLFriend != null && frndID != null) {
                    Intent singleChatIntent = new Intent(PhoneBookActivity.this, SingleChatActivity.class);
                    singleChatIntent.putExtra("bean", bean_phoneNumbers);
                    singleChatIntent.putExtra("friendsName", friendName);
                    singleChatIntent.putExtra("absoluteURL", completeURLFriend);
                    singleChatIntent.putExtra("friendID", frndID);
                    startActivity(singleChatIntent);
                }

                //If PhoneBookActivity is started from GroupChatActivity
                if (roomID != null && ownerID != null && group_members != null && group_name != null) {
                    Intent groupChatIntent = new Intent(PhoneBookActivity.this, GroupChatActivity.class);
                    groupChatIntent.putExtra("bean", bean_phoneNumbers);
                    groupChatIntent.putExtra("GroupRoomID", roomID);
                    groupChatIntent.putExtra("group_members", group_members);
                    groupChatIntent.putExtra("group_name", group_name);
                    groupChatIntent.putExtra("owner_id", ownerID);


                    startActivity(groupChatIntent);
                }
            }
        });


        getSupportLoaderManager().initLoader(1, null, this);

    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        adapter_phoneBook.getFilter().filter(s);
        return false;
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        return new CursorLoader(this, CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        listBeanPhoneNumbers = new ArrayList<>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            bean_phoneNumbers = new Bean_PhoneNumbers();
            bean_phoneNumbers.setName(cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)));
            bean_phoneNumbers.setPhoneNumber(cursor.getString(cursor.getColumnIndex(NUMBER)));
            listBeanPhoneNumbers.add(bean_phoneNumbers);
            cursor.moveToNext();
        }
        adapter_phoneBook = new Adapter_PhoneBook(getApplicationContext(), listBeanPhoneNumbers);
        listView.setAdapter(adapter_phoneBook);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

单击“单聊天/群聊”活动中的“联系人”图标时,将调用此活动。

 contacts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.show();
            Toast.makeText(getApplicationContext(), "Contacts Clicked", Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();
           // ArrayList<Bean_PhoneNumbers> listBeanPhone = getContactsFromPhone();
            Intent intent = new Intent(GroupChatActivity.this, PhoneBookActivity.class);
            //intent.putExtra("listBeanPhone", listBeanPhone);
           // intent.putParcelableArrayListExtra("listBeanPhone", listBeanPhone);
            intent.putExtra("GroupRoomID", roomID);
            intent.putExtra("group_members", group_members);
            intent.putExtra("group_name", group_name);
            intent.putExtra("owner_id", ownerID);
            Log.e("rID", roomID);
            startActivity(intent);
        }
    });

这段代码现在对我有用。希望它对其他人也有帮助。快乐编码: - )