Managing text messages in Android

Source: Internet
Author: User

To viewCodeEasy to use, while viewing Android JavaSource code.

It happened to be a class of mmssmsdatabasehelper. java. Android stored all the text messages in mmssms. DB.

This class is not available in public sdks and cannot be used directly. Therefore, I wrote a sqliteopenhelper myself, but an SQL exception occurred during the query.

It seems that you cannot do whatever you want, but according to the online information, you can copy dB files to back up text message data.

Since each dB is related to the package name, a project named com. Android. providers. telephony has been created to test whether the package is successful.

The result output is Please execute 'adb uninstall com. Android. providers. telephony 'in a shell. Android security is very powerful.

You cannot directly access the database. You can only access the database through the Protocol,
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.
Address Sender's mobile phone number
Person The serial number in the contact list. The stranger is null.
Date Date of delivery
Protocol Protocol, divided into: 0 sms_rpoto, 1 mms_proto
Read Whether to read 0 unread, 1 read
Status Status-1 received, 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 Number of the SMS Service Center
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 );
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 Fields Query the 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
Condition SQL condition characters without Where are used. If there is a parameter? Replacement, such as "_ id =? And thread_id =? Or type = '1 '"
Parameters in the condition Parameter character array, one-to-one correspondence with the preceding conditions
Sort 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:
<Uses-Permission Android: Name = "android. Permission. write_sms"> </uses-Permission>
Then delete the text message:
This. getcontentresolver (). Delete (URI. parse ("content: // SMS"), "_ id =? ", New string [] {" 3 "});
Deleted successfully.

URLContent: // SMSReplaceContent: // SMS/But other URLsProgramAn error is reported, for exampleContent: // 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 based on the conditions in the parameter
sms_all_id Delete SMS table data based on _ id
sms_conversations_id : delete SMS table data based on thread_id, the raw table can be deleted with other conditions
sms_raw_message Based on the conditions in the parameter
sms_status_pending Based on the conditions in the parameter to delete the sr_pending table
sms_sim delete data from SIM card

Try sms_conversations_id :"Content: // SMS/conversations/3", Delete thread_id =" 3 ", _ id =" 5 "Data
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
fail
sms_all
sms_failed
sms_queued
sms_inbox
sms_sent
sms_draft
sms_outbox
BR> sms_conversations
sms_all_id
sms_inbox_id
comment
sms_sent_id
sms_draft_id
sms_outbox_id
comment
sms_status_id

Test it with sms_inbox_id:
Contentvalues CV = new contentvalues ();
Cv. Put ("thread_id", "2 ");
Cv. Put ("addresses", "00000 ");
Cv. Put ("person", "11 ");
Cv. Put ("day", "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 failing 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 ("addresses", "9999 ");
Cv. Put ("person", "888 ");
Cv. Put ("day", "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 very good, so that you can query all the tables in the database, but also multi-table joint Query

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.