---
title: "接收短信的receiver"
author: "zhangle2612"
date: "2010-10-25T18:04:54Z"
tags: []
categories: ["移动开发"]
slug: "receiver"
draft: false
lastmod: "2010-10-25T18:04:54Z"
id: 793520
url: "https://lowry.iteye.com/blog/793520"
---

public class EX06_05SMSreceiver extends BroadcastReceiver 
{ 
  /*
  * 声明静态字符串,并使用
  * android.provider.Telephony.SMS_RECEIVED
  * 作为Action为短信的依据
  */
  private static final String mACTION =
  "android.provider.Telephony.SMS_RECEIVED";
  
  private String str_receive="收到短信!";
  
  @Override 
  public void onReceive(Context context, Intent intent)
  {
    // TODO Auto-generated method stub 
    Toast.makeText(context, str_receive.toString(),
    Toast.LENGTH_LONG).show();
    
    /*判断传来Intent是否为短信*/
    if (intent.getAction().equals(mACTION))
    { 
      /*建构一字符串集合变量sb*/
      StringBuilder sb = new StringBuilder(); 
      /*接收由Intent传来的数据*/
      Bundle bundle = intent.getExtras(); 
      /*判断Intent是有数据*/
      if (bundle != null) 
      { 
        /* pdus为 android内置短信参数 identifier
         * 通过bundle.get("")返回一包含pdus的对象*/
        Object myOBJpdus = (Object) bundle.get("pdus");
        
        /*构造短信对象array,并依据收到的对象长度来创建array的大小*/
        SmsMessage messages = new SmsMessage;  
        
        for (int i = 0; i<myOBJpdus.length; i++) 
        {  
          messages* =
          SmsMessage.createFromPdu((byte) myOBJpdus*);
        }
          
        /* 将送来的短信合并自定义信息于StringBuilder当中 */  
        for (SmsMessage currentMessage : messages) 
        {  
          sb.append("接收到来自:\n");  
          /* 收信人的电话号码 */ 
          sb.append(currentMessage.getDisplayOriginatingAddress());
          sb.append("\n------传来的短信------\n");  
          /* 取得传来信息的BODY */  
          sb.append(currentMessage.getDisplayMessageBody()); 
          Toast.makeText
          (
            context, sb.toString(), Toast.LENGTH_LONG
          ).show();
        }
      }
      
      /* 以Notification(Toase)显示来讯信息  */
      Toast.makeText
      (
        context, sb.toString(), Toast.LENGTH_LONG
      ).show();
       
      /* 返回主Activity */ 
      Intent i = new Intent(context, EX06_05.class); 
      /*自定义一Bundle*/
      Bundle mbundle = new Bundle(); 
      /*将短信信息以putString()方法存入自定义的bundle内*/
      mbundle.putString("STR_INPUT",  sb.toString()); 
      /*将自定义bundle写入Intent中*/
      i.putExtras(mbundle); 
      /*设置Intent的Flag以一个全新的task来运行*/
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(i); 
    } 
  } 
}


收到消息后要返回到的主activity 

public class EX06_05 extends Activity
{ 
  /*声明一个TextView,String数组与两个文本字符串变量*/
  private TextView mTextView1;
  public String strEmailReciver;
  public String strEmailSubject;
  public String strEmailBody;
  
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    
    /*通过findViewById构造器创建TextView对象*/ 
    mTextView1 = (TextView) findViewById(R.id.myTextView1); 
    mTextView1.setText("等待接收短信..."); 
    
    try
    {
      /*取得短信传来的bundle*/
      Bundle bunde = this.getIntent().getExtras(); 
      if (bunde!= null)
      {
        /*将bunde内的字符串取出*/
        String sb = bunde.getString("STR_INPUT");
        /*自定义一Intent来运行寄送E-mail的工作*/
        Intent mEmailIntent = 
        new Intent(android.content.Intent.ACTION_SEND);
        /*设置邮件格式为"plain/text"*/
        mEmailIntent.setType("plain/text");
        
        /*
        * 取得EditText01,02,03,04的值作为
        * 收件人地址,附件,主题,正文
        */
        strEmailReciver =new String{"jay.mingchieh@gmail.com"};
        strEmailSubject = "你有一封短信!!";
        strEmailBody = sb.toString();
        
        /*将取得的字符串放入mEmailIntent中*/
        mEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
        strEmailReciver); 
        mEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
        strEmailSubject);
        mEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
        strEmailBody);
        startActivity(Intent.createChooser(mEmailIntent, 
        getResources().getString(R.string.str_message)));
      }
      else
      {
        finish();
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}