Get mobile phone contacts,
Package com.org. demo. demo; import com.org. wangfeng. r; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. editText; public class GetPhoneActivity extends Activity {private EditText editText; private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. getphoneactivity); editText = (EditText) findViewById (R. id. et_getphone); button = (Button) findViewById (R. id. bb_getphone); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub Intent intent = new Intent (GetPhoneActivity. this, ContactActivity. class); startActivityForResult (intent, 1) ;}}) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// System. out. println ("resultCode:" + resultCode); // System. out. println ("requestCode:" + requestCode); Log. d ("jiejie", "called"); if (resultCode = Activity. RESULT_ OK) {String phone = data. getStringExtra ("phone"); Log. d ("jiejie", "___________" + phone); phone = phone. replaceAll ("-",""). replaceAll ("", ""); // replace-and space editText. setText (phone); // set the phone number to the input box} super. onActivityResult (requestCode, resultCode, data );}}
Package com.org. demo. demo; import java. util. arrayList; import java. util. hashMap; import com.org. wangfeng. r; import android. app. activity; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. listView; import android. widget. simpleAdapter; import android. widget. adapterView. OnItemClickListener; import android. widget. textView; public class ContactActivity extends Activity {private ListView lvList; private ArrayList <HashMap <String, String> readContact; private TextView back; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_contact); back = (TextView) findViewById (R. id. back); back. setOnClic KListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub finish () ;}}); lvList = (ListView) findViewById (R. id. lv_list); readContact = readContact (); // System. out. println (readContact); lvList. setAdapter (new SimpleAdapter (this, readContact, R. layout. contact_list_item, new String [] {"name", "phone"}, new int [] {R. id. TV _name, R. id. TV _pho Ne}); lvList. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {String phone = readContact. get (position ). get ("phone"); // read the phone number of the current item Intent intent = new Intent (); intent. putExtra ("phone", phone); setResult (Activity. RESULT_ OK, intent); // put the data in intent and return it to finish () ;}});} private ArrayList <HashMap <String, String> readContact () {// first, read the contact id ("contact_id") from raw_contacts. // second, query the corresponding phone numbers and contacts from the data table based on the contact_id. Name // then, based on mimetype, identify the contact and the phone number Uri rawContactsUri = Uri. parse ("content: // com. android. contacts/raw_contacts "); Uri dataUri = Uri. parse ("content: // com. android. contacts/data "); ArrayList <HashMap <String, String> list = new ArrayList <HashMap <String, String> (); // read the contact id ("contact_id") from raw_contacts. Cursor rawContactsCursor = getContentResolver (). query (rawContactsUri, new String [] {"contact_ I D "}, null); if (rawContactsCursor! = Null) {while (rawContactsCursor. moveToNext () {String contactId = rawContactsCursor. getString (0); // System. out. println (contactId); // query the corresponding phone number and contact name from the data table based on contact_id. The actual query is view view_data Cursor dataCursor = getContentResolver (). query (dataUri, new String [] {"data1", "mimetype"}, "contact_id =? ", New String [] {contactId}, null); if (dataCursor! = Null) {HashMap <String, String> map = new HashMap <String, String> (); while (dataCursor. moveToNext () {String data1 = dataCursor. getString (0); String mimetype = dataCursor. getString (1); // System. out. println (contactId + ";" + data1 + ";" // + mimetype); if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {map. put ("phone", data1);} else if ("vnd. android. cursor. item/name ". equals (mimetype) {map. put ("name", data1) ;}} list. add (map); dataCursor. close () ;}} rawContactsCursor. close () ;}return list ;}}