Two ways to listen for SMS in Android _android

Source: Internet
Author: User

1. Listening and broadcasting

Disadvantage, because the reason for the priority may not be received.

Code:

public static final String TAG = "Imichatsmsreceiver";

  public static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED";
      public void OnReceive (context context, Intent Intent) {if (Intent.getaction (). Equals (Sms_received_action)) {

      smsmessage[] messages = getmessagesfromintent (intent); for (Smsmessage message:messages) {String text = message.getoriginatingaddress () + ":" + Mess Age.getdisplayoriginatingaddress () + ":" + message.getdisplaymessagebody () + ":" + message.gettimest

        Ampmillis ();
        String num = message.getoriginatingaddress ();
        LOG.I (TAG, "-------------" + text);
        Toast.maketext (context, text, Toast.length_long). Show ();

        SendMessage (num, "from" + num + "SMS has received", context); Context.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), True, new Smsobserver (new
      Handler (), context)); }} public void SendMeSsage (String num, string text, context context) {Smsmanager Smsmanager = Smsmanager.getdefault ();
    Pendingintent sentintent = pendingintent.getbroadcast (context, 0, New Intent (), 0);

    String strcontent = text;

    Smsmanager.sendtextmessage (num, null, strcontent, sentintent, NULL);
    Telephonymanager tl = (telephonymanager) context.getsystemservice (Context.telephony_service);
    int itype = Tl.getphonetype ();
    LOG.I (TAG, "-------------" + "current card type is:" + itype); 
    if (Itype = = telephonymanager.phone_type_gsm) {toast.maketext (context, "current card type: GSM", Toast.length_long). Show (); else if (Itype = = TELEPHONYMANAGER.PHONE_TYPE_CDMA) {toast.maketext (context, "current card type: CDMA", Toast.)
    Length_long). Show (); else if (Itype = = Telephonymanager.phone_type_none) {toast.maketext (context, "current card type: NONE", toast.length_
    LONG). Show (); } public final smsmessage[] Getmessagesfromintent (Intent Intent) {object[] messages = (OBject[]) Intent.getserializableextra ("PDUs");

    byte[][] Pduobjs = new byte[messages.length][];

    for (int i = 0; i < messages.length i++) {pduobjs[i] = (byte[)) messages[i];

    } byte[][] PDUs = new byte[pduobjs.length][];

    int pducount = Pdus.length;

    smsmessage[] msgs = new Smsmessage[pducount];

      for (int i = 0; i < Pducount i++) {pdus[i] = pduobjs[i];

    Msgs[i] = SMSMESSAGE.CREATEFROMPDU (Pdus[i]);

  return msgs;

 }



2, the use of observation methods, listening to the SMS database

public class Smsobserver extends Contentobserver {private context mcontext;
    Public Smsobserver (Handler Handler) {super (Handler);
  Mcontext = context; 
    } public void OnChange (Boolean selfchange) {Super.onchange (selfchange); 
    Cursor Cursor =null; try {//Read the message in the Inbox cursor = Mcontext.getcontentresolver (). Query (Uri.parse ("Content://sms/inbox"), NULL, NU 
      ll, NULL, "date desc");
      String body;
      Boolean Hasdone =false; if (cursor!=null) {while (Cursor.movetonext ()) {BODY = cursor.getstring (Cursor.getcolu
          Mnindex ("body"));
            if (body!=null)//&& body.equals ("" Startmyactivity "" {Hasdone =true;
          Break
          } if (Hasdone) {break;
    catch (Exception e) {e.printstacktrace ()}}}; finally {if (cursor!=null) cursor.close ();
    }
  }

}

 


Permissions to use:

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

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.