Android 自制发送短信程序
1 package com.turboradio.googlesdk;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import android.app.Activity;
7 import android.app.PendingIntent;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.telephony.SmsManager;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.Toast;
15
16 public class Ex_5_3 extends Activity {
17 private Button send;
18 private EditText phoneNumber;
19 private EditText content;
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.ex_5_3);
24 phoneNumber =(EditText) findViewById(R.id.phoneNumber);
25 content = (EditText)findViewById(R.id.content);
26 send = (Button)findViewById(R.id.send);
27 phoneNumber.setOnClickListener(new Button.OnClickListener(){
28
29 public void onClick(View v) {
30 phoneNumber.setText("");
31 }});
32 content.setOnClickListener(new Button.OnClickListener(){
33
34 public void onClick(View v) {
35 content.setText("");
36 }});
37 send.setOnClickListener(new Button.OnClickListener(){
38
39 public void onClick(View v) {
40 /**短信收信人电话**/
41 String strDestAddress = phoneNumber.getText().toString();
42 /**获取短信的内容**/
43 String strMessage = content.getText().toString();
44 /**构建一个SmsManager对象**/
45 SmsManager smsManager = SmsManager.getDefault();
46 /**检查收件人的电话号码和短信字数是否超过70个字符**/
47 if(isPhoneNumberValid(strDestAddress) && iswithin70(strMessage)){
48 /**两个条件都满足的条件下,发送短信**/
49 PendingIntent mPi = PendingIntent.getBroadcast(Ex_5_3.this, 0, new Intent(), 0);
50 smsManager.sendTextMessage(strDestAddress, null, strMessage+"", mPi, null);
51 Toast.makeText(Ex_5_3.this, "发送成功!", Toast.LENGTH_LONG).show();
52 }else{
53 /**电话格式和短信文字不符合时**/
54 if(!isPhoneNumberValid(strDestAddress)&&!iswithin70(strMessage)){
55 Toast.makeText(Ex_5_3.this, "电话号码错误 + 短信内容超过70个,请检查", Toast.LENGTH_LONG).show();
56 }
57 if(!isPhoneNumberValid(strDestAddress)&&iswithin70(strMessage)){
58 Toast.makeText(Ex_5_3.this, "电话号码错误,请检查 ", Toast.LENGTH_LONG).show();
59 }
60 if(isPhoneNumberValid(strDestAddress)&&!iswithin70(strMessage)){
61 Toast.makeText(Ex_5_3.this, "短信内容超过70个字符!!", Toast.LENGTH_LONG).show();
62 }
63 }
64
65 }});
66
67 }
68 /**检查字符串是否为电话号码 并返回 true false**/
69 public boolean isPhoneNumberValid(String phoneNumber){
70 boolean isValid = false;
71 String expression = "^(?(\\d{3})\\)?(\\d{3})[-]?(\\d{5})$";
72 String expression2 = "^\\(?\\d{3}\\)?[-]?(\\d{4})$";
73 CharSequence inputStr = phoneNumber;
74 /**创建Pattern**/
75 // Pattern pattern = Pattern.compile(expression);
76 // Matcher matcher = pattern.matcher(inputStr);
77 // Pattern pattern2 = Pattern.compile(expression2);
78 // Matcher matcher2 = pattern2.matcher(inputStr);
79 // if(matcher.matches() || matcher2.matches()){
80 // isValid = true;
81 // }
82 // return isValid;
83 return true;
84 }
85 public boolean iswithin70(String text){
86 if(text.length()<=70){
87 return true;
88 }else{
89 return false;
90 }
91 }
92 }
ex5_3.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:background="#FFFFFF"
6 >
7 <TextView
8 android:id="@+id/text1"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="收件人:"
12 android:textSize="8pt"
13 android:layout_marginTop="20dip"
14 />
15 <EditText
16 android:id="@+id/phoneNumber"
17 android:layout_width="match_parent"
18 android:layout_height="wrap_content"
19 android:hint="请输入电话号码"
20 android:layout_toRightOf="@id/text1"
21 android:layout_alignParentTop="true"
22 />
23 <EditText
24 android:id="@+id/content"
25 android:layout_width="fill_parent"
26 android:layout_height="200dip"
27 android:hint="请输入短信内容"
28 android:background="#dcdcdcdc"
29 android:layout_below="@id/phoneNumber"
30 />
31 <Button
32 android:id="@+id/send"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:layout_below="@id/content"
36 android:layout_marginLeft="100dip"
37 android:layout_marginTop="50dip"
38 android:text=" 拨我发送短信 "
39 />
40 </RelativeLayout>