[Android] Text Message Management Code

Source: Internet
Author: User

First paste the relevant Protocols:
Content: // SMS/inbox
Content: // SMS/sent
Content: // SMS/Draft draft
Content: // SMS/outbox sender
Content: // SMS/failed to send
Content: // SMS/queued list to be sent

No data was found in outbox on the simulator, and the outbox was not found on the simulator for a long time. It was very depressing.
The following are the SMS-related fields in the database:

_ ID: An auto-increment field, starting from 1. thread_id serial number. the ID of the same sender is the same as the serial number in the contact list of the sender's mobile phone number, and the stranger is null date. Protocol, divided into: 0 sms_rpoto, 1 mms_proto read whether read 0 unread, 1 read status-1 receive, 0 complete, 64 pending, 128 failed type all = 0; inbox = 1; sent = 2; draft = 3; outbox = 4; failed = 5; queued = 6; body text message content service_center text message service center number subject text message subject reply_path_present TP-reply-path locked

The data retrieval method is simple:

Uri uri = Uri.parse("content://sms/inbox");         Cursor cur = this.managedQuery(uri, null, null, null, null);         if (cur.moveToFirst()) {             do{         for(int j = 0; j < cur.getColumnCount(); j++){                 info = "name:" + cur.getColumnName(j) + "=" + cur.getString(j);             Log.i("====>", info);         }     }while(cur.moveToNext());      }

Managedquery also converts parameters to SQL statements to send messages to SQLite. Therefore, parameters are similar to SQL statements. Therefore, you can add SQL functions to the query fields,
For example, new string [] projection = new string [] {"count (*) as Count.
The parameters in managedquery are Uri,
Query field query field array, you can also put all the fields to be queried into one character
For example, new projection [] {"_ ID", "thread_id"} and new projection [] {"_ id, thread_id"} are consistent.
Like SQL, field names are case-insensitive
The condition does not contain the SQL condition character of where. If there is a parameter, use? Replacement, such as "_ id =? And thread_id =? Or type = '1 ′"
Parameter parameter character array in the condition, one-to-one correspondence with the preceding Condition
Sort strings without order by, such as _ id DESC and type
If the parameter is null, the SQL query field is "*", and the related conditions are blank.
You can also use getcontentresolver () to obtain a contentresolver,
Getcontentresolver (). Query () returns a cursor object with the same parameters as managedquery.
However, securityexception is reported when you use the contentresolver object to update, delete, and insert a data record. It seems that you do not have the permission. Add the permission to manifest. xml:

Then delete the text message:
This. getcontentresolver (). Delete (URI. parse ("content: // SMS"), "_ id = ?", New String [] {"3 ″});
Deleted successfully.
Content: // SMS in the URL is successfully replaced with content: // SMS/, but the program reports an error in other URLs, such as content: // SMS/inbox.
After reading the android source code, the protocols supported by SMS include:

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);


The supported protocols in the delete method are:
Sms_all Delete SMS table data according to the conditions in the Parameter
Sms_all_id Delete SMS table data by _ id
Sms_conversations_id: deletes the SMS table data based on thread_id. It can contain other conditions.
Sms_raw_message: Delete the raw table based on the conditions in the parameter.
Sms_status_pending: Delete the sr_pending table based on the conditions in the parameter.
Sms_sim deletes data from SIM card
Try sms_conversations_id: "content: // SMS/conversations/3" and delete the data of thread_id = "3", _ id = "5 ".
In emulator control in eclipse, send three pieces of data to the simulator as 13800, and then send one entry as 13900
This. getcontentresolver (). Delete (URI. parse ("content: // SMS/conversations/3"), "_ id = ?", New String [] {"5 ″});
A data entry is deleted successfully.
The thread_id of each sender in the database is the same, but it is not fixed. If you delete all the data of a sender,
Then, when sending text messages with a new number, thread_id is assigned with the largest ID + 1 in the database.

Update supports many 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
Test it with sms_inbox_id:
Contentvalues CV = new contentvalues ();
Cv. Put ("thread_id", "2 ″);
Cv. Put ("Address", "00000 ″);
Cv. Put ("person", "11 ″);
Cv. Put ("date", "11111111 ″);
This. getcontentresolver (). Update (URI. parse ("content: // SMS/inbox/4"), CV, null, null );
Too strong. You can modify thread_id.

Protocols Supported by insert:
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 the SMS table, the type is automatically set according to the protocol,
If date is not set in the input data, it is automatically set to the current system time; if the protocol is not sms_inbox, the read flag is set to 1.
When the sms_inbox protocol is used, the system automatically queries and sets the person
When threadid is null or 0, the system automatically sets
I have been worried about failure to send emails. Now I will do one:
Content: // SMS/failed
Contentvalues CV = new contentvalues ();
Cv. Put ("_ ID", "99 ″);
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 ("type", "0 ″);
Cv. Put ("body", "");
This. getcontentresolver (). insert (URI. parse ("content: // SMS/failed"), CV );
Type is set to 5, thread_id is set to 1

The system did not even perform the minimum data verification. Google was so kind to programmers.
See if you can explore the functions of SMS. First, make an error query:
Getcontentresolver (). Query (URI. parse ("Content: // SMS/"), new string [] {"A"}, "B", null, null );
SQL statements with log output errors:
Select a from SMS where (B) Order by date DESC
There is no group by in the query method. If you want to make statistics on text messages, it is too slow to traverse the cursor and then make statistics.
In SQL, if group by is behind where, you can find a solution in the condition parameter:
When Android organizes an SQL statement, add () to both ends of the condition, and then combine a group by statement:
Getcontentresolver (). query (URI. parse ("content: // SMS/"), new string [] {"count (*) as Count, thread_id"}, "1 = 1) group by (thread_id ", null, null );
Then the output SQL = select count (*) as Count, thread_id from SMS where (1 = 1) group by (thread_id) order by date DESC
What should I do if I want to query tables that do not have a uri? For example, I want to know which tables are in the mmssms. DB database,
The queried table is set by Uri, and it is certainly not possible to splice it in the condition parameters.
Let's move our eyes forward to see if we can't make it together in the field parameters.
To query other tables, remove the from SMS that is added by the system,
Use the notes in SQL,
Getcontentresolver (). query (URI. parse ("content: // SMS/"), new string [] {"* From sqlite_master where type = 'table'-"}, null );
Then the output SQL = select * From sqlite_master where type = 'table'-from SMS order by date DESC
Can run.
If ";" can also be used, haha, you can do whatever you want to create, delete, or update a table.
Getcontentresolver (). query (URI. parse ("Content: // SMS/"), new string [] {"* from SMS; select * From thrreads;-"}, null );
Unfortunately, only the first SQL statement is run. It seems that android has some control over the key issues.
However, the support is also good, so that you can query all the tables in the database and perform multi-table join queries.

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.