SmsActivity.java
- package com.fisnail;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.telephony.SmsManager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class SmsActivity extends Activity implements OnClickListener {
- /**
- * Button 短信发送按钮
- */
- private Button btn_send;
- /**
- * EditText 电话号码输入框
- */
- private EditText et_phoneNum;
- /**
- * EditText 短信内容输入框
- */
- private EditText et_smsContent;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- btn_send.setOnClickListener(this);
- }
- private void initView() {
- btn_send = (Button) findViewById(R.id.btn_id_sms_send);
- et_phoneNum = (EditText) findViewById(R.id.et_id_sms_phoneNum);
- et_smsContent = (EditText) findViewById(R.id.et_id_sms_smsContent);
- }
- public void onClick(View v) {
- String phoneNum = et_phoneNum.getText().toString();
- String smsContent = et_smsContent.getText().toString();
- SmsManager smsManager = SmsManager.getDefault();// 获取一个短信管理器
- List<String> smsString = smsManager.divideMessage(smsContent);// 将传入短信进行拆分
- for (String sms : smsString) {
- smsManager.sendTextMessage(phoneNum, null, sms, null, null);// 要发送的目标手机号码、短信中心号码、要发送的短信内容、短信是否发送成功、对方是否收到短信
- Toast.makeText(SmsActivity.this,
- getResources().getString(R.string.text_success),
- Toast.LENGTH_LONG).show();
- }
- }
- }
main.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" >
- <TextView
- style="@style/fill_parent"
- android:text="@string/tv_text_sms_title" />
- <TextView
- style="@style/fill_parent"
- android:text="@string/tv_text_sms_phoneNum" />
- <EditText
- android:id="@+id/et_id_sms_phoneNum"
- style="@style/fill_parent"
- android:inputType="number"
- android:singleLine="true" />
- <TextView
- style="@style/fill_parent"
- android:text="@string/tv_text_sms_smsContent" />
- <EditText
- android:id="@+id/et_id_sms_smsContent"
- android:layout_width="fill_parent"
- android:layout_height="150dip" />
- <Button
- android:id="@+id/btn_id_sms_send"
- style="@style/wrap_content"
- android:text="@string/btn_text_sms_send" />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, SmsActivity!</string>
- <string name="app_name">Sms</string>
- <string name="tv_text_sms_title">×××</string>
- <string name="tv_text_sms_phoneNum">请输入手机号</string>
- <string name="tv_text_sms_smsContent">请输入短信内容</string>
- <string name="btn_text_sms_send">发送短信</string>
- <string name="text_success">发送成功</string>
- </resources>
style.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="wrap_content">
- <item name="android:layout_width">wrap_content</item>
- <item name="android:layout_height">wrap_content</item>
- </style>
- <style name="fill_parent">
- <item name="android:layout_width">fill_parent</item>
- <item name="android:layout_height">wrap_content</item>
- </style>
- </resources>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.fisnail"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.SEND_SMS" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".SmsActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>