Android短信发送器_08

1.string xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="hello">Hello World, SmsActivity!</string>
 5     <string name="app_name">短信发送器</string>
 6     <string name="lab_number">请输入手机号</string>
 7     <string name="lab_sms">请输短信内容</string>
 8     <string name="lab_btn">发送短信</string>
 9     <string name="sucess">发送成功</string>
10 
11 </resources>

2.mail xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/lab_number" />
11     <EditText
12          android:layout_width="fill_parent"
13         android:layout_height="wrap_content"
14         android:id="@+id/txt_number" />
15     <TextView
16          android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:text="@string/lab_sms" />
19     <EditText
20          android:layout_width="fill_parent"
21         android:layout_height="wrap_content"
22         android:minLines="3"
23         android:id="@+id/txt_sms" />
24     <Button
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:text="@string/lab_btn"
28         android:id="@+id/btn_ok"
29         />
30         
31 
32 </LinearLayout>

3.SmsActivity.java 代码

 1 package FosgeIT.sms;
 2 
 3 import java.util.ArrayList;
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.telephony.SmsManager;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 
12 
13 /*
14  * 
15  * @author YinRQ
16  * 短信模拟器
17  * 2013-07-05 09:09:44
18  */
19 
20 public class SmsActivity extends Activity {
21     
22     //定义窗口元素
23     private EditText txt_number;
24     private EditText txt_sms;
25     private Button btn_ok;
26     
27     /** Called when the activity is first created. */
28     @Override
29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.main);
32         
33         //初始化控件
34         txt_number = (EditText) this.findViewById(R.id.txt_number);
35         txt_sms = (EditText) this.findViewById(R.id.txt_sms);
36         btn_ok = (Button) this.findViewById(R.id.btn_ok);
37         
38         btn_ok.setOnClickListener(new ButtonClickListener());
39     }
40     
41     private final class ButtonClickListener implements View.OnClickListener{
42 
43         public void onClick(View v) {
44             String number = txt_number.getText().toString();
45             String sms = txt_sms.getText().toString();
46             
47             //获取SmsManager 
48             SmsManager manager=SmsManager.getDefault(); 
49             //如果内容大于70字,则拆分为多条
50             ArrayList<String> texts=manager.divideMessage(sms); 
51             //逐条发送短信
52             for(String text:texts) { 
53                 manager.sendTextMessage(number, null, text, null, null); 
54             }                 
55             //发送结果提示
56             Toast.makeText(SmsActivity.this,R.string.sucess, Toast.LENGTH_LONG).show(); 
57 
58         }}
59 }

4.发送短信权限 AndroidManifest.xml文件

1 <uses-permission android:name="android.permission.SEND_SMS"/> 

效果:

关于SmsManager

SDK中的介绍:Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().

方法:

public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

 

destinationAddress: 收件人地址

scAddress: 短信中心号码,null为默认中心号码

sentIntent: 当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。

deliveryIntent: 当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。

如果收件人或者信息为空则抛出 IllegalArgumentException 。

public ArrayList<String> divideMessage (String text)

将大于70字的短信分割为多条。

参数:text    the original message. Must not be null.

返回:an ArrayList of strings that, in order, comprise the original message

sendDataMessage 参数与上类似,只是用于发送Data。

sendMultipartTextMessage发送多条短信,发送内容必须是用divideMessage分割好了的。

posted @ 2013-07-05 10:56  Joye.Net  阅读(298)  评论(0编辑  收藏  举报