gpt4 book ai didi

android - 在android中获取联系人非常慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:09 27 4
gpt4 key购买 nike

我编写了一个代码来从联系人中获取联系人姓名、电话号码和图像,并将其显示在 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,
null, 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,null,
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.从您需要的Cursor读取,根据您的要求,您只需要_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 }, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");

2.不要在 UI 线程中执行耗时过程..改用 AsyncTask

注意 : 这两个步骤会在一定程度上解决..但不完全

关于android - 在android中获取联系人非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com