[Android] Text Message application -- text message (Session) Deletion

Source: Internet
Author: User

Compared with those who have used Android phones, we all know that androidos text messages are displayed in session mode like those in iOS, which makes it easy for users to find previous chat records.

UsageCodeDeleting a text message actually deletes a conversation, both recent and previous.

Next let's look at the code.

First, you need to get the text message directory.

 
Contentresolver CR; CR = getcontentresolver ();

Then, you need to review all text messages.

 
String sms_read_column = "read"; string where_condition = sms_read_column + "= 0"; string sort_order = "date DESC"; int COUNT = 0; cursor = CR. query (URI. parse ("content: // SMS/inbox"), new string [] {"_ id", "thread_id", "Address", "person", "date ", "body"}, where_condition, null, sort_order); If (cursor! = NULL) {try {COUNT = cursor. getcount (); If (count> 0) {cursor. movetofirst (); threadid = cursor. getlong (1) ;}} finally {cursor. close ();}}

"Content: // SMS/inbox" is the path of the SMS, inbox, and inbox.

The above code can get the latest text message.

We need to call this method. We can call this method.

 
Long id = getthreadid (); Uri muri = URI. parse ("content: // SMS/conversations/" + id );

In this way, you can get the ID of the latest hop SMS.

Delete the file.

 
Cr. Delete (muri, null, null );

Do not forget to add permissions to manifest. xml.

 
<Uses-Permission Android: Name = "android. Permission. write_sms"/> <uses-Permission Android: Name = "android. Permission. read_sms"/>

In addition, if you want to obtain the latest inbox information, you must obtain the ID again. The ID here is always 0, but if you directly replace the ID with 0, the deleted items are not the latest ones.

If you want to delete other text messages, you only need to add conditions during traversal so that the returned ID is the session ID corresponding to the condition,

Another point is to emphasize that this method can only delete unread text message conversations, because its search condition is inbox, which is of course the inbox; if the text message is read, then the Android system should determine that the information is not in the inbox.

Demo download: http://download.csdn.net/detail/etzmico/3975576

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.