Android Add SMS, get mobile SMS, delete SMS and modify SMS

Source: Internet
Author: User

Note Add permissions:

    <uses-permission android:name= "Android.permission.READ_SMS" ></uses-permission>    < Uses-permission android:name= "Android.permission.WRITE_SMS" ></uses-permission>

The code is as follows:

//Update SMSPrivate voidupdatesms () {/*the update supports a number of protocols: Sms_raw_message sms_status_pending sms_all sms_failed sms_queued sms_inbox sms_sent Sms_draft sms_outbox sms_conversations sms_all_id sms_inbox_id sms_failed_id sms_sent_id SMS_DRAFT_ ID sms_outbox_id sms_conversations_id sms_status_id*/contentvalues CV=Newcontentvalues (); Cv.put ("thread_id", "2"); Cv.put ("Address", "00000"); Cv.put ("Person", "11"); //cv.put ("date", "11111111"); Cv.put ("Body", "hello,modify content333333333"); Getcontentresolver (). Update (Uri.parse ("CONTENT://SMS/SENT/3"), CV,NULL,NULL); //getcontentresolver (). Update (Uri.parse ("Content://SMS/SENT/3 "), CV," id= ", New string[]{" 3 "}); }//Delete SMSPrivate voiddeletesms () {/** Among the protocols supported in the Delete method are: * Sms_all delete SMS table data according to the criteria in the parameter * sms_all_id delete SMS table data based on _ID * sms_conversations_id based on Thread_ ID Delete SMS table data, can take other conditions * sms_raw_message Delete RAW table based on conditions in Parameters * sms_status_pending Delete sr_pending table based on conditions in parameters * Sms_sim Delete data from SIM card * Try sms_conversations_id: "Content://sms/conversations/3", delete thread_id= "3", _id= "5" data in the emulator* control in Eclipse, Send three data to the simulator with 13800 and send A * this.getcontentresolver () to 13900. delete* (Uri.parse ("CONTENT://SMS/CONVERSATIONS/3"), "_ Id=? ", new* string[]{" 5 "}); A piece of data was successfully deleted. * Although the thread_id of each sender in the database is the same, but not fixed, if all the data of a sender is deleted, * and then a new number is sent to send the text message, THREAD_ID is assigned the largest id+1 in the database. *//*Looking at the source code of Android, SMS supports the following protocols: Surlmatcher.adduri ("SMS", NULL, Sms_all); Surlmatcher.adduri ("SMS", "#", sms_all_id); Surlmatcher.adduri ("SMS", "Inbox", Sms_inbox); Surlmatcher.adduri ("SMS", "inbox/#", sms_inbox_id); Surlmatcher.adduri ("SMS", "sent", sms_sent); Surlmatcher.adduri ("SMS", "sent/#", sms_sent_id); Surlmatcher.adduri ("SMS", "Draft", Sms_draft); Surlmatcher.adduri ("SMS", "draft/#", sms_draft_id); Surlmatcher.adduri ("SMS", "Outbox", Sms_outbox); Surlmatcher.adduri ("SMS", "outbox/#", sms_outbox_id); Surlmatcher.adduri ("SMS", "undelivered", sms_undelivered); Surlmatcher.adduri ("SMS", "failed", sms_failed); Surlmatcher.adduri ("SMS", "failed/#", sms_failed_id); Surlmatcher.adduri ("SMS", "queued", sms_queued); Surlmatcher.adduri ("SMS", "Conversations", sms_conversations); Surlmatcher.adduri ("SMS", "conversations/*", sms_conversations_id); Surlmatcher.adduri ("SMS", "raw", sms_raw_message); Surlmatcher.adduri ("SMS", "attachments", sms_attachment); Surlmatcher.adduri ("SMS", "attachments/#", sms_attachment_id); Surlmatcher.adduri ("SMS", "ThreadID", sms_new_thread_id); Surlmatcher.adduri ("SMS", "threadid/*", sms_query_thread_id); Surlmatcher.adduri ("SMS", "status/#", sms_status_id); Surlmatcher.adduri ("SMS", "sr_pending", sms_status_pending); Surlmatcher.adduri ("SMS", "Sim", Sms_all_sim); Surlmatcher.adduri ("SMS", "sim/#", Sms_sim);*///getcontentresolver (). Delete (Uri.parse ("Content://SMS/FAILED/3 ")," _id= ", New string[]{" 99 "}); getcontentresolver (). Delete (Uri.parse ("content://sms/#"), "_id=?",Newstring[]{"99"}); }//Add SMSPrivate voidaddsms () {/** INSERT Supported protocols: * * Sms_all * sms_inbox * sms_failed * sms_queued * sms_sent * sms_draft* sms_outbox * sms_raw_message * sms_status_pending * sms_attachment* sms_new_thread_id* * When inserting data into an SMS table, the type is automatically set according to the protocol, * automatically set to the current system time if no date is set in the incoming data , non-Sms_inbox protocol, when the read flag is set to the 1* Sms_inbox protocol, the system automatically queries and sets the person threadid to null or 0 o'clock, the system will automatically set * * has been to create a "send failed" message to worry about, now to do a: Content://sms/failed*/String ID=NULL; Contentvalues CV=Newcontentvalues ();  for(inti = 0;i<5;i++) {id.valueof (i); Cv.put ("_ID", id); Cv.put ("thread_id", "0"); Cv.put ("Address", "9999"); Cv.put ("Person", "888"); //cv.put ("date", "9999");Cv.put ("Protocol", "0"); Cv.put ("Read", "1"); Cv.put ("Status", "1"); Cv.put ("Body", "" "); Getcontentresolver (). Insert (Uri.parse ("Content://sms/sent"), CV); Cv.clear ();} }//Query SMS ContentPrivate voidgetsms () {/** Content://sms/inbox Inbox content://sms/sent sent * content://sms/draft draft content://sms/outbox Outbox * content://sms/ Failed send failed content://sms/queued to send list * The SMS-related fields in the database are as follows: * _id a self-increment field, starting from 1 thread_id serial number, same Sender ID * thre of the same contact            AD_ID is the same, by traversing the thread ID and contrast to know a contact has a few text messages; * Of course, there is a message_out field in the Threads table also shows a contact, but the threads table has not found a way to read; * Address Sender's mobile number * The ordinal of the person's contact list, the stranger is null, that is, the contact_id of the Rawcontact table, or the _id;* of the Contact table can be associated with this ID; However, the person corresponds to the result is unstable, should use phonelookup.content_filter_uri* Date sent by protocol protocol, divided into: 0 sms_rpoto, 1 Mms_proto * Read 0 unread , 1 Read * Status state-1 receive, 0 complete, pending, failed * type all = 0; INBOX = 1; SENT = 2; DRAFT = 3; OUTBOX = 4; FAILED = 5; QUEUED = 6; * Body SMS Content * Service_center SMS Service center number * Subject SMS Theme * Reply_path_present tp-reply-path locked*/LOG.I ("SMS", "Start getting SMS"); string[] Projection=Newstring[] {"_ID",          "Address",          "Person",          "Date",          "Protocol",          "Read",          "Status",          "Body",          "Service_center",          "Subject"         }; Cursor cur= Getcontentresolver (). Query (Uri.parse ("content://sms"), Projection,NULL,NULL, "Date desc"); StringBuilder Str=NewStringBuilder (); if(Cur.movetofirst ()) {String name;                   String PhoneNumber;            String SMS; Longdate;            String ID; intIdcolumn = Cur.getcolumnindex ("_id"); intNameColumn = Cur.getcolumnindex ("Person"); intPhonecolumn = Cur.getcolumnindex ("Address"); intSmscolumn = Cur.getcolumnindex ("Body"); intDatecolumn=cur.getcolumnindex ("Date");  Do {                //Get the field valuesname=cur.getstring (NameColumn); PhoneNumber=cur.getstring (Phonecolumn); SMS=cur.getstring (Smscolumn); Date=Cur.getlong (Datecolumn); String Temp=long2string (date); ID=cur.getstring (Idcolumn); //date = String2long (date);Str.append ("{"); Str.append (Name+","); Str.append (PhoneNumber+","); Str.append (SMS+","); Str.append (Temp+",");                Str.append (ID); Str.append ("}"); if(NULL==SMS) SMS=""; }  while(Cur.movetonext ()); }        Else{str.append ("No result!"); } log.i ("SMS", str.tostring ());        Toast.maketext (Context, str.tostring (), Toast.length_short). Show (); System.out.print ("SMS Result:" +str.tostring ()); Cur.close ();}PrivateString long2string (LongTime ) {SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy mm month DD Day HH:mm:ss:SSSS");returnSdf.format (NewDate (time));

 

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.