Android Contacts (ii) SMS messages are associated with Contacts contacts

Source: Internet
Author: User
Tags filter dateformat

Android SMS Read SMS, you can get the sender/recipient's mobile phone number (address), contacts contact, can filter mobile phone number (addressed), so SMS can be connected to contacts contact via mobile phone number (address)

Sms-contacts Association Code

By Address cell Phone Number association contacts the display name of the contact person     
    private string Getpeoplenamefromperson.     
        || Address = = "") {return     
            ' (no address) \ n ";     
        }     

        String Strperson = "null";     
        string[] projection = new string[] {phone.display_name, phone.number};     

        Uri Uri_person = Uri.withappendedpath (ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);  Address Mobile number Filter     
        Cursor Cursor = Getcontentresolver (). Query (Uri_person, projection, NULL, NULL, NULL);     

        if (Cursor.movetofirst ()) {     
            int index_peoplename = Cursor.getcolumnindex (phone.display_name);     
            String strpeoplename = cursor.getstring (index_peoplename);     
            Strperson = Strpeoplename;     
        }     
        Cursor.close ();     

        return Strperson;     
    }

Sms-contacts Association Sample code:

Package com.homer.phone;     
Import Java.sql.Date;     

Import Java.text.SimpleDateFormat;     
Import android.app.Activity;     
Import Android.database.Cursor;     
Import android.database.sqlite.SQLiteException;     
Import Android.net.Uri;     
Import Android.os.Bundle;     
Import Android.provider.ContactsContract;     
Import Android.provider.ContactsContract.CommonDataKinds.Phone;     
Import Android.util.Log;     
Import Android.widget.ScrollView;     

Import Android.widget.TextView;     
        public class PhoneRead2 extends activity {@Override public void onCreate (Bundle savedinstancestate) {     

        Super.oncreate (savedinstancestate);     
        TextView TV = new TextView (this);     

        Tv.settext (Getsmsinphone ());     
        ScrollView SV = new ScrollView (this);     

        Sv.addview (TV);     
    Setcontentview (SV);     
     public string Getsmsinphone () {final string sms_uri_all = "content://sms/";   Final String Sms_uri_inbox = "Content://sms/inbox";     
        Final String sms_uri_send = "Content://sms/sent";     
        Final String Sms_uri_draft = "Content://sms/draft";     
        Final String Sms_uri_outbox = "Content://sms/outbox";     
        Final String sms_uri_failed = "content://sms/failed";     

        Final String sms_uri_queued = "content://sms/queued";     

        StringBuilder Smsbuilder = new StringBuilder ();     
            try {uri uri = Uri.parse (Sms_uri_all);     
            string[] projection = new string[] {' _id ', ' address ', ' person ', ' body ', ' date ', ' type '};      Cursor cur = getcontentresolver (). Query (URI, projection, null, NULL, "date desc"); Get phone internal SMS if (Cur.movetofirst ()) {int index_address = Cur.getcolumnindex ("Addres     
                S ");     
                int Index_person = Cur.getcolumnindex ("person");     
  int index_body = Cur.getcolumnindex ("Body");              int index_date = Cur.getcolumnindex ("Date");     
         
                int index_type = Cur.getcolumnindex ("Type");     
                    do {String straddress = cur.getstring (index_address);     
                    int Intperson = Cur.getint (Index_person);     
                    String strbody = cur.getstring (index_body);     
                    Long longdate = Cur.getlong (index_date);     
         
                    int inttype = Cur.getint (Index_type);     
                    SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");     
                    Date d = new Date (longdate);     
         
                    String strdate = Dateformat.format (d);     
                    String strtype = "";     
                    if (inttype = = 1) {strtype = "receive";     
                    else if (Inttype = 2) {strtype = "send"; } ELSE {strtype = "null";     
                             
                    String strAddress2 = Getpeoplenamefromperson (straddress);     
Smsbuilder.append ("[");     
                    Smsbuilder.append (straddress + ",");     
                    Smsbuilder.append (straddress + ":" + StrAddress2 + ",");     
                    Smsbuilder.append (Intperson + ",");     
                    Smsbuilder.append (Strbody + ",");     
                    Smsbuilder.append (strdate + ",");     
                    Smsbuilder.append (strtype);     
                Smsbuilder.append ("]\n\n");

                while (Cur.movetonext ());     
                    if (!cur.isclosed ()) {cur.close ();     
                cur = null;     
            } else {smsbuilder.append ("no result!"); }//End If SMSBUIlder.append ("Getsmsinphone has executed!");     
        catch (Sqliteexception ex) {LOG.D ("sqliteexception in Getsmsinphone", Ex.getmessage ());
    return smsbuilder.tostring ();  //via Address cell phone number associated contacts contact's display name private string Getpeoplenamefromperson null | |
        Address = = "") {return "(no address) \ n";
        } String Strperson = "null";

        string[] projection = new string[] {phone.display_name, phone.number};  Uri Uri_person = Uri.withappendedpath (ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);

        Address Mobile number filter Cursor Cursor = Getcontentresolver (). Query (Uri_person, projection, NULL, NULL, NULL);
            if (Cursor.movetofirst ()) {int index_peoplename = Cursor.getcolumnindex (phone.display_name);
            String strpeoplename = cursor.getstring (index_peoplename);
        Strperson = Strpeoplename;
   }     Cursor.close ();
    return Strperson; }     

}

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.