接收短信的receiver

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[myOBJpdus.length];

for (int i = 0; i<myOBJpdus.length; i++)
{
messages[i] =
SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
}

/* 将送来的短信合并自定义信息于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();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值