SMS (session) Delete (GO)

Source: Internet
Author: User

Friends who have used Android phones know that androidos text messages, like iOS, are presented in conversational mode, which makes it easy for users to find previous chats.

about using code to delete text messages, in fact, the deletion is also a dialogue, whether it is the recent, or the previous, all will be deleted.

Let's look at the code below.

First you need to get the SMS directory.

View Plain
    1. Contentresolver CR;
    2. CR = Getcontentresolver ();

Then there is the need to traverse all text messages.

View Plain
  1. String sms_read_column = "READ";
  2. String where_condition = sms_read_column + "= 0";
  3. String Sort_order = "Date DESC";
  4. int count = 0;
  5. cursor cursor = cr.query (Uri.parse ("Content://sms/inbox"),
  6. New string[] { "_id", "thread_id", "address", "person", "date" ,
  7. "Body"}, Where_condition, null, sort_order);
  8. if (cursor! = null) {
  9. try {
  10. Count = Cursor.getcount ();
  11. if (Count > 0) {
  12. Cursor.movetofirst ();
  13. ThreadId = Cursor.getlong (1);
  14. }
  15. } finally {
  16. Cursor.close ();
  17. }
  18. }



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

The above code can get the latest text message.

We need to call this method and we can take this method.

View Plain
    1. Long id = getthreadid ();
    2. Uri MUri = Uri.parse ("content://sms/conversations/" + ID);

This will give you the ID of the latest hop message.

The final deletion will be possible.

View Plain
    1. Cr.delete (MUri, null, null);


Don't forget to add permissions to the Manifest.xml.

View Plain
    1. <uses-permission android:name="Android.permission.WRITE_SMS"/>
    2. <uses-permission android:name="Android.permission.READ_SMS"/>



It is also important to emphasize that if you want to get the latest Inbox information, be sure to re-obtain the ID, where the ID is always 0, but if the ID is replaced with 0 directly, then the deletion is not the latest.

If you want to delete other text messages, simply add the conditions when traversing, so that the returned ID is the session ID corresponding to the condition.

It is also important to emphasize that this method can only delete unread SMS conversations, because his search condition is inbox, of course, is the Inbox, if you read a text message, then the Android system should be determined that the information does not belong to the inbox.

Real-time SMS information acquisition

We know that only through the code can read the text in the Inbox, the text message in the Outbox, but there is no way to get the instant message sent to the moment, if we can get the corresponding information in the instant message, then we can start to expand a lot of applications-that is, by SMS to remote operation of a mobile phone.

If you want to get it in real time, you need to call receiver and write a listener class so that we can get the short message in real time.

Preview Map:

Let's take a look at the code.

First, we need to create a listener class smsbroadcastreceiver, and let him inherit broadcastreceiver.

Then initialize a constant action and assign the text message related parameter values.

View Plain
    1. Android.provider.Telephony.SMS_RECEIVED

Then create the OnReceive method.

Then use Getaction to monitor the mobile phone message related dynamics, using StringBuffer to save text messages.

Then the main code again.

View Plain
  1. @Override
  2. public void OnReceive (context context, Intent Intent) {
  3. if (intent.getaction (). Equals (ACTION)) {
  4. StringBuffer smsaddress = new StringBuffer ();
  5. StringBuffer smscontent = new StringBuffer ();
  6. Bundle bundle = Intent.getextras ();
  7. if (bundle! = null) {
  8. Object[] pdusobjects = (object[]) bundle.get ("PDUs");
  9. smsmessage[] messages = new Smsmessage[pdusobjects.length];
  10. For (int i = 0; i < pdusobjects.length; i++) {
  11. Messages[i] = Smsmessage
  12. . CREATEFROMPDU ((byte[]) pdusobjects[i]);
  13. }
  14. For (Smsmessage message:messages) {
  15. Smsaddress.append (Message.getdisplayoriginatingaddress ());
  16. Smscontent.append (Message.getdisplaymessagebody ());
  17. }
  18. }
  19. }
  20. }

The smsaddress in the code is the number that sends the text message, Smscontent is the SMS content.

The simplest way to see if it's successful is to print the two parameters.

View Plain
    1. SYSTEM.OUT.PRINTLN ("Send number:" + smsaddress + "\ n" + "SMS Content:")
    2. + smscontent);

But add them to the for loop because Smsaddress and smscontent will be replaced when new information is sent.

So if the application is done, it is also judged in the For loop.

Finally, remember to register the listener in the manifest.xml.

View Plain
    1. <receiver android:name="Cn.etzmico.SMSBroadcastReceiver" >
    2. <intent-filter>
    3. <action android:name="Android.provider.Telephony.SMS_RECEIVED" ></action>
    4. </intent-filter>
    5. </receiver>


Also add permissions.

View Plain
    1. <uses-permission android:name="Android.permission.RECEIVE_SMS"></ Uses-permission>

This way, after we run the program, Smsaddress and Smscontent will be assigned as long as the SMS is received.

Here, by the way, add a point of knowledge about the Eclipse program.

I believe many beginners do not know, eclipse comes with a texting plugin that can be implemented to send SMS to the virtual machine. This way, we do not need to start multiple virtual machines at the same time when we are doing SMS applications ...

How does it work? Here's how.

1. Click the window in the menu bar.

2. Find out where the Show View directory is.

3. Select Other ....

Then we find that a window will pop up.

4, in order to facilitate operation, we in the pop-up window of the search bar, directly enter the Emulator Control.

5. Click on the Emulator Control in the list, then click OK, or double-click directly.

So there is a window with many parameters.

The other will have the opportunity to do the introduction, we have to use 4 of them this time.

, we just need to enter the corresponding parameters, select the desired type, and the last point will be sent.

PS: Some people wonder why gray, can not input, can not choose, it is because you did not check the simulator. This plugin can only send messages to an emulator at the same time. The choice of the simulator, and the method called Emulator control, the difference is just input emulator control when the input Devices can be. Which simulator you are currently selecting will send a message to which simulator, no need to enter the simulator number.

SMS Real-time deletion

First, declare contentresolver in the activity class and define it in OnCreate.

View Plain
    1. Static Contentresolver CR;
    2. CR = Getcontentresolver ();

Second, Android's short-message reception mechanism, is to listen to, then into the Inbox and in the notification prompt, the system will not immediately listen to the SMS Inbox and prompts, so in the deletion should pay special attention.

View Plain
  1. New Timer (). Schedule (new TimerTask () {
  2. @Override
  3. public Void Run () {
  4. <span style="White-space:pre" > </span>//Delete SMS code
  5. }
  6. }, 5000);

We can do such a delay, because receive SMS trigger SMS receive class in the OnReceive method, there is no information in the Inbox, even if the priority to a minimum is useless.
So we have to delete the method of SMS to do a delay operation can be, according to my test, 5 seconds is wonderful, because many machines will be due to a variety of reasons early into the system run slow ... If it is longer, the machine is more card, then the event will be added a little more ... The 5000 here is 5000 milliseconds, or 5 seconds.

Also, in order to avoid the deletion of SMS, we can do if judgment, to delete the text message method to add conditions.

View Plain
    1. StringBuffer smsaddress.append (Message.getdisplayoriginatingaddress ()); //Sender phone number
    2. StringBuffer smscontent.append (Message.getdisplaymessagebody ()); //SMS content

In this way, there will be no accidental deletion of text messages.

Finally, the manifest.xml of the permissions in the next issue, a total of 3.

View Plain
      1. <uses-permission android:name="Android.permission.RECEIVE_SMS"/>
      2. <uses-permission android:name="Android.permission.WRITE_SMS"/>
      3. <uses-permission android:name="Android.permission.READ_SMS"/>


http://blog.csdn.net/wop_niaoren19870227/article/details/7077765

SMS (session) Delete (GO)

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.