发送短信的关键是通过SmsManager对象的sendTextMessage()方法来实现的,其中sendTextMessage() 方法需要传入5个值,依次是收件人地址(String),发送地址(String) 正文内容(String)发送服务PendingIntent和送达服务(PendingIntent)其中收件人与正文是不可为Null的两个参数
定义个一个PendingIntent,getBroacast()的方法自定义PendingIntent并进行Broadcast,而后使用SmsManager。getDefault(当处理SMS短信相关的活动,例如发送数据,文字与pdu SMS 信息,都需要调用这种静态方法)所预先构建的SmsManager使用sendTextMessage()方法,将相关数据以参数形式带入既可以完成送达任务
首先定义一个Activity
package cn.mw.com.sms; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class EX01_03Activity extends Activity { EditText ed_shoujian, ed_content; Button btn_send; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ed_shoujian = (EditText) findViewById(R.id.ed_shoujian); ed_content = (EditText) findViewById(R.id.ed_content); btn_send = (Button) findViewById(R.id.btn_send); ed_shoujian.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ed_shoujian.setText(""); } }); ed_content.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ed_content.setText(""); } }); btn_send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 获取收件人的信息 String strAddress = ed_shoujian.getText().toString(); // 获取短信的信息 String strMessage = ed_content.getText().toString(); // 创建一取得defaultinstance 的SmsManager对象 SmsManager sm = SmsManager.getDefault(); // 检查收件人电话格式与短信字数是否超过七十个字符 if (isPhoneNumberValid(strAddress) == true && iswithin70(strMessage) == true) { try { PendingIntent mPI = PendingIntent.getBroadcast( getApplicationContext(), 0, new Intent(), 0); sm.sendTextMessage(strAddress, null, strMessage, mPI, null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_LONG).show(); ed_shoujian.setText(""); ed_content.setText(""); } else { //电话格式与短信字符不符合的条件下 Toast提醒用户 if (isPhoneNumberValid(strAddress) == false) { if (iswithin70(strMessage) == false) { Toast.makeText(getApplicationContext(), "电话号码格式错误,短信内容超过七十个字,请检查", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(), "电话号码格式错误", Toast.LENGTH_LONG).show(); } } //短信字符数超过70 else if(iswithin70(strMessage)==false){ Toast.makeText(getApplicationContext(), "短信内容超过七十个,请删除部门内容", Toast.LENGTH_LONG).show(); } } } }); } protected static boolean iswithin70(String text) { // TODO Auto-generated method stub if(text.length()<=70){ return true; } else{ return false; } } //检查字符串是否为电话号码 protected boolean isPhoneNumberValid(String phoneNumber) { boolean isValid=false; String expression="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$"; String expression2="^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$"; CharSequence inputStr=phoneNumber; Pattern pattern=Pattern.compile(expression); Matcher matcher=pattern.matcher(inputStr); Pattern pattern2=Pattern.compile(expression2); Matcher matcher2=pattern.matcher(inputStr); if(matcher.matches()||matcher2.matches()){ isValid=true; } // TODO Auto-generated method stub return isValid; } }
定义的XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_shoujian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收件人" /> <EditText android:id="@+id/ed_shoujian" android:layout_width="314dp" android:layout_height="wrap_content" android:hint="请输入收件人" /> </LinearLayout> <EditText android:id="@+id/ed_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.35" android:hint="请输入内容" /> <Button android:id="@+id/btn_send" android:layout_width="76dp" android:layout_height="wrap_content" android:text="发送" /> </LinearLayout>
发送短信的权限 要在mainfest.xml 里边定义
<uses-permission android:name="android.permission.SEND_SMS"/>
以上程序就完成了发送短信的功能 (发送短信的内容不能超过70个字符,然后电话号码不能是其他字符)
什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Observer设计模式。
首先创建一个Event事件类:
[java] view plaincopyprint?
01. 1. public class EmailListEvent extends ApplicationEvent {
02. 2.
03. 3. private static final long serialVersionUID = 1L;
04. 4. public String address;
05. 5. public String text;
06. 6.
07. 7. public EmailListEvent(Object source) {
08. 8. super(source);
09. 9. }
10.10.
11.11. public EmailListEvent(Object source, String address, String text) {
12.12. super(source);
13.13. this.address = address;
14.14. this.text = text;
15.15. }
16.16.
17.17. public void print() {
18.18. System.out.println("Hello,Spring Event!!!");
19.19. }
20.20. }
1. public class EmailListEvent extends ApplicationEvent {
2.
3. private static final long serialVersionUID = 1L;
4. public String address;
5. public String text;
6.
7. public EmailListEvent(Object source) {
8. super(source);
9. }
10.
11. public EmailListEvent(Object source, String address, String text) {
12. super(source);
13. this.address = address;
14. this.text = text;
15. }
16.
17. public void print() {
18. System.out.println("Hello,Spring Event!!!");
19. }
20. }
其次创建一个ApplicationListener类:
[java] view plaincopyprint?
01. 1. public class EmailNotifier implements ApplicationListener {
02. 2.
03. 3. public void onApplicationEvent(ApplicationEvent evt) {
04. 4. if (evt instanceof EmailListEvent) {
05. 5. EmailListEvent emailEvent = (EmailListEvent) evt;
06. 6. emailEvent.print();
07. 7. System.out.println("the source is:" + emailEvent.getSource());
08. 8. System.out.println("the address is:" + emailEvent.address);
09. 9. System.out.println("the mail's context is :" + emailEvent.text);
10.10. }
11.11.
12.12. }
13.13. }
1. public class EmailNotifier implements ApplicationListener {
2.
3. public void onApplicationEvent(ApplicationEvent evt) {
4. if (evt instanceof EmailListEvent) {
5. EmailListEvent emailEvent = (EmailListEvent) evt;
6. emailEvent.print();
7. System.out.println("the source is:" + emailEvent.getSource());
8. System.out.println("the address is:" + emailEvent.address);
9. System.out.println("the mail's context is :" + emailEvent.text);
10. }
11.
12. }
13. }
接着将Listener注册到Spring的xml文件中:
[html] view plaincopyprint?
01.<?xml version="1.0" encoding="UTF-8"?>
02. <beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:aop="http://www.springframework.org/schema/aop"
05. xmlns:tx="http://www.springframework.org/schema/tx"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
08. http://www.springframework.org/schema/aop
09. http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
10. http://www.springframework.org/schema/tx
11. http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
12.
13. <bean id="emailListListener" ></bean>
14.
15.</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="emailListListener" ></bean>
</beans>
最后创建Demo类:
[java] view plaincopyprint?
01. 1. public class ListenerEventDemo {
02. 2.
03. 3. /**
04. 4. * @param args
05. 5. */
06. 6. public static void main(String[] args) {
07. 7. ApplicationContext context = new ClassPathXmlApplicationContext(
08. 8. "SpringEvent.xml");
09. 9. EmailListEvent emailListEvent = new EmailListEvent("hello",
10.10. "helloSpring@sina.com", "this is a test eamil content");
11.11. //在ApplicationContext中发布一个 ApplicationEvent
12.12. context.publishEvent(emailListEvent);
13.13. }
14.14.
15.15. }
1. public class ListenerEventDemo {
2.
3. /**
4. * @param args
5. */
6. public static void main(String[] args) {
7. ApplicationContext context = new ClassPathXmlApplicationContext(
8. "SpringEvent.xml");
9. EmailListEvent emailListEvent = new EmailListEvent("hello",
10. "helloSpring@sina.com", "this is a test eamil content");
11. //在ApplicationContext中发布一个 ApplicationEvent
12. context.publishEvent(emailListEvent);
13. }
14.
15. }
测试结果:
[plain] view plaincopyprint?
01.# Hello,Spring Event!!!
02.# the source is:hello
03.# the address is:helloSpring@sina.com
04.# the mail's context is :this is a test eamil content
0
http://www.baidu.com/?tn=61089049_pg
96
1373013383643
1373013383739
0
http://www.baidu.com/?tn=61089049_pg
96
1373013383643
1373013383739
0
http://s1.bdstatic.com/r/www/cache/static/global/js/home_f949edf5.js
76
1373013384030
1373013384106
0
http://suggestion.baidu.com/su?wd=&cb=window.bdsug.sugPreRequest&sid=1464_2724_2488_1788_2789_2250_2701&t=1373013384509
68
1373013384526
1373013384594
0
http://s1.bdstatic.com/r/www/cache/static/global/js/tangram-1.3.4c1.0_07038476.js
33
1373013384612
1373013384645
0
http://s1.bdstatic.com/r/www/cache/static/user/js/u_bfce4b69.js
42
1373013384715
1373013384757
0
http://passport.baidu.com/passApi/js/uni_login_wrapper.js?cdnversion=1373013384767
65
1373013384784
1373013384849
Totle time of list : 476
Totle time of page : 252
39
1373013384894
1373013384933
1
http://www.baidu.com/
53
1373013385158
1373013385211
1
http://suggestion.baidu.com/su?wd=&cb=window.bdsug.sugPreRequest&sid=1464_2724_2488_1788_2789_2250_2701&t=1373013385310
42
1373013385318
1373013385360
1
http://passport.baidu.com/passApi/js/uni_login_wrapper.js?cdnversion=1373013385387
35
1373013385391
1373013385426
Totle time of list : 130
Totle time of page : 238
38
1373013385459
1373013385497
2
http://www.baidu.com/
37
1373013385699
1373013385736
2
http://suggestion.baidu.com/su?wd=&cb=window.bdsug.sugPreRequest&sid=1448_2703_2785_1788_2250_2702&t=1373013385810
36
1373013385821
1373013385857
2
http://passport.baidu.com/passApi/js/uni_login_wrapper.js?cdnversion=1373013385864
42
1373013385866
1373013385908
Totle time of list : 115
Totle time of page : 213
36
1373013385941
1373013385977