简单界面截图如下:
一、要使用发送短信服务,首先要在AndroidManifest.xml清单文件中添加该权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="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>
二、在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="input_phone_number">请输入手机号:</string>
<string name="empty_phone_number">手机号码不能为空</string>
<string name="input_sms_content">请输入短信内容:</string>
<string name="empty_sms_content">短信内容不能为空</string>
<string name="send_sms">发送短信</string>
<string name="send_sms_success">短信发送成功</string>
</resources>
<?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
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_phone_number" />
<EditText
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/input_sms_content" />
<EditText
android:id="@+id/etSmsContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/sendSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_sms" />
</LinearLayout>
package com.example.sms;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
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 {
private EditText mPhoneNumber = null;
private EditText mSmsContent = null;
private Button mSendSms = null;
private SmsManager mSmsManager = SmsManager.getDefault( );
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
mPhoneNumber = ( EditText ) findViewById( R.id.etPhoneNumber );
mSmsContent = ( EditText ) findViewById( R.id.etSmsContent );
mSendSms = ( Button ) findViewById( R.id.sendSms );
mSendSms.setOnClickListener( new OnClickListener( ) {
@Override
public void onClick( View v ) {
String phoneNumber = mPhoneNumber.getText( ).toString( );
if ( phoneNumber.equals( "" ) ) {
Toast.makeText( getApplicationContext( ), R.string.empty_phone_number, Toast.LENGTH_LONG ).show( );
return;
}
String smsContent = mSmsContent.getText( ).toString( );
if ( smsContent.equals( "" ) ) {
Toast.makeText( getApplicationContext( ), R.string.empty_sms_content, Toast.LENGTH_LONG ).show( );
return;
}
PendingIntent sentIntent = PendingIntent.getBroadcast( getApplicationContext( ), 0, new Intent( ), 0 );
if ( smsContent.length( ) > 70 ) {
List<String> msgList = mSmsManager.divideMessage( smsContent );
for ( int i = 0, size = msgList.size( ); i < size; i++ ) {
mSmsManager.sendTextMessage( phoneNumber, null, msgList.get( i ), sentIntent, null );
}
} else {
mSmsManager.sendTextMessage( phoneNumber, null, smsContent, sentIntent, null );
}
Toast.makeText( getApplicationContext( ), R.string.send_sms_success, Toast.LENGTH_LONG );
}
} );
}
}