Android Contacts Management One of the contacts get

Source: Internet
Author: User

As we know, Android's contacts and SMS management is developed via ContentProvider to the developer interface. Must start from the Contentresolver to solve. The Address book operation involves the use of the system source API, especially in the table URI above easy to confuse. In the next few articles, snails will continue to introduce Android communication management related knowledge of the article. These include contact acquisition, call history acquisition, SMS acquisition, SMS details to get sent SMS. No more nonsense.

First look at the structure of the contact's table


Which for the development of this is mainly concerned about the above three tables, which to use the joint query, about three table design can be Baidu to related articles.

The code eventually


Contact:


Want the source of children's shoes can be downloaded http://download.csdn.net/detail/waniu123/8554533;

Package cn.zxw.contact.domain;/** * Contact * @author Zhan * */public class Contactsinfo {public int _id;public String NAME;PU Blic 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=" + E Mail + "]";}}
Get Contacts
/** * Get Contact * * @param context * @return */public static list<contactsinfo> Getcontactsinfos (context context) {Cont Entresolver resolver = Context.getcontentresolver (); list<contactsinfo> infos = new arraylist<contactsinfo> ();//Get Contact data access to the content provider of the contact// Contactscontract.authority com.android.contacts Authorization//The content provider action is required to read/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,null, null); while (Cursor1.movetonext ()) {int _id = Curs Or1.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, NULL, 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 Logs * * @param context * @return */public list<callloginfo> Getcalllog (context context) {List<calll oginfo> infos = new arraylist<callloginfo> (); Contentresolver CR = Context.getcontentresolver (); Uri uri = Calls.content_uri; string[] projection = new string[] {calls.number, calls.date,calls.type}; cursor cursor = cr.query (URI, projection, NULL, NULL, 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<contactsinfo> infos = Contactsmsgutils.getcontactsinfos (Getapplicationcontext ()); Myadapter adapter = new Myadapter (infos); Lv.setadapter (adapter);} Private class Myadapter extends Baseadapter {//list<contactsinfo> infos = new arraylist<contactsinfo> (); Private TextView tv_number;private TextView tv_name;public myadapter (list&Lt Contactsinfo> 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 (Getapplicationco ntext (), 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;}}}










Android Contacts Management One of the contacts get

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.