The Android project enables SMS to be sent, received and intercepted by SMS _android

Source: Internet
Author: User

To be honest, some of the things that are relevant to texting in Android are a bit of a primer. The reason I have to write this blog now is because there is something in the development, and want to share these things to more people to learn, and in the future of the Android text message for other learning will be placed here to make a record, so wrote this long-winded article. If you think this is a good thing, welcome to the collection so that you can see what I have updated in this article more easily later. I'll go through the three aspects of the title to learn a little about the SMS operation in the Android system.

Send a message

Because of the excellent encapsulation of the method of sending SMS in Android, the development of SMS is very simple.

public static void SendMessage (context context, string content, String phonenumber) { 
    Smsmanager SMS = smsmanager.getd Efault (); 
    pendingintent pi = pendingintent.getbroadcast (context, 0, New Intent (), 0); 
    Sms.sendtextmessage (PhoneNumber, NULL, content, PI, null); 
  } 

And don't forget the permission issues:

<uses-permission android:name= "Android.permission.SEND_SMS"/> 

Receipt of SMS

It is relatively complicated to receive SMS messages. And the complicated reasons should be easier to understand--the reception is uncontrollable. That means our cell phone doesn't know when there's a text message coming in. Because it's too passive, there's a powerful thing in Android that you can admire, and that's the broadcast receiver. We register a broadcast receiver and then let the broadcast receiver always listen to the event that the message arrives. In this way, the passive trigger event is perfectly resolved. Let's take a look at this process here:

public void OnReceive (context context, Intent Intent) { 
     
    if (intent.getaction (). Equals (" Android.provider.Telephony.SMS_RECEIVED ")) { 
      Bundle Bundle = Intent.getextras (); 
       
      Object messages[] = (object[]) bundle.get ("PDUs"); 
      Smsmessage smsmessage[] = new Smsmessage[messages.length]; 
      for (int n = 0; n < messages.length n++) { 
        Smsmessage[n] = SMSMESSAGE.CREATEFROMPDU ((byte[)) messages[n]); 
      showtoast (context, "SMS content:" + smsmessage[0].getmessagebody ()); 
    } 
   

To add a permission:

<uses-permission android:name= "Android.permission.RECEIVE_SMS"/> 

and a static registration in manifest:

<receiver android:name= ". Broadcast. Smsreceiver "> 
        <action android:name=" Android.provider.Telephony.SMS_RECEIVED "/> 
      </ Intent-filter> 
    

Interception of SMS

It is much simpler to do a short message interception on the basis of being able to receive text messages. Because can receive, so I just become the first person to receive, and the received SMS no longer spread downward, this completes the short message interception.

Increase Priority:

Block propagation:

Deletion of SMS

Here is a message-related protocol.

Content://sms/inbox Inbox
Content://sms/sent has been sent
Content://sms/draft Draft
Content://sms/outbox Outbox
content://sms/failed Send failed
Content://sms/queued Ready to Send list

Take the Inbox as an example to implement the deletion code for the text message as follows:

To delete text messages that contain a field in a message:

public void Deletesms (String smscontent) { 
    Contentresolver CR = Getcontentresolver (); 
    try { 
      //Prepare the system SMS Inbox URI address 
      uri uri = uri.parse ("Content://sms/inbox"); 
       
      Query Inbox all SMS 
      Cursor Cursor = Cr.query (URI, new string[] {"_id", "address", "person", "body", "date", "type"}, NULL , NULL, NULL); 
      int count = Cursor.getcount (); 
      if (Count > 0) {while 
        (Cursor.movetonext ()) { 
          String BODY = cursor.getstring (Cursor.getcolumnindex (' body ') //Get information Content 
          if (Body.contains (smscontent)) { 
            int id = cursor.getint (cursor.getcolumnindex ("_id")); 
            Cr.delete (Uri.parse ("content://sms"), "_id=" + ID, NULL);}}} 
 
    catch (Exception e) { 
      log.v ("E", E.getmessage ()); 
    } 
   

Delete information based on the latest SMS (deleted objects are contacts rather than text messages)

public void Deletesms () { 
    Contentresolver CR = Getcontentresolver (); 
    Query Inbox all SMS 
    Cursor Cursor = Cr.query (Uri.parse ("Content://sms/inbox"), new string[] {"_id", "thread_id"}, NULL, n ull, null); 
    if (cursor!= null) { 
      cursor.movetofirst (); 
      int a = Cursor.getcount (); 
      int b = Cursor.getcolumncount (); 
      Long threadId = Cursor.getlong (1); 
      Cr.delete (Uri.parse ("content://sms/conversations/" + threadId), NULL, NULL); 
    } 
   

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.