1. 示例代码
activity_main.xml 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_number" /> <EditText android:id="@+id/et_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_content" /> <EditText android:id="@+id/et_content" android:minLines="3" android:minHeight="100px" android:lines="5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" /> <Button android:id="@+id/bt_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send_sms" /> </LinearLayout>
AndroidManifest.xml 权限管理
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.sms.MainActivity" 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>
MainActivity.java
import java.util.List; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; private EditText et_content; private Button bt_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_content = (EditText) findViewById(R.id.et_content); et_number = (EditText) findViewById(R.id.et_number); bt_send = (Button) findViewById(R.id.bt_send); bt_send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String number = et_number.getText().toString().trim(); String content = et_content.getText().toString().trim(); if(TextUtils.isEmpty(number)||TextUtils.isEmpty(content)){ Toast.makeText(MainActivity.this, "号码或者短信内容不能为空", Toast.LENGTH_LONG).show(); return ; } //发送短信 SmsManager smsManager = SmsManager.getDefault(); List<String> smsMessages = smsManager.divideMessage(content); for(String smsMessage : smsMessages){ smsManager.sendTextMessage(number, null, smsMessage, null, null); Toast.makeText(MainActivity.this, "短信发送完毕", Toast.LENGTH_LONG).show(); } } }); } }