欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中监听短信的两种方法

程序员文章站 2022-03-23 13:53:38
1、监听广播 缺点,因为优先级的原因可能接收不到。 代码: public static final string tag = "imichatsmsrecei...

1、监听广播

缺点,因为优先级的原因可能接收不到。

代码:

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() + " : " +

        message.getdisplayoriginatingaddress() + " : " +

        message.getdisplaymessagebody() + " : " +

        message.gettimestampmillis();

        string num = message.getoriginatingaddress();
        log.i(tag, "-------------" + text);
        toast.maketext(context, text, toast.length_long).show();
        sendmessage(num, "来自" + num + "的短信已经收到", 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, "-------------" + "当前卡类型为:" + itype);
    if (itype == telephonymanager.phone_type_gsm)
    {
      toast.maketext(context, "当前卡类型为:gsm", toast.length_long).show();
    }
    else if (itype == telephonymanager.phone_type_cdma)
    {
      toast.maketext(context, "当前卡类型为 : cdma", toast.length_long).show();
    }
    else if (itype == telephonymanager.phone_type_none)
    {
      toast.maketext(context, "当前卡类型为: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、采用观察方法,监听短信数据库

public class smsobserver extends contentobserver
{

  private context mcontext;

  public smsobserver(handler handler , context context)
  {
    super(handler);
    mcontext = context;
  }
  
  public void onchange(boolean selfchange) 
  { 
    super.onchange(selfchange); 
    cursor cursor =null; 
    try
    {
      //读取收件箱中的短信 
      cursor = mcontext.getcontentresolver().query(uri.parse("content://sms/inbox"), null, null, null, "date desc"); 
      string body;
      boolean hasdone =false;
      if (cursor !=null)
      { 
        while (cursor.movetonext())
        {
          body = cursor.getstring(cursor.getcolumnindex("body"));
          if(body !=null)//&& body.equals("【startmyactivity】"
          {
            hasdone =true;
            break;
          }
          if (hasdone)
          {
            break;
          }
        } 
      } 
    }
    catch(exception e)
    {
      e.printstacktrace();
    }
    finally
    {
      if(cursor!=null)    cursor.close();
    }
  }

}


用到的权限:

<uses-permission android:name="android.permission.send_sms" />
  <uses-permission android:name="android.permission.receive_sms" />
  <uses-permission android:name="android.permission.read_sms" />