gpt4 book ai didi

android - 从手机获取联系人需要太多时间

转载 作者:行者123 更新时间:2023-11-30 01:25:35 25 4
gpt4 key购买 nike

我正在开发一个即时聊天消息应用程序。我正在从手机获取联系人并将它们显示在 Activity 的 ListView 中。但这需要 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 中。然后我将该数组列表传递给具有 ListView 的 PhoneBookActivity。我使用 BaseAdapter 填充 ListView 。

最佳答案

我已经使用 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) {

}
}

单击单聊/群聊 Activity 中的联系人图标会调用此 Activity 。

 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);
}
});

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

关于android - 从手机获取联系人需要太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467365/

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