android发短信
SmsActivity.java
main.xml
string.xml
AndroidManifest.xml
1
package
com.pptv.activity;
2
3 import java.util.List;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.telephony.SmsManager;
8 import android.view.View;
9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.Toast;
12
13 public class SmsActivity extends Activity {
14 private EditText mobileText;
15 private EditText contentText;
16 private Button button;
17
18 @Override
19 public void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 mobileText = (EditText) findViewById(R.id.edittext_number);
23 contentText = (EditText) findViewById(R.id.edittext_content);
24 button = (Button) findViewById(R.id.button_send);
25 button.setOnClickListener(new View.OnClickListener() {
26
27 @Override
28 public void onClick(View v) {
29 String strMobile = mobileText.getText().toString();
30 String strContent = contentText.getText().toString();
31 SmsManager smsManager = SmsManager.getDefault();
32 if (strContent.length() > 70) {
33 List<String> contents = smsManager
34 .divideMessage(strContent);
35 for (String sms : contents) {
36 smsManager.sendTextMessage(strMobile, null, sms, null,
37 null);
38 }
39 } else {
40 smsManager.sendTextMessage(strMobile, null, strContent,
41 null, null);
42 }
43 contentText.setText("");
44 Toast.makeText(SmsActivity.this, R.string.send_info, 0).show();
45
46 }
47 });
48 }
49}
2
3 import java.util.List;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.telephony.SmsManager;
8 import android.view.View;
9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.Toast;
12
13 public class SmsActivity extends Activity {
14 private EditText mobileText;
15 private EditText contentText;
16 private Button button;
17
18 @Override
19 public void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 mobileText = (EditText) findViewById(R.id.edittext_number);
23 contentText = (EditText) findViewById(R.id.edittext_content);
24 button = (Button) findViewById(R.id.button_send);
25 button.setOnClickListener(new View.OnClickListener() {
26
27 @Override
28 public void onClick(View v) {
29 String strMobile = mobileText.getText().toString();
30 String strContent = contentText.getText().toString();
31 SmsManager smsManager = SmsManager.getDefault();
32 if (strContent.length() > 70) {
33 List<String> contents = smsManager
34 .divideMessage(strContent);
35 for (String sms : contents) {
36 smsManager.sendTextMessage(strMobile, null, sms, null,
37 null);
38 }
39 } else {
40 smsManager.sendTextMessage(strMobile, null, strContent,
41 null, null);
42 }
43 contentText.setText("");
44 Toast.makeText(SmsActivity.this, R.string.send_info, 0).show();
45
46 }
47 });
48 }
49}
main.xml
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
3 android:orientation = " vertical "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent "
6 >
7 < TextView
8 android:layout_width = " fill_parent "
9 android:layout_height = " wrap_content "
10 android:text = " @string/insert_number "
11 />
12 < EditText
13 android:id = " @+id/edittext_number "
14 android:layout_width = " fill_parent "
15 android:layout_height = " wrap_content "
16 />
17 < TextView
18 android:layout_width = " fill_parent "
19 android:layout_height = " wrap_content "
20 android:text = " @string/insert_sms "
21 />
22 < EditText
23 android:id = " @+id/edittext_content "
24 android:layout_width = " fill_parent "
25 android:layout_height = " wrap_content "
26 android:minLines = " 4 "
27 />
28 < Button
29 android:id = " @+id/button_send "
30 android:layout_width = " wrap_content "
31 android:layout_height = " wrap_content "
32 android:text = " @string/button_send "
33 />
34 </ LinearLayout >
2 < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
3 android:orientation = " vertical "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent "
6 >
7 < TextView
8 android:layout_width = " fill_parent "
9 android:layout_height = " wrap_content "
10 android:text = " @string/insert_number "
11 />
12 < EditText
13 android:id = " @+id/edittext_number "
14 android:layout_width = " fill_parent "
15 android:layout_height = " wrap_content "
16 />
17 < TextView
18 android:layout_width = " fill_parent "
19 android:layout_height = " wrap_content "
20 android:text = " @string/insert_sms "
21 />
22 < EditText
23 android:id = " @+id/edittext_content "
24 android:layout_width = " fill_parent "
25 android:layout_height = " wrap_content "
26 android:minLines = " 4 "
27 />
28 < Button
29 android:id = " @+id/button_send "
30 android:layout_width = " wrap_content "
31 android:layout_height = " wrap_content "
32 android:text = " @string/button_send "
33 />
34 </ LinearLayout >
string.xml
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < resources >
3 < string name = " insert_number " > 请输入对方手机号码: </ string >
4 < string name = " app_name " > 短信发送器 </ string >
5 < string name = " insert_sms " > 请输入短信内容: </ string >
6 < string name = " button_send " > 发送短信 </ string >
7 < string name = " send_info " > 发送成功! </ string >
8 </ resources >
9
2 < resources >
3 < string name = " insert_number " > 请输入对方手机号码: </ string >
4 < string name = " app_name " > 短信发送器 </ string >
5 < string name = " insert_sms " > 请输入短信内容: </ string >
6 < string name = " button_send " > 发送短信 </ string >
7 < string name = " send_info " > 发送成功! </ string >
8 </ resources >
9
AndroidManifest.xml
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2 < manifest xmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.activity "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < application android:icon = " @drawable/icon " android:label = " @string/app_name " >
7 < activity android:name = " .SmsActivity "
8 android:label = " @string/app_name " >
9 < intent - filter >
10 < action android:name = " android.intent.action.MAIN " />
11 < category android:name = " android.intent.category.LAUNCHER " />
12 </ intent - filter >
13 </ activity >
14
15 </ application >
16 < uses - sdk android:minSdkVersion = " 4 " />
17
18 // 加入发短信权限
19 < uses - permission android:name = " android.permission.SEND_SMS " />
20
21 </ manifest >
2 < manifest xmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.activity "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < application android:icon = " @drawable/icon " android:label = " @string/app_name " >
7 < activity android:name = " .SmsActivity "
8 android:label = " @string/app_name " >
9 < intent - filter >
10 < action android:name = " android.intent.action.MAIN " />
11 < category android:name = " android.intent.category.LAUNCHER " />
12 </ intent - filter >
13 </ activity >
14
15 </ application >
16 < uses - sdk android:minSdkVersion = " 4 " />
17
18 // 加入发短信权限
19 < uses - permission android:name = " android.permission.SEND_SMS " />
20
21 </ manifest >