import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPhoneContacts();
Log.e(“YQY”, mContacts.size() + “-------------mContacts---------”);
initList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** 联系人名称 **/
private ArrayList mContacts = new ArrayList();
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID };
/** 联系人显示名称 **/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/** 电话号码 **/
private static final int PHONES_NUMBER_INDEX = 1;
/** 头像ID **/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/** 联系人的ID **/
private static final int PHONES_CONTACT_ID_INDEX = 3;
private void getPhoneContacts() {
ContentResolver resolver = getContentResolver();
try {
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手机号码
String phoneNumber = phoneCursor
.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
// 得到联系人ID
Long contactid = phoneCursor
.getLong(PHONES_CONTACT_ID_INDEX);
// 得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
// 得到联系人头像Bitamp
Bitmap contactPhoto = null;
// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contactid);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
} else {
contactPhoto = BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher);
}
ContactEntity mContact = new ContactEntity(contactName,
phoneNumber, contactPhoto);
mContacts.add(mContact);
}
phoneCursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void initList() {
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
if (mContacts != null && mContacts.size() > 0) {
return mContacts.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
if (mContacts != null && mContacts.size() > 0) {
return mContacts.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.item_contact, null);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.number = (TextView) convertView
.findViewById(R.id.tv_number);
holder.photo = (ImageView) convertView
.findViewById(R.id.iv_photo);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
ContactEntity contact = mContacts.get(position);
holder.name.setText(contact.getName()+“”);
holder.number.setText(contact.getNumber()+“”);
最后送福利了,现在关注我可以获取包含源码解析,自定义View,动画实现,架构分享等。
内容难度适中,篇幅精炼,每天只需花上十几分钟阅读即可。
大家可以跟我一起探讨,有flutter—底层开发—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿
= mContacts.get(position);
holder.name.setText(contact.getName()+“”);
holder.number.setText(contact.getNumber()+“”);
最后送福利了,现在关注我可以获取包含源码解析,自定义View,动画实现,架构分享等。
内容难度适中,篇幅精炼,每天只需花上十几分钟阅读即可。
大家可以跟我一起探讨,有flutter—底层开发—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿
[外链图片转存中…(img-8Jtx6mbr-1726006482769)]