首页 / 操作系统 / Linux / Android SDK 2.0以上读取手机联系人
Android SDK 2.0以上的版本对联系人相关的API做了很大的调整,因此在使用时与1.6版本的差异也较大。具体读取联系人及号码,以ListView方式显示,代码如下。
public class ReadContacts extends Activity { ListView listView; ListAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setBackgroundColor(Color.BLACK); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); listView = new ListView(this); listView.setBackgroundColor(Color.BLACK); linearLayout.addView(listView, param);
this.setContentView(linearLayout); // 生成动态数组,加入数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); ArrayList<HashMap<String, Object>> listItemRead= newArrayList<HashMap<String, Object>>(); Cursor cursor = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(cursor.moveToNext()) { HashMap<String, Object> map = new HashMap<String, Object>(); String phoneName = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); map.put("ItemTitle", phoneName);// 电话姓名 String contactId = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); String hasPhone = cursor .getString(cursor