Android Address Book Management 1 contact acquisition
As we know, Android address book and SMS management use contentprovider to Develop interfaces for developers. You must start with ContentResolver. The address book operation involves the use of the system source code api, especially on the uri of the table. In the following articles, snail mail will gradually introduce Android communication management knowledge. These include contact acquisition, call record acquisition, SMS retrieval, and SMS details retrieval. Not to mention nonsense first
First look at the structure of the contact table
For development, we mainly focus on the above three tables, including the use of joint queries. for the design of the three tables, we can refer to Baidu to relevant articles.
Code final
Contact:
You can download the source code from http://download.csdn.net/detail/waniu123/8554533;
Package cn. zxw. contact. domain;/*** contact * @ author zhan **/public class ContactsInfo {public int _ id; public String name; public String phone; public String email; public ContactsInfo () {super ();} public ContactsInfo (int _ id, String name, String phone, String email) {super (); this. _ id = _ id; this. name = name; this. phone = phone; this. email = email ;}@ Overridepublic String toString () {return "PersonInfo [_ id =" + _ id + ", name =" + name + ", phone = "+ phone +", email = "+ email +"] ";}}
Get contact
/*** Get the contact ** @ param context * @ return */public static List
GetContactsInfos (Context context) {ContentResolver resolver = context. getContentResolver (); List
Infos = new ArrayList
(); // Obtain the contact data to access the contact's content provider // ContactsContract. AUTHORITY com. android. contacts authorization // This content provider operation requires read and write permissions // matcher. addURI (ContactsContract. AUTHORITY, "raw_contacts", // RAW_CONTACTS); // matcher. addURI (ContactsContract. AUTHORITY, "raw_contacts/#/data", // RAW_CONTACTS_DATA); Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); Cursor cursor1 = resolver. query (uri, new String [] {"_ id"}, null); while (cursor1.moveToNext () {int _ id = cursor1.getInt (0 ); contactsInfo info = new ContactsInfo (); uri = Uri. parse ("content: // com. android. contacts/raw_contacts/"+ _ id +"/data "); Cursor cursor2 = resolver. query (uri, new String [] {"data1", "mimetype"}, null); while (cursor2.moveToNext () {String data1 = cursor2.getString (0 ); string mimetype = cursor2.getString (1); if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {// number info. phone = data1;} else if ("vnd. android. cursor. item/name ". equals (mimetype) {// name info. name = data1 ;}} cursor2.close (); infos. add (info);} cursor1.close (); return infos;}/*** get all call records ** @ param context * @ return */public List
GetCallLog (Context context) {List
Infos = new ArrayList
(); ContentResolver cr = context. getContentResolver (); Uri uri = CILS. CONTENT_URI; String [] projection = new String [] {CILS. NUMBER, CILS. DATE, CILS. TYPE}; Cursor cursor = cr. query (uri, projection, null); while (cursor. moveToNext () {String number = cursor. getString (0); long date = cursor. getLong (1); int type = cursor. getInt (2); infos. add (new CallLogInfo (number, date, type);} cursor. close (); return infos ;}
Package cn. zxw. contact; import java. util. arrayList; import java. util. list; import cn. zxw. contact. domain. contactsInfo; import cn. zxw. contact. utils. contactsMsgUtils; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView; import android. app. activity;/*** contact ** @ author zhan **/public class ContactsActivity extends Activity {private ListView lv; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_contacts_msg_calllog); lv = (ListView) findViewById (R. id. lv); List
Infos = ContactsMsgUtils. getContactsInfos (getApplicationContext (); MyAdapter adapter = new MyAdapter (infos); lv. setAdapter (adapter);} private class MyAdapter extends BaseAdapter {// List
Infos = new ArrayList
(); Private TextView TV _number; private TextView TV _name; public MyAdapter (List
Infos) {super (); this. infos = infos;} @ Overridepublic int getCount () {return infos. size () ;}@ Overridepublic Object getItem (int position) {return null ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {View view = View. inflate (getApplicationContext (), R. layout. contacts_list_item, null); TV _number = (TextView) view. findViewById (R. id. TV _number); TV _name = (TextView) view. findViewById (R. id. TV _name); ContactsInfo info = infos. get (position); TV _number.setText (info. phone); TV _name.setText (info. name); return view ;}}}