/*必须引用database.Cursor,Contacts.People 与 net.uri等类来使用联系人数据*/ import android.database.Cursor; import android.net.Uri; import android.provider.Contacts.People;
@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (requestCode) { case PICK_CONTACT_SUBACTIVITY: final Uri uriRet = data.getData(); if(uriRet != null) { try { /* 必须要有android.permission.READ_CONTACTS权限 */ Cursor c = managedQuery(uriRet, null, null, null, null); /*将Cursor移到资料最前端*/ c.moveToFirst(); /*取得联系人的姓名*/ String strName = c.getString(c.getColumnIndexOrThrow(People.NAME)); /*取得联系人的电话*/ String strPhone = c.getString(c.getColumnIndexOrThrow(People.NUMBER)); /*将姓名与电话写入EditText01,EditText02中*/ mEditText01.setText(strName); mEditText02.setText(strPhone); } catch(Exception e) { /*将错误信息在TextView中显示*/ mTextView01.setText(e.toString()); e.printStackTrace(); } } break; } super.onActivityResult(requestCode, resultCode, data); }
如果只是查询电话簿,需 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
如果需要增加修改删除则需 <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>