Android read SMS and Contacts

Source: Internet
Author: User

Reading text messages and contacts is often used, to understand that this is the content provider's knowledge point

say the text first, to get data from a database, you need to understand its structure

The following path to the SMS database in Data-->data

Its table structure is as follows, there are 3 we should pay attention to

The Java code is

//Get content providerContentresolver Contentresolver =Getcontentresolver (); //get the path to the SMS tableUri uri = uri.parse ("Content://sms"); //set the name of the column to queryString[] line = {"Address", "date", "Body"}; //the meaning of each parameter, path, column name, condition, condition parameter, sortcursor cursor = contentresolver.query (URI, line,NULL,NULL,NULL); //The following is the same as working with a normal database.        if(Cursor! =NULL) {             while(Cursor.movetonext ()) {String address= Cursor.getstring (Cursor.getcolumnindex ("Address")); String Date= Cursor.getstring (Cursor.getcolumnindex ("date")); String Body= Cursor.getstring (Cursor.getcolumnindex ("Body")); LOG.E ("SMS", "Address:" + address + "\ndate:" + Date + "\nbody:" +body);        } cursor.close (); }

Getting a contact is complicated, and the table structure is more complex

The following path in the location data-->data of your database

Data table

Mimetypes table

Raw_contacts table

Once you know the results of the table, you start writing code.

       Private list<contactsdata> Contactsdatas = new arraylist<> ();
        //Get content providerContentresolver Contentresolver =Getcontentresolver (); //get the path to the Raw_contacts tableUri Raw_contact_uri = Uri.parse ("Content://com.android.contacts/raw_contacts"); //the meaning of each parameter, path, column name, condition, condition parameter, sortCursor contactId = Contentresolver.query (Raw_contact_uri,Newstring[]{"contact_id"},NULL,NULL,NULL); //The following is the same as working with a normal database.        if(ContactId! =NULL) {             while(Contactid.movetonext ()) {//gets the value of the contact_id that corresponds to the value of raw_contact_id in the data tableString id = contactid.getstring (contactid.getcolumnindex ("contact_id")); //get the path to the data tableUri Data_uri = Uri.parse ("Content://com.android.contacts/data"); //the meaning of each parameter, path, column name, condition, condition parameter, sortCursor datacursor = Contentresolver.query (Data_uri,Newstring[]{"MimeType", "Data1"},                        "Raw_contact_id=?",NewString[]{id},NULL); if(Datacursor! =NULL) {                    //Each loop creates an instance to hold the data in the database tableContactsdata Contactsdata =NewContactsdata ();  while(Datacursor.movetonext ()) {String type= Datacursor.getstring (Datacursor.getcolumnindex ("MimeType")); Switch(type) { Case"Vnd.android.cursor.item/email_v2":                                //This is the mailbox informationContactsdata.setemail (Datacursor.getstring (Datacursor.getcolumnindex ("Data1")));  Break;  Case"Vnd.android.cursor.item/phone_v2":                                //This is the cell phone number information .Contactsdata.setnumber (Datacursor.getstring (Datacursor.getcolumnindex ("Data1")));  Break;  Case"Vnd.android.cursor.item/name":                                //This is the name of the contact.Contactsdata.setname (Datacursor.getstring (Datacursor.getcolumnindex ("Data1")));  Break; }                    }                    //Add the queried information to the collectionContactsdatas.add (Contactsdata);                Datacursor.close ();        }} contactid.close (); }         for(Contactsdata c:contactsdatas) {LOG.E (Contact, c.tostring ()); Toast.maketext ( This, C.tostring (), Toast.length_short). Show (); }

Create a JavaBean for ease of administration

 Public classContactsdata {PrivateString Email; PrivateString number; PrivateString name;  PublicString Getemail () {returnemail; }     Public voidsetemail (String email) { This. email =email; }     PublicString GetNumber () {returnNumber ; }     Public voidSetnumber (String number) { This. Number =Number ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"contactsdata{" + "email=" + email + ' \ ' + ', number= ' + number + ' + ' , Name= ' + name + ' \ ' + '} '; }}

Finally, don't forget to add permissions.

<android:name= "Android.permission.READ_SMS"/><   android:name= "Android.permission.READ_CONTACTS"/>

If you have more than Android 6.0 version also to get permissions dynamically

Get Contact's

Android read SMS and Contacts

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.