android发短信
16lz
2021-12-22
android发短信 SmsActivity.java
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 {
14privateEditTextmobileText;
15privateEditTextcontentText;
16privateButtonbutton;
17
18@Override
19publicvoidonCreate(BundlesavedInstanceState){
20super.onCreate(savedInstanceState);
21setContentView(R.layout.main);
22mobileText=(EditText)findViewById(R.id.edittext_number);
23contentText=(EditText)findViewById(R.id.edittext_content);
24button=(Button)findViewById(R.id.button_send);
25button.setOnClickListener(newView.OnClickListener(){
26
27@Override
28publicvoidonClick(Viewv){
29StringstrMobile=mobileText.getText().toString();
30StringstrContent=contentText.getText().toString();
31SmsManagersmsManager=SmsManager.getDefault();
32if(strContent.length()>70){
33List<String>contents=smsManager
34.divideMessage(strContent);
35for(Stringsms:contents){
36smsManager.sendTextMessage(strMobile,null,sms,null,
37null);
38}
39}else{
40smsManager.sendTextMessage(strMobile,null,strContent,
41null,null);
42}
43contentText.setText("");
44Toast.makeText(SmsActivity.this,R.string.send_info,0).show();
45
46}
47});
48}
49}
main.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < LinearLayoutxmlns: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 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < resources >
3 < stringname = " insert_number " > 请输入对方手机号码: </ string >
4 < stringname = " app_name " > 短信发送器 </ string >
5 < stringname = " insert_sms " > 请输入短信内容: </ string >
6 < stringname = " button_send " > 发送短信 </ string >
7 < stringname = " send_info " > 发送成功! </ string >
8 </ resources >
9
AndroidManifest.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < manifestxmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.activity "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < applicationandroid:icon = " @drawable/icon " android:label = " @string/app_name " >
7 < activityandroid:name = " .SmsActivity "
8 android:label = " @string/app_name " >
9 < intent - filter >
10 < actionandroid:name = " android.intent.action.MAIN " />
11 < categoryandroid:name = " android.intent.category.LAUNCHER " />
12 </ intent - filter >
13 </ activity >
14
15 </ application >
16 < uses - sdkandroid:minSdkVersion = " 4 " />
17
18 // 加入发短信权限
19 < uses - permissionandroid:name = " android.permission.SEND_SMS " />
20
21 </ manifest >
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 {
14privateEditTextmobileText;
15privateEditTextcontentText;
16privateButtonbutton;
17
18@Override
19publicvoidonCreate(BundlesavedInstanceState){
20super.onCreate(savedInstanceState);
21setContentView(R.layout.main);
22mobileText=(EditText)findViewById(R.id.edittext_number);
23contentText=(EditText)findViewById(R.id.edittext_content);
24button=(Button)findViewById(R.id.button_send);
25button.setOnClickListener(newView.OnClickListener(){
26
27@Override
28publicvoidonClick(Viewv){
29StringstrMobile=mobileText.getText().toString();
30StringstrContent=contentText.getText().toString();
31SmsManagersmsManager=SmsManager.getDefault();
32if(strContent.length()>70){
33List<String>contents=smsManager
34.divideMessage(strContent);
35for(Stringsms:contents){
36smsManager.sendTextMessage(strMobile,null,sms,null,
37null);
38}
39}else{
40smsManager.sendTextMessage(strMobile,null,strContent,
41null,null);
42}
43contentText.setText("");
44Toast.makeText(SmsActivity.this,R.string.send_info,0).show();
45
46}
47});
48}
49}
main.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < LinearLayoutxmlns: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 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < resources >
3 < stringname = " insert_number " > 请输入对方手机号码: </ string >
4 < stringname = " app_name " > 短信发送器 </ string >
5 < stringname = " insert_sms " > 请输入短信内容: </ string >
6 < stringname = " button_send " > 发送短信 </ string >
7 < stringname = " send_info " > 发送成功! </ string >
8 </ resources >
9
AndroidManifest.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < manifestxmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.activity "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < applicationandroid:icon = " @drawable/icon " android:label = " @string/app_name " >
7 < activityandroid:name = " .SmsActivity "
8 android:label = " @string/app_name " >
9 < intent - filter >
10 < actionandroid:name = " android.intent.action.MAIN " />
11 < categoryandroid:name = " android.intent.category.LAUNCHER " />
12 </ intent - filter >
13 </ activity >
14
15 </ application >
16 < uses - sdkandroid:minSdkVersion = " 4 " />
17
18 // 加入发短信权限
19 < uses - permissionandroid:name = " android.permission.SEND_SMS " />
20
21 </ manifest >
更多相关文章
- android实现发送短信demo
- webservice获取手机归属地
- Android(安卓)Handler Message 传递字符串
- 调用android系统自带功能
- Android(安卓)发送无序广播
- Android(安卓)Training - 使用IntentService执行任务(Lesson 3 -
- Android之极光推送发送自定义消息
- ch07 Android(安卓)回调方法
- Android中我为什么发不了邮件--Android邮件发送详解