在android中获取联系人非常慢

前端之家收集整理的这篇文章主要介绍了在android中获取联系人非常慢前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我编写了一个代码来从联系人中获取联系人姓名,电话号码和图像,并将其显示android的listview中.它工作正常,但需要更多时间来加载.我曾尝试在代码的某些部分使用多线程.但加载时间并未减少.

这是onCreate()方法

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 lvDetail = (ListView) findViewById(R.id.listView1);

 fetchcontacts();

 lvDetail.setAdapter(new MyBaseAdapter(context,myList));
 }

以下是获取联系人的代码

private void fetchcontacts() {

        // TODO Auto-generated method stub
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,ContactsContract.Contacts.DISPLAY_NAME + " ASC");
                int count = cursor.getCount();
                if (count > 0) {
                     Toast.makeText(context,"count >0",Toast.LENGTH_SHORT).show();
                    while (cursor.moveToNext()) {
                        String columnId = ContactsContract.Contacts._ID;
                        int cursorIndex = cursor.getColumnIndex(columnId);
                        String id = cursor.getString(cursorIndex);

                      name = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                      Toast.makeText(context,"Toast 1",Toast.LENGTH_SHORT).show();




                        int numCount = Integer.parseInt(cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                        if (numCount > 0) {
                             Toast.makeText(context,"Toast 2",Toast.LENGTH_SHORT).show();
                            Cursor phoneCursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,CommonDataKinds.Phone.CONTACT_ID+" = ?",new String[] { id
                },ContactsContract.Contacts.DISPLAY_NAME + " ASC");

                                while (phoneCursor.moveToNext()) {
                                     Toast.makeText(context,"Toast 3",Toast.LENGTH_SHORT).show();
                              phoneNo = phoneCursor.getString(phoneCursor
                .getColumnIndex(ContactsContract.CommonDataKinds.
                Phone.NUMBER));


                                String image_uri = phoneCursor
                                         .getString(phoneCursor
                                         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

                             if (image_uri != null) {
                                 Toast.makeText(context,"Toast 4",Toast.LENGTH_SHORT).show();
                                 System.out.println(Uri.parse(image_uri));
                                 try {
                              bitmap = MediaStore.Images.Media
                                 .getBitmap(this.getContentResolver(),Uri.parse(image_uri));
                                // sb.append("\n Image in Bitmap:" + bitmap);
                                // System.out.println(bitmap);

                                 } catch (FileNotFoundException e) {
                                 // TODO Auto-generated catch block
                                 e.printStackTrace();
                                 } catch (IOException e) {
                                 // TODO Auto-generated catch block
                                 e.printStackTrace();
                                 }

                                 }
                             Toast.makeText(context,name,Toast.LENGTH_SHORT).show();

                                    getDataInList(name,phoneNo,bitmap);
                                 name=null;
                                 phoneNo=null;
                                 Drawable myDrawable = getResources().getDrawable(R.drawable.star1);
                                 bitmap = ((BitmapDrawable) myDrawable).getBitmap();



                                    }
                                    phoneCursor.close();
                                }

                            }


                        }

这里,listview的setAdapter()函数在将所有联系人提取到ArrayList之后正在工作.有没有人知道如何在获取联系人期间显示联系人?任何示例代码

解决方法

1.从光标中读取您只需要的列,根据您的要求,您只需要_ID,HAS_PHONE_NUMBER,DISPLAY_NAME,所以更改光标读数
Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.HAS_PHONE_NUMBER,ContactsContract.Contacts.DISPLAY_NAME },ContactsContract.Contacts.DISPLAY_NAME + " ASC");

2.不要在UI线程中执行时间过程.请使用AsyncTask

注意:这两个步骤将在某种程度上解决.但不完全

猜你在找的Android相关文章